我试图防止所有查询中出现任何 SQL 注入,并且想知道如何在此查询中添加双引号。谢谢
string.Format("SELECT TOP 10 article_guid, article_title
FROM article
WHERE article.article_isdeleted = 0 AND
FREETEXT(article_title, @val)");
第 1 步:不要这样做。用一个参数化查询反而。
参数化查询消除了与 SQL 注入攻击相关的大部分风险。
从链接:
private void CallPreparedCmd() {
string sConnString =
"Server=(local);Database=Northwind;Integrated Security=True;";
string sSQL =
"UPDATE Customers SET City=@sCity WHERE CustomerID=@sCustomerID";
using (SqlConnection oCn = new SqlConnection(sConnString)) {
using (SqlCommand oCmd = new SqlCommand(sSQL, oCn)) {
oCmd.CommandType = CommandType.Text;
oCmd.Parameters.Add("@sCustomerID", SqlDbType.NChar, 5);
oCmd.Parameters.Add("@sCity", SqlDbType.NVarChar, 15);
oCn.Open();
oCmd.Prepare();
oCmd.Parameters["@sCustomerID"].Value = "ALFKI";
oCmd.Parameters["@sCity"].Value = "Berlin2";
oCmd.ExecuteNonQuery();
oCmd.Parameters["@sCustomerID"].Value = "CHOPS";
oCmd.Parameters["@sCity"].Value = "Bern2";
oCmd.ExecuteNonQuery();
oCn.Close();
}
}
}
话虽这么说,您可以通过转义双引号将引号插入到字符串中,如下所示:
string newstring = " \"I'm Quoted\" ";
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)