求助:C# WINFORM程序中 SQL防注入的代码

来源:百度知道 编辑:UC知道 时间:2024/05/30 16:04:45
求一段实用的代码,应用在C# 窗体程序设计当中,用来验证用户注册以及发布相关信息时,对其发布数据进行检验 ,防止写入SQL注入代码。最好有使用的例子。谢谢大家,实用的代码会有分数送的。

不知道你用的是什么数据库
给你一段SQL-Server的代码
防止注入式攻击,最重要的是不要用拼接参数方式,使用参数赋值方式。
SqlConnection conn=......
SqlCommand comm =new SqlCommand ("select count (*)from Table1 where name = @loginame and password = @loginpassword",conn);
comm.Parameters.Add(new SqlParameter("@loginame",SqlDbType.NVarchar,20);
comm.Parameters["@loginame"].value=TextBox1.Text;
comm.Parameters.Add(new SqlParameter("@loginpassword",SqlDbType.NVarchar,20);
comm.Parameters["@loginpassword"].value=TextBox2.Text;
comm.Connection.Open();
int mark=(int)comm.ExecuteScalar()
//--mark用于标记
...........

剩下的东西你应该会写了

防止注入的最好方法就是使用Parameters 指定了参数的明确类型和长度 来传递变量

在SQL环境中建立存储过程,然后在VS里调用该存储过程,不仅避免SQL注入式攻击而且提高数据访问速度。

用存储过程