【问题标题】:Send Unicode string from MS Access to SQL Server using a DataSet使用数据集将 Unicode 字符串从 MS Access 发送到 SQL Server
【发布时间】:2016-11-27 14:37:19
【问题描述】:

我正在尝试从 MS Access 数据库中获取字符串并使用数据集插入 SQL Server 数据库。但是我的 SQL 语句中的 utf8 字符串像 ????????? 这样插入 - 我该怎么办?

这是我的代码:

OleDbCommand cmd2 = new OleDbCommand("select * from t_about_us", con_access);
OleDbDataAdapter da2 = new OleDbDataAdapter(cmd2);

DataSet ds2 = new DataSet();
da2.Fill(ds2, "t_about_us");

con.Open();
string command2 = "insert into t_about_us(matn,see,metatag_description,metatag_keywords,metatag_author) values('" +
          Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(ds2.Tables[0].Rows[0]["matn"].ToString())) + "','" +
           Convert.ToInt32(ds2.Tables[0].Rows[0]["see"].ToString()) + "','" +
           ds2.Tables[0].Rows[0]["metatag_description"].ToString() + "','" +
           ds2.Tables[0].Rows[0]["metatag_keywords"].ToString() + "','" +
           ds2.Tables[0].Rows[0]["metatag_author"].ToString() + "')";

SqlCommand cmdd2 = new SqlCommand(command2, con);
cmdd2.ExecuteNonQuery();
con.Close();

【问题讨论】:

  • SQL Injection alert - 您应该将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入

标签: c# sql sql-server ms-access encoding


【解决方案1】:

通过使用 动态 SQL 来构造一个带有像 'this' 这样的字符串文字的 SQL 语句,您将字符串从 Unicode 隐式转换为 SQL Server 使用的单字节字符集,以及任何 Unicode未映射到该目标字符集的字符将被问号替换。

因此,例如,对于我的 SQL Server ...

cmd.CommandText = "INSERT INTO myTable (textCol) VALUES ('γιορτή')";
cmd.ExecuteNonQuery();

... 将被插入为 ...

????t?

...即使 [textCol] 被定义为 NVARCHAR 列。

正确的做法是使用参数化查询,像这样

cmd.CommandText = "INSERT INTO myTable (textCol) VALUES (@word)";
cmd.Parameters.Add("@word", System.Data.SqlDbType.NVarChar).Value = "γιορτή";
cmd.ExecuteNonQuery();

【讨论】:

    【解决方案2】:

    谢谢我的朋友们终于我的代码工作了,这就是答案:

    OleDbCommand cmd2 = new OleDbCommand("select * from t_about_us", con_access);
            OleDbDataAdapter da2 = new OleDbDataAdapter(cmd2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2, "t_about_us");
    
            con.Open();
            SqlCommand cmd1 = new SqlCommand("INSERT INTO t_about_us(matn,see,metatag_description,metatag_keywords,metatag_author) VALUES (@matn,@see,@metatag_description,@metatag_keywords,@metatag_author)",con);
            cmd1.Parameters.Add("@see", System.Data.SqlDbType.BigInt).Value = Convert.ToInt32(ds2.Tables[0].Rows[0]["see"].ToString());
            cmd1.Parameters.Add("@matn", System.Data.SqlDbType.NVarChar).Value = ds2.Tables[0].Rows[0]["matn"].ToString();
            cmd1.Parameters.Add("@metatag_description", System.Data.SqlDbType.NVarChar).Value = ds2.Tables[0].Rows[0]["metatag_description"].ToString();
            cmd1.Parameters.Add("@metatag_keywords", System.Data.SqlDbType.NVarChar).Value = ds2.Tables[0].Rows[0]["metatag_keywords"].ToString();
            cmd1.Parameters.Add("@metatag_author", System.Data.SqlDbType.NVarChar).Value = ds2.Tables[0].Rows[0]["metatag_author"].ToString();
            cmd1.ExecuteNonQuery();
            con.Close();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      相关资源
      最近更新 更多