c#为sql列制作组合框过滤器 [英] c# making combobox filter for sql columns

查看:57
本文介绍了c#为sql列制作组合框过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我正在尝试使用2个组合框,它们都从不同的sql表中读取数据。我需要这样做;

-当我在combobox1上选择一些东西时(它只读取列上的名字)

-当我想在组合框2上选择一些东西时(它只读取不同表格中列上的Surnames),我想看到以combobox1上的名字开头的名字。







这是我的代码;



Hey,
I'm trying to use 2 comboboxes that all reads data from different sql table. I need to do that;
-When i select something on combobox1 (it only reads names on a Column)
-when i want to select something on combobox 2(it only reads Surnames on a Column in different table), i want to see names that starts with name on combobox1.



here is my code;

SqlConnection con1 = new SqlConnection("Data Source=.;Initial Catalog=AY_DB;Integrated Security=True");
con1.Open();
SqlCommand cmd2 = new SqlCommand("select Name from CLASS", con1);
SqlCommand cmd3 = new SqlCommand("select Surname from CLASS2 where (Name = '" + comboBox1.Text + "')", con1);
SqlDataReader dr1,dr2;

try
{
    dr1 = cmd2.ExecuteReader();

    while (dr1.Read())
    {

        comboBox1.Items.Add(dr1["Name"]);

    }
    con1.Close();

}
catch (Exception es)
{
    MessageBox.Show(es.Message, "FAIL");
}
try
{
    con1.Open();
    dr5 = cmd3.ExecuteReader();
    while(dr2.Read())
    {
        comboBox2.Items.Add(dr5["Surname"]);
    }
    con1.Close();
}

    catch(Exception eg)
{
        MessageBox.Show(eg.Message,"FAIL");

    }







CLASS表中的名称为as与CLASS2表中的相同,它们的表名也相同(两个列名都是name)。我可以在combobox1上看到名字。没关系,但是在选择了combobox1上的项目之后,我无法在combobox2上看到一个项目。它似乎是空的。

我怎样才能看到以combobox1上的项目开头的姓氏?




Names in CLASS table is as same as in CLASS2 table and their table names are same too(both column names are "name"). I can see names on combobox1. It's ok but, after selecting an item on combobox1, i couldn't see an item on combobox2. It seems empty.
How can i see surnames which starts with item on combobox1?

推荐答案

you should subscribe to combobox event SelectedValueChanged
And if the Event fired , do you select with cmd3 and refill the combobox2with new data

See for example HERE
<a href="http://msdn.microsoft.com/de-de/library/system.windows.forms.listcontrol.selectedvaluechanged(v=vs.110).aspx">http://msdn.microsoft.com/de-de/library/system.windows.forms.listcontrol.selectedvaluechanged(v=vs.110).aspx</a>


正如已经指出的那样t,你需要处理 SelectedIndexChanged [ ^ ]事件在第一个组合中,在该事件中,您填写第二个组合。所以基本上你将你的代码分成两个不同的地方。



另外重要的是要注意,因为你直接将值连接到你的SQL语句,你很容易受到不同类型的攻击SQL注入,数据类型转换问题等问题。正确的方法是使用 SqlParameter中所述的参数。 [ ^ ]
As already pointed out, you need to handle the SelectedIndexChanged[^] event in the first combo and in that event you fill the second combo. So basically you split your code to two different places.

Also it's important to notice that because you concatenate values directly to your SQL statement, you're vulnerable to different kinds of problems such as SQL injection, data type conversion problems and so on. The proper way is to use parameters as explained in SqlParameter[^]


这篇关于c#为sql列制作组合框过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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