如题所示,我将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
                }
            }

解决方案 »

  1.   

    首先要看看
    读进来以后 table里是不是乱码其次要看看数据库的语言排序,数据库的属性中能看到
      

  2.   

    插入语句 在每个字段的值前面加上N 或者将你数据库中的字段 可能出现中文的字段类型改为nvarchar
      

  3.   

    可是我的数据库中,中英文字段都设置的是ntext啊
      

  4.   

      strSql += myDs.Tables[0].Rows[i].ItemArray[4].ToString() + "', '";//是不是你的 逗号全角半角问题,  不是的话,先改变一下你的字段类型试试。排查问题嘛