如何使用组合框搜索数据库中的数据 [英] how to search data in database using combobox

查看:110
本文介绍了如何使用组合框搜索数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

在我的表格中,我有一个工具条,我有一个组合框,一个文本框和一个按钮。


$ b $在这里,操作是当我在文本框中键入文本并单击按钮时搜索操作进行得很顺利,与文本相关的数据出现在数据网格视图中......但组合框的主要目的是...... br />
i在数据库中有很多列..比如cutomercode,customername,cutomeraddress,phone,email,fax,....



当我输入客户名在文本框中显示客户的详细信息...但我想要的是当我在组合框中选择客户代码并在文本框中键入代码并单击搜索按钮时,该代码的客户的详细信息应显示在数据网格视图中...就像那样当我在组合框中选择手机时,客户关于电话的详细信息应该出现....

我为搜索框写的代码是....



hi guys,
in my form, i had a toolstrip in that i had a combobox, a textbox and a button.

here, the operation is when i type a text in textbox and click on button the search operation is going well and the data related to text is appearing in datagrid view... but the main purpose of the combobox is....
i have many columns in database.. like cutomercode, customername, cutomeraddress, phone, email, fax, ....

when i type customername in textbox the details of a customer is appearing... but i want is when i select customer code in combobox and type code in textbox and click on searchbutton the deatils of customer of that code should display in datagrid view... like that when ever i select phone in combobox, the details of customer regarding the phonenumber should appear....
the code i written for search box is....

private void cut_searchbtn_Click(object sender, EventArgs e)
        {

 SqlCommand cmd = new SqlCommand("select * from invoice where customer_name=@cut_searchtb");
                cmd.Connection = con;
                try
                {
                SqlParameter search = new SqlParameter();
                search.ParameterName = "@cut_searchtb";
                search.Value = cut_searchtb.Text;
                cmd.Parameters.Add(search);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                cut_grid.DataSource = dt;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }





我应该编写什么代码来选择组合框中的特定菜单并在搜索框中输入特定名称。 ..

请帮我这个编码....



i为这个组合框写了一个代码......但它是不工作...





and what code should i write for selecting the specific menu in combo box and type specific name in searchbox....
please help me in this coding....

i have written a code for that combo box... but it was not working...

Cut_searchcb.Text = "Customer_Name";

            {
                SqlCommand cmd = new SqlCommand("select * from invoice where customer_name=@cut_searchtb");
                cmd.Connection = con;
                SqlParameter search = new SqlParameter();
                search.ParameterName = "@cut_searchtb";
                search.Value = cut_searchtb.Text;
                cmd.Parameters.Add(search);
                SqlDataReader sdr = cmd.ExecuteReader();
                DataTable dat = new DataTable();
                dat.Load(sdr);
                cut_grid.DataSource = dat;
                con.Close();
            }
            cut_searchtb.Text = "Phone1";
            {
                SqlCommand cmd = new SqlCommand("select * from invoice where phone1=@cut_searchtb");
                cmd.Connection = con;
                SqlParameter search = new SqlParameter();
                search.ParameterName = "@cut_searchtb";
                search.Value = cut_searchtb.Text;
                cmd.Parameters.Add(search);
                SqlDataReader sr = cmd.ExecuteReader();
                DataTable da = new DataTable();
                da.Load(sr);
                cut_grid.DataSource = da;
                con.Close();
            }

推荐答案

按以下步骤操作,

do as below,
if(Cut_searchcb.Text == "Customer_Name")
{
    SqlCommand cmd = new SqlCommand("select * from invoice where customer_name=@cut_searchtb");
    cmd.Connection = con;
    SqlParameter search = new SqlParameter();
    search.ParameterName = "@cut_searchtb";
    search.Value = cut_searchtb.Text;
    cmd.Parameters.Add(search);
    SqlDataReader sdr = cmd.ExecuteReader();
    DataTable dat = new DataTable();
    dat.Load(sdr);
    cut_grid.DataSource = dat;
    con.Close();
}
if(Cut_searchcb.Text == "Phone1")
{
    SqlCommand cmd = new SqlCommand("select * from invoice where phone1=@cut_searchtb");
    cmd.Connection = con;
    SqlParameter search = new SqlParameter();
    search.ParameterName = "@cut_searchtb";
    search.Value = cut_searchtb.Text;
    cmd.Parameters.Add(search);
    SqlDataReader sr = cmd.ExecuteReader();
    DataTable da = new DataTable();
    da.Load(sr);
    cut_grid.DataSource = da;
    con.Close();
}


这篇关于如何使用组合框搜索数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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