返回首页

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

回答

评论会员:游客 时间:2012/02/07
Mahendra.p25:尝试codepreCon=newSqlConnection(spanclass="code-string""/spanspanclass="code-string"yOURcONNECTIONsTRING"/span);Con.spanclass="code-keyword"Open/span();Cmd=newSqlCommand(spanclass="code-string""/spanspanclass="code-string"Select*fromProducts"/span);dt=newSqlDataAdapter(spanclass="code-string""/spanspanclass="code-string"SelectProductId,ProductName+ProductPriceasNamefromproducts"/span,Con);DataSetds=newDataSet();dt.Fill(ds);DropDownList1.DataSource=ds;DropDownList1.DataTextField=spanclass="code-keyword"Convert/span.ToString(ds.Tables[spanclass="code-digit"0/span].Rows[spanclass="code-digit"0/span][spanclass="code-string""/spanspanclass="code-string"Name"/span]);DropDownList1.DataValueField=spanclass="code-keyword"Convert/span.ToString(ds.Tables[spanclass="code-digit"0/span].Rows[spanclass="code-digit"0/span][spanclass="code-string""/spanspanclass="code-string"ProductId"/span]);DropDownList1.DataBind();Con.spanclass="code-keyword"Close/span();/pre/code
anvas kuttan:
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();
评论会员:游客 时间:2012/02/07
普拉温・帕蒂尔,孟买:看到imgsrc=http://www.orcode.com/img/ico/smiley_wink.gif]
尝试

SqlCommand cmd = new SqlCommand("yourquery", yourConnection);

           SqlDataReader dr = cmd.ExecuteReader();

 

while (dr.Read())

            {

                comboBox1.Items.Add(dr[Index].ToString());

            }

            dr.Close();


{体C3}
采取一看详情]
评论会员:RaviRanjankr 时间:2012/02/07
我的SQL Server表: -
USER_ID名称
1王子
2拉克什
拉胡尔3

现在我的表名是学生

-----
在C#Windows应用: -
申报页面加载事件
{的C4}
在上述溶液中,ComboBox1的是我的ComboBox中
在你的另一种方法使用到ComboBox和选择DataSource属性和添加项目数据源

现在,您选择您要显示的DisplayMember属性。也ValueMamber财产的使用显示值。

第三法

{C5的}
评论会员:拉克什从巴特那 时间:2012/02/07
公共无效fill_groups()
{

 0; 尝试
 0; {
SqlConnection的CON =新的SqlConnection(ConnectionString中)
&# 160; 的DataSet DS =新的DataSet();
SqlDataAdapter的广告=新的SqlDataAdapter("选择组名,从groupNames",CON);
&# 160;ad.Fill(DS)
&# 160;cmbgroup.Items.Add("---选择---");
 0; (我= 0;我LT; ds.Tables [0] Rows.Count;我)
 60;{
&# 160;cmbgroup.Items.Add(ds.Tables [0]行[I] [0]的ToString())

}
 60; }
& #160; 赶上(例外EX)
 0;{
MessageBox.Show(ex.Message);
}
&# 160;
}
评论会员:anilkumar143 时间:2012/02/07
SqlCommand的CMD =新的SqlCommand(的"yourquery",yourConnection)
SqlDataReader的博士= cmd.ExecuteReader();

而(dr.Read())
&# 160; {
& #160; comboBox1.Items.Add(,dr.Getstring(0)的ToString())
}
 0; dr.Close();
评论会员:游客 时间:2012/02/07
Rethinaprasath:嗨,{A2}]
| srinivas vadepally:我会建议您重命名为svchost.exe的
这可疑的{S0的}