在 SqlDataAdapter 中防止 SQL 注入的最佳方法 [英] Best way to protect against SQL injection in SqlDataAdapter

查看:53
本文介绍了在 SqlDataAdapter 中防止 SQL 注入的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想知道在 SqlDataAdapter 中防止 SQL 注入 的最佳方法是什么(因为无法使用参数化查询)?

Hello I'm wondering what is the best way to protect against SQL injection in SqlDataAdapter (as there is no way to use parameterized query)?

例如让我们使用这部分代码:

For example lets use this part of code:

da_services = new SqlDataAdapter("SELECT * from table WHERE column='" + textBox1.Text + "' AND column2='" + somestring + "'", conn);
scd_services = new SqlCommandBuilder(da_services);
dt_services = new DataTable();
da_services.Fill(dt_services);
dtg_services.DataSource = dt_services;
conn.Close();

感谢您抽出宝贵时间.

推荐答案

可以尝试访问DataAdapter的SqlCommand对象:

You can try accessing the SqlCommand object of the DataAdapter:

da_services = new SqlDataAdapter("SELECT * from table WHERE column=@column AND column2=@column2", conn);
da_services.SelectCommand.Parameters.AddWithValue("@column", textBox1.Text);
da_services.SelectCommand.Parameters.AddWithValue("@column2", somestring);

这篇关于在 SqlDataAdapter 中防止 SQL 注入的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆