我试图从ASP.NET 2005运行此SQL,但我得到一个无效的SQL错误,因为它有一个奇怪的字符”.
在我的代码中,我试图替换”,"但它没有这样做,因为替换命令中的特殊字符正在改变".
查询:
insert into userquery(description)
values ('In fact, Topeka Google Mayor Bill Bunten expressed it best: “Don’t be fooled. Even Google recognizes that all roads lead to Kansas, not just yellow brick ones.”')
Run Code Online (Sandbox Code Playgroud)
如果我在mysql中复制并执行它,它运行良好.
我怎么解决这个问题?
您正在构建错误的查询.您没有在客户端上说c#或vb.net,但无论哪种方式,您的sql字符串应该看起来更像这样:
string query = "insert into userquery(description) values (@description)";
Run Code Online (Sandbox Code Playgroud)
然后你设置你的描述值如下:
using (var cn = new MySqlConnection("... your connection string here..."))
using (var cmd = new MySqlCommand(query, cn))
{
/* THIS IS THE IMPORTANT PART */
cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = MyDescriptionVariable;
/*****************************/
cn.Open();
cmd.ExecuteNonQuery();
}
Run Code Online (Sandbox Code Playgroud)
和vb版本:
Using cn As New MySqlConnection("... your connection string here..."), _
cmd As New MySqlCommand(query, cn)
''# THIS IS THE IMPORTANT PART
cmd.Parameters.Add("@description", SqlDbType.VarChar).Value = MyDescriptionVariable
''############################
cn.Open()
cmd.ExecuteNonQuery()
End Using
Run Code Online (Sandbox Code Playgroud)
这将确保您的参数被正确转义,无论您有多么奇怪的字符.
| 归档时间: |
|
| 查看次数: |
304 次 |
| 最近记录: |