我想问你如何使用C#和SQL Server填充组合框

问题描述:

我想问你如何使用c#和sql server填充组合框

I want to ask you how to fill combo box using c# and in sql server

尝试一下.

Try this.

Con = new SqlConnection("yOUR cONNECTION sTRING ");
            Con.Open();
             Cmd = new SqlCommand("Select * from Products");
             dt = new SqlDataAdapter("Select ProductId,ProductName+ProductPrice as Name from products", Con);
              DataSet ds = new DataSet();
              dt.Fill(ds);
              DropDownList1.DataSource = ds;
              DropDownList1.DataTextField = Convert.ToString(ds.Tables[0].Rows[0]["Name"]);
              DropDownList1.DataValueField = Convert.ToString(ds.Tables[0].Rows[0]["ProductId"]);
              DropDownList1.DataBind();
            Con.Close();


string connecttionstring = System.Configuration.ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
            SqlConnection cs1 = new SqlConnection(connecttionstring);
            SqlCommand cmd1 = new SqlCommand();
            cs1.Open();
            cmd1.CommandText = "your select Query";
            cmd1.Connection = cs1;
            DataSet dtst = new DataSet();
            SqlDataAdapter dp = new SqlDataAdapter(cmd1);
            dp.Fill(dtst);// for select query
            DropDownList1.DataSource = dtst;
            DropDownList1.DataTextField = "Value you want to show";//this value should be there in your select query
            DropDownList1.DataValueField = "Value you want to select ";//this value should be there in your select query
            DropDownList1.DataBind();


请参见 ^ ]