阿拉伯字母数据保存到数据库为“?”

时间:2016-03-18 10:58:01

标签: c# asp.net-mvc file-upload datatable oledb

我正在使用Excel工作表将数据上传到数据库表

我正在将数据上传到ProductStatisticsTemp

在该表中,我有以下列

  • PRODUCT_ID
  • ProductNameEn
  • ProductNameAr

这是上传数据POST方法

    [HttpPost]
    public ActionResult FileUpload(HttpPostedFileBase file)
    {
        if (Request.Files["file"].ContentLength > 0)
        {
            string fileExtension = System.IO.Path.GetExtension(Request.Files["file"].FileName);

            if (fileExtension == ".xls" || fileExtension == ".xlsx")
            {
                string fileLocation = Server.MapPath("~/Content/ExcelFiles/") + Request.Files["file"].FileName;
                if (System.IO.File.Exists(fileLocation))
                {
                    System.IO.File.Delete(fileLocation);
                }

                Request.Files["file"].SaveAs(fileLocation);
                string excelConnectionString = string.Empty;
                excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";

                //connection String for xls file format.
                if (fileExtension == ".xls")
                {
                    excelConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
                }
                //connection String for xlsx file format.
                else if (fileExtension == ".xlsx")
                {
                    excelConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
                }
                //Create Connection to Excel work book and add oledb namespace
                OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
                excelConnection.Open();
                DataTable dt = new DataTable();

                dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                if (dt == null)
                {
                    return null;
                }

                String[] excelSheets = new String[dt.Rows.Count];
                int t = 0;
                //excel data saves in temp file here.
                foreach (DataRow row in dt.Rows)
                {
                    excelSheets[t] = row["TABLE_NAME"].ToString();
                    t++;
                }

                OleDbConnection excelConnection1 = new OleDbConnection(excelConnectionString);
                string query = string.Format("Select * from [{0}]", excelSheets[0]);

                using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                {
                    dataAdapter.Fill(ds);
                }
            }

            if (fileExtension.ToString().ToLower().Equals(".xml"))
            {
                string fileLocation = Server.MapPath("~/Content/ExcelFiles") + Request.Files["FileUpload"].FileName;

                if (System.IO.File.Exists(fileLocation))
                {
                    System.IO.File.Delete(fileLocation);
                }

                Request.Files["FileUpload"].SaveAs(fileLocation);
                XmlTextReader xmlreader = new XmlTextReader(fileLocation);
                // DataSet ds = new DataSet();
                ds.ReadXml(xmlreader);
                xmlreader.Close();
            }


            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {

                string conn = ConfigurationManager.ConnectionStrings["dbconnection"].ConnectionString;
                SqlConnection con = new SqlConnection(conn);
                string query = "Insert into ProductStatisticsTemp(Product_ID,ProductNameEn,ProductNameAr) Values('" + ds.Tables[0].Rows[i][0] + "','" + ds.Tables[0].Rows[i][1] + "','" + ds.Tables[0].Rows[i][2] )";
                con.Open();

                SqlCommand cmd = new SqlCommand(query, con);
                cmd.ExecuteNonQuery();
                con.Close();
            }
        }

        return RedirectToAction("FileUpload", "FileUpload");
    }

但是在这里数据保存顺利,但ProductNameAr字段阿拉伯字母值保存为“?”

例如:如果excel值حساب التوفير للاستثمار Albrka 然后将其保存在数据库表中???? ??????? ????????? Albrka

如何在Excel工作表中保存为精确格式

PS。数据库表中的此ProductNameAr数据类型为NVARCHAR

1 个答案:

答案 0 :(得分:0)

  • 当您应使用'value'

  • 形式的Unicode文字时,使用非Unicode字符串文字N'value'
  • 您最好重写为参数化SqlCommand使用AddWithValue传递值

string query = "Insert into ProductStatisticsTemp(Product_ID,ProductNameEn,ProductNameAr) Values(@Product_ID,@ProductNameEn,@ProductNameAr)"; SqlCommand cmd = new SqlCommand(query, con); cmd.Parameters.AddWithValue("@Product_ID", ds.Tables[0].Rows[i][0]); ...etc...

  • 您无需重新创建并打开循环中的SqlConnection