C# 阿拉伯文字母数据保存到数据库中为“quot;”&引用;

C# 阿拉伯文字母数据保存到数据库中为“quot;”&引用;,c#,asp.net-mvc,file-upload,datatable,oledb,C#,Asp.net Mvc,File Upload,Datatable,Oledb,我正在使用excel工作表将数据上传到DB表 我正在将数据上载到ProductStatisticsTemp表 在该表中,我有以下列 产品标识 ProductNameEn 产品名称 这是上传数据的POST方法 [HttpPost] public ActionResult FileUpload(HttpPostedFileBase file) { if (Request.Files["file"].ContentLength > 0)

我正在使用excel工作表将数据上传到DB表

我正在将数据上载到
ProductStatisticsTemp

在该表中,我有以下列

  • 产品标识
  • ProductNameEn
  • 产品名称
这是上传数据的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");
    }
[HttpPost]
公共操作结果文件上载(HttpPostedFileBase文件)
{
if(Request.Files[“file”].ContentLength>0)
{
字符串fileExtension=System.IO.Path.GetExtension(Request.Files[“file”].FileName);
如果(文件扩展名==“.xls”| |文件扩展名==“.xlsx”)
{
字符串fileLocation=Server.MapPath(“~/Content/ExcelFiles/”)+Request.Files[“file”].FileName;
if(System.IO.File.Exists(fileLocation))
{
System.IO.File.Delete(fileLocation);
}
Request.Files[“file”].SaveAs(文件位置);
字符串excelConnectionString=string.Empty;
excelConnectionString=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“+fileLocation+”“扩展属性=\”Excel 12.0;HDR=Yes;IMEX=2\”;
//xls文件格式的连接字符串。
如果(文件扩展名=“.xls”)
{
excelConnectionString=“Provider=Microsoft.Jet.OLEDB.4.0;数据源=“+fileLocation+”“扩展属性=\”Excel 8.0;HDR=Yes;IMEX=2\”;
}
//xlsx文件格式的连接字符串。
else if(文件扩展名=“.xlsx”)
{
excelConnectionString=“Provider=Microsoft.ACE.OLEDB.12.0;数据源=“+fileLocation+”“扩展属性=\”Excel 12.0;HDR=Yes;IMEX=2\”;
}
//创建到Excel工作簿的连接并添加oledb命名空间
OleDbConnection excelConnection=新的OleDbConnection(excelConnectionString);
excelConnection.Open();
DataTable dt=新的DataTable();
dt=excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
如果(dt==null)
{
返回null;
}
String[]excelSheets=新字符串[dt.Rows.Count];
int t=0;
//excel数据保存在此处的临时文件中。
foreach(数据行中的数据行)
{
excelSheets[t]=行[“表名称”]。ToString();
t++;
}
OLEDB连接excelConnection1=新的OLEDB连接(excelConnectionString);
string query=string.Format(“Select*from[{0}]”,excelSheets[0]);
使用(OleDbDataAdapter dataAdapter=新的OleDbDataAdapter(查询,excelConnection1))
{
dataAdapter.Fill(ds);
}
}
if(fileExtension.ToString().ToLower().Equals(“.xml”))
{
字符串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=新的XmlTextReader(文件位置);
//数据集ds=新数据集();
ReadXml(xmlreader);
xmlreader.Close();
}
对于(int i=0;i
但在这里数据保存平稳,但
ProductNameAr
字段阿拉伯字母值另存为“?”

例如:如果excel值
Albrka
然后将其保存在数据库表中为Albrka

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

另外,数据库表中的此
ProductNameAr
数据类型为NVARCHAR

  • 当您应该以
    N'value'

  • 最好重写为参数化的SqlCommand,使用


string query=“插入ProductStatisticsTemp(Product\u ID、ProductNameEn、ProductNameAr)值(@Product\u ID、@ProductNameEn、@ProductNameAr)”;
SqlCommand cmd=新的SqlCommand(查询,con);
cmd.Parameters.AddWithValue(“@Product_ID”,ds.Tables[0]。行[i][0]);
等

  • 您不需要在循环中重新创建和打开
    SqlConnection

您应该尝试将问题一分为二。问题是在从Excel读取还是在写入DB的过程中发生的?但我已经看到了问题:不要以那种方式编写INSERT(用值编写字符串)。使用
SqlParameter
!@xanatos我可以看看一些示例吗?”插入sqlparameter c#“…您应该能够找到许多示例。我得到以下错误
“SqlCommand”不包含“AddWithValue”的定义,并且找不到接受“SqlCommand”类型的第一个参数的扩展方法“AddWithValue”
抱歉,应该是这样的