根据上面帖子中的问题,大多数人建议使用“参数化查询”来避免 SQL 注入。
以下是我使用 SQL 注入的代码:
public DataSet checkemp(string user)
{
strsql = "SELECT * from employee where employeeid = @userid";
SqlConnection con = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter(strsql, connectionString);
da.SelectCommand.Parameters.Add("@userid", SqlDbType.VarChar, 50).Value = user;
// pretend the user name is "Micheal"
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
con.Dispose();
return ds;
}
在调试过程中,当我指向“strsql”标签时,我只能获得查询语句“SELECT * from employee where employeeid = @userid”,而不是“SELECT * from employee where employeeid = 'Micheal'”。
有什么解决办法可以解决这个问题并使其最有效?谢谢大家!
da.SelectCommand.Parameters
中的所有参数。此外,您可以尝试先创建一个参数列表,然后将它们添加到List<SqlParameter> paramList
中,然后循环遍历paramList
并将它们添加到命令中。 - Alex W