【发布时间】:2015-05-01 15:26:43
【问题描述】:
听说用参数化查询可以防止SQL注入攻击,但是不知道怎么写。
如何将以下内容编写为参数化查询?
SqlConnection con = new SqlConnection(
"Data Source=" + globalvariables.hosttxt + "," + globalvariables.porttxt + "\\SQLEXPRESS;" +
"Database=ha;" +
"Persist Security Info=false;" +
"UID='" + globalvariables.user + "';" +
"PWD='" + globalvariables.psw + "'");
string query = "SELECT distinct ha FROM app WHERE 1+1=2";
if (comboBox1.Text != "")
{
query += " AND firma = '" + comboBox1.Text + "'";
}
if (comboBox2.Text != "")
{
query += " AND type = '" + comboBox2.Text + "'";
}
if (comboBox3.Text != "")
{
query += " AND farve = '" + comboBox3.Text + "'";
}
SqlCommand mySqlCmd = con.CreateCommand();
mySqlCmd.CommandText = query;
con.Open();
…
【问题讨论】:
标签: c# .net ado.net sql-injection parameterized-query