使用参数化查询语句进行查询的示例:
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注入。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:为什么参数化查询可以防止sql注入