
C# combobox获取数据库数据的问题
SqlConnectionsqlcon=newSqlConnection();sqlcon.ConnectionString="DataSource=8BAHAEMRYH...
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = "Data Source=8BAHAEMRYHWRLHV;Initial Catalog=DIY;Integrated Security=True";
sqlcon.Open();
string str = "select type from Accessories ";
SqlCommand sqlcom = new SqlCommand(str, sqlcon);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlcom);
da.Fill(ds);
int j = ds.Tables[0].Rows.Count;
for (int i = 0; i < j; i++)
{
comboBox1.Items.Add(ds.Tables [0].Rows[i].ToString ());
}
sqlcon.Close();
为什么我这样写不能获取到数据库里的内容啊? 展开
sqlcon.ConnectionString = "Data Source=8BAHAEMRYHWRLHV;Initial Catalog=DIY;Integrated Security=True";
sqlcon.Open();
string str = "select type from Accessories ";
SqlCommand sqlcom = new SqlCommand(str, sqlcon);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sqlcom);
da.Fill(ds);
int j = ds.Tables[0].Rows.Count;
for (int i = 0; i < j; i++)
{
comboBox1.Items.Add(ds.Tables [0].Rows[i].ToString ());
}
sqlcon.Close();
为什么我这样写不能获取到数据库里的内容啊? 展开
2个回答
展开全部
检查一下,sql是不是能查到值,跟一下代码呗看看ds里面是不是有值,还有,你那个添加值的时候有点问题吧
comboBox1.Items.Add(ds.Tables [0].Rows[i].ToString ());
试试这个:
comboBox1.Items.Add(ds.Tables [0].Rows[i][0].ToString ());
吐槽一下,不就是单个列的值么,有必要用到DataAdapter么……
comboBox1.Items.Add(ds.Tables [0].Rows[i].ToString ());
试试这个:
comboBox1.Items.Add(ds.Tables [0].Rows[i][0].ToString ());
吐槽一下,不就是单个列的值么,有必要用到DataAdapter么……
追问
哦哦,谢谢啦~!我基础不好,只会用DataAdapter...

2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询