2016-03-18 101 views
-1

我使用excel表將阿拉伯文字母數據保存爲數據庫爲「?」

上傳數據到DB表我將數據上載到ProductStatisticsTemp

在該表中我具有以下

  • PRODUCT_ID
  • ProductNameEn
  • 產品名稱Ar

這個上傳數據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

你應該儘量平分問題裏面。在從Excel讀取期間或在寫入數據庫過程中,問題是否發生? – xanatos

+1

但是我已經可以看到問題了:不要以這種方式編寫INSERT(用值組成一個字符串)。使用'SqlParameter'! – xanatos

+0

@xanatos我可以舉一些例子 – kez

回答

0
  • 您使用非Unicode字符串'value'時,你應該使用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循環
+0

我收到以下錯誤''SqlCommand'不包含'AddWithValue'的定義,並且沒有接受'SqlCommand'類型的第一個參數的擴展方法'AddWithValue'可以找到' – kez

+0

抱歉,那應該是cmd.Parameters。 AddWithValue()。請參閱MSDN https://msdn.microsoft.com/zh-CN/library/system.data.sqlclient.sqlcommand.parameters.aspx – devio

相關問題