【问题标题】:Best way to protect against SQL injection in SqlDataAdapter在 SqlDataAdapter 中防止 SQL 注入的最佳方法
【发布时间】:2013-10-05 08:42:55
【问题描述】:

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

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

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();

感谢您的宝贵时间。

【问题讨论】:

  • 因为没有办法使用参数化查询。 Uh.......

标签: c# sql sql-server winforms


【解决方案1】:

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

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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-09
    • 2011-01-07
    • 1970-01-01
    • 2015-08-08
    • 1970-01-01
    • 2012-12-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多