c#查询数据库给combobox赋值

 在开发一个T6导入工具的过程中遇到了几个combobox绑定数据源的问题:

  • 获取所有数据库名称,通过下拉框选择

	using (System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection(connString)) {
                //初始化,comboBox5绑定帐套名称
                string Sql = "SELECT Name FROM Master..SysDatabases where name like 'ufdata%' ORDER BY Name";
                DataSet Ds = new DataSet();
                SqlDataAdapter Da = new SqlDataAdapter(Sql, Conn);
                Da.Fill(Ds, "databases");
                comboBox5.DataSource = Ds.Tables["databases"];
                comboBox5.DisplayMember = "Name";
                comboBox1.ValueMember = "Name";
            }

  • 绑定combobox事件,同时根据选中的数据库向其他下拉框绑定数据

 	database = (string)comboBox5.SelectedText;

            using (System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection(connString+";database="+database))
            {
                //  初始化,comboBox1绑定客户表
                DataSet Ds = new DataSet();
                string Sql1 = "select cSTCode,cSTName from SaleType";
                SqlDataAdapter Da1 = new SqlDataAdapter(Sql1, Conn);
                Da1.Fill(Ds, "combobox1");
                comboBox1.DataSource = Ds.Tables["combobox1"];
                comboBox1.DisplayMember = "cSTName";
                comboBox1.ValueMember = "cSTCode";

                string Sql2 = "select cCuscode,cCusName from Customer";
                SqlDataAdapter Da2 = new SqlDataAdapter(Sql2, Conn);
                Da2.Fill(Ds, "combobox2");
                comboBox2.DataSource = Ds.Tables["combobox2"];
                comboBox2.DisplayMember = "cCusName";
                comboBox2.ValueMember = "cCuscode";

                string Sql3 = "select cDepCode,cDepName from Department";
                SqlDataAdapter Da3 = new SqlDataAdapter(Sql3, Conn);
                Da3.Fill(Ds, "combobox3");
                comboBox3.DataSource = Ds.Tables["combobox3"];
                comboBox3.DisplayMember = "cDepName";
                comboBox3.ValueMember = "cDepCode";

                string Sql4 = "select cPersonCode,cPersonName from person";
                SqlDataAdapter Da4 = new SqlDataAdapter(Sql4, Conn);
                Da4.Fill(Ds, "combobox4");
                comboBox4.DataSource = Ds.Tables["combobox4"];
                comboBox4.DisplayMember = "cPersonName";
                comboBox4.ValueMember = "cPersonCode";
            }
              







你可能感兴趣的:(dotnet)