我想问你如何使用C#和SQL Server填充组合框 [英] I want to ask you how to fill combo box using c# and in sql server

查看:112
本文介绍了我想问你如何使用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();


请参见 ^ ]


这篇关于我想问你如何使用C#和SQL Server填充组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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