如题所示,我将excel导入sql数据库中,英文和数字没问题,中文却是乱码,怎么回事? 拜托大家帮帮忙啊! protected void ButtonUpload_click(object sender, EventArgs e)
{
Label3.Text = "double click the upload button";
int countContract = 0;
if (path == "")// 如果路径为空
{
Response.Write("");
}
else
{
try
{
string mystring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = '" + path + "';Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection(mystring);
OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [" + DropDownList1.SelectedItem.Text.ToString().Trim() + "]", cnnxls);
DataSet myDs = new DataSet();
myDa.Fill(myDs); // excel 数据都在myDS中
//excel中有记录的话
if (myDs.Tables[0].Rows.Count > 0)
{
string strSql = "";
string CnnString = "Data Source=.\\SQLEXPRESS;Initial Catalog=askbian;User Id=sa;Password=sa";
// string CnnString = "Provider=Microsoft.Jet.OLEDB.3.51; DataSource=C:\\Program Files (x86)\\Microsoft SQL Server\\MSSQL10.SQLEXPRESS\\MSSQL\\DATA\\askbian.mdf";
SqlConnection conn = new SqlConnection(CnnString); conn.Open();
SqlCommand myCmd = null; #region//导入excel中的每行
for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
{
//判断excel第一列是否为空,为空,则不导入。
object a = myDs.Tables[0].Rows[i].ItemArray[0].ToString();
if (myDs.Tables[0].Rows[i].ItemArray[0].ToString() == "" || myDs.Tables[0].Rows[i].ItemArray[0].ToString() == "序号")
{
;
}
//第一列不为空
else
{ strSql = "insert into QAtest (ID,QuestionCHS,QuestionENG,AnswerCHS,AnswerENG) values ('";
strSql += myDs.Tables[0].Rows[i].ItemArray[0].ToString() + "', '";//
strSql += myDs.Tables[0].Rows[i].ItemArray[3].ToString() + "', '";//
strSql += myDs.Tables[0].Rows[i].ItemArray[1].ToString() + "', '";//
strSql += myDs.Tables[0].Rows[i].ItemArray[4].ToString() + "', '";//
strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "')";//
countContract++;
myCmd = new SqlCommand(strSql, conn);
myCmd.ExecuteNonQuery();
}
}
conn.Close();
Response.Write("");
#endregion
}
}
{
Label3.Text = "double click the upload button";
int countContract = 0;
if (path == "")// 如果路径为空
{
Response.Write("");
}
else
{
try
{
string mystring = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = '" + path + "';Extended Properties=Excel 8.0";
OleDbConnection cnnxls = new OleDbConnection(mystring);
OleDbDataAdapter myDa = new OleDbDataAdapter("select * from [" + DropDownList1.SelectedItem.Text.ToString().Trim() + "]", cnnxls);
DataSet myDs = new DataSet();
myDa.Fill(myDs); // excel 数据都在myDS中
//excel中有记录的话
if (myDs.Tables[0].Rows.Count > 0)
{
string strSql = "";
string CnnString = "Data Source=.\\SQLEXPRESS;Initial Catalog=askbian;User Id=sa;Password=sa";
// string CnnString = "Provider=Microsoft.Jet.OLEDB.3.51; DataSource=C:\\Program Files (x86)\\Microsoft SQL Server\\MSSQL10.SQLEXPRESS\\MSSQL\\DATA\\askbian.mdf";
SqlConnection conn = new SqlConnection(CnnString); conn.Open();
SqlCommand myCmd = null; #region//导入excel中的每行
for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
{
//判断excel第一列是否为空,为空,则不导入。
object a = myDs.Tables[0].Rows[i].ItemArray[0].ToString();
if (myDs.Tables[0].Rows[i].ItemArray[0].ToString() == "" || myDs.Tables[0].Rows[i].ItemArray[0].ToString() == "序号")
{
;
}
//第一列不为空
else
{ strSql = "insert into QAtest (ID,QuestionCHS,QuestionENG,AnswerCHS,AnswerENG) values ('";
strSql += myDs.Tables[0].Rows[i].ItemArray[0].ToString() + "', '";//
strSql += myDs.Tables[0].Rows[i].ItemArray[3].ToString() + "', '";//
strSql += myDs.Tables[0].Rows[i].ItemArray[1].ToString() + "', '";//
strSql += myDs.Tables[0].Rows[i].ItemArray[4].ToString() + "', '";//
strSql += myDs.Tables[0].Rows[i].ItemArray[2].ToString() + "')";//
countContract++;
myCmd = new SqlCommand(strSql, conn);
myCmd.ExecuteNonQuery();
}
}
conn.Close();
Response.Write("");
#endregion
}
}
解决方案 »
- C#GBK转码问题
- 晚上就回去了,发表一个问题大家讨论下!
- 新手求教! 在C#里面利用委托做连个页面直接传值?
- 如何用c#来实现FTPS,给点思路也好。
- 日期解析字符串
- 急急急!!!关于在datagrid中嵌入checkbox的问题,在线等...
- 怎么读服务器的事件查看器到本机上
- web服务的问题
- 谁有DevPartner Studio Professional Edition?
- 这是我的第一个发布的细细软件,http://www.csdn.net/cnshare/soft/10/10423.shtm,请指教!
- 为什么MYSQL 官方都没有 64位的 Connector/Net ?
- 请教 一个数据库还原问题?
读进来以后 table里是不是乱码其次要看看数据库的语言排序,数据库的属性中能看到