c#下拉框如何读取数据库
OleDbConnectioncon=newOleDbConnection(conn);//ECT连接con.Open();//连接打开OleDbCommandcmd=n...
OleDbConnection con = new OleDbConnection(conn);//ECT连接 con.Open();//连接打开 OleDbCommand cmd = new OleDbCommand(); cmd.Connection = con; cmd.CommandText = "select * from 表名"; cmd.CommandType = CommandType.Text; OleDbDataReader sdr = cmd.ExecuteReader()while (sdr.Read()) { listBox1.Items.Add(sdr["字段名"].ToString()); }这段代码已经成功读取字段中所有行。现在我需要的是comboBox第一个值读取数据库第一行,第二个值读取第二个行。请问改怎么修改,老师们,谢谢!
读出来显示在列表框。 展开
读出来显示在列表框。 展开
1个回答
展开全部
两种方法:
1. 绑定数据源:
comboBox1.DataSource=ds.Tables[0];comboBox1.DisplayMenber="<a href="https://www.baidu.com/s?wd=%E5%AD%97%E6%AE%B5%E5%90%8D&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLPH-hmvckuHI9mHu-PADs0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En10LPW01PHD4" target="_blank" class="baidu-highlight">字段名</a>";comboBox1.Text="<a href="https://www.baidu.com/s?wd=%E5%88%9D%E5%A7%8B%E5%8C%96&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLPH-hmvckuHI9mHu-PADs0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En10LPW01PHD4" target="_blank" class="baidu-highlight">初始化</a>值";
2. 添加删除记录方法:
//添加for(int i=0;i<list.Count;i++){ comboBox1.Items.Add(list[i]);} //删除comboBox1.Items.RemoveAt(Index);
1. 绑定数据源:
comboBox1.DataSource=ds.Tables[0];comboBox1.DisplayMenber="<a href="https://www.baidu.com/s?wd=%E5%AD%97%E6%AE%B5%E5%90%8D&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLPH-hmvckuHI9mHu-PADs0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En10LPW01PHD4" target="_blank" class="baidu-highlight">字段名</a>";comboBox1.Text="<a href="https://www.baidu.com/s?wd=%E5%88%9D%E5%A7%8B%E5%8C%96&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1YLPH-hmvckuHI9mHu-PADs0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En10LPW01PHD4" target="_blank" class="baidu-highlight">初始化</a>值";
2. 添加删除记录方法:
//添加for(int i=0;i<list.Count;i++){ comboBox1.Items.Add(list[i]);} //删除comboBox1.Items.RemoveAt(Index);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |