为什么搜索代码不起作用.... [英] Why search code not working....

查看:72
本文介绍了为什么搜索代码不起作用....的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么搜索代码不起作用....



Why search code not working....

private void cmdsearch_Click(object sender, EventArgs e)
{
    if (comboBox1.SelectedItem="ID")
    {
        SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where id='" + textBox3.Text + "'", conn);
        DataTable tbl = new DataTable();
        adp.Fill(tbl);
        dataGridView1.DataSource = tbl;
    }
    else if (comboBox1.SelectedItem="Name")
    {
        SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where name='" + textBox3.Text + "'", conn);
        DataTable tbl = new DataTable();
        adp.Fill(tbl);
        dataGridView1.DataSource = tbl;
    }
}

推荐答案

1.您应该调试应用程序,检查SqlDataAdapter中使用的SQL和复制SQL命令并在SQL Server中测试它们,然后您将看到错误。

2.从您的代码中我怀疑在您使用ID的第一个SQL中,ID是一个整数。如果是这种情况,你应该纠正这个SQL,如下所示:

1.You should debug you application, inspect both SQL used in the SqlDataAdapter and copy the SQL commands and test them in you SQL server then you will see the error.
2.From your code I suspect that in you first SQL, where ID is used, the ID is an integer. If this is the case you should correct this SQL like in below:
SqlDataAdapter adp = new SqlDataAdapter("select * from test1 where id=" + textBox3.Text, conn);



更好的方法是在代码中进行验证,并尝试在将用户输入发送到SQL之前将值转换为int。

3.在第二个SQL的情况下,您应该进行验证并尝试防止SQL注入攻击。这是一个 MSDN链接。 [ ^ ]


这篇关于为什么搜索代码不起作用....的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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