温馨提示×

参数化sql查询如何防止sql注入

小新
193
2021-01-07 10:44:48
栏目: 云计算

参数化sql查询如何防止sql注入

使用参数化查询语句进行查询的示例:

string Account =Request.Form["Account"];

string sql = "select id,Name,Account from User where Account = @Account";

SqlParameter[] values = new SqlParameter[] { //参数化查询, 防止sql注入

new SqlParameter("@Account",Account),

};

DataTable datatable = DBHelper.GetDataTable(sql, values);

//把用户传到后台的账号Account赋值给@Account,这样数据库服务器不会将参数的内容视为SQL指令来处理,而是在数据库完成SQL指令的编译后,才套用参数运行,从而防止SQL注入。

0