如何从SQL数据库获取数据存储在组合框中 - [英] How to get data from SQL database to store in combo box - C#

查看:126
本文介绍了如何从SQL数据库获取数据存储在组合框中 - 的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从Comp table获取company_name的值并将其存储在comboBox上?

How can i get the value of company_name from Comp table and store it on a comboBox?

这是我从数据库获取值并存储它的初始代码在组合框上:

here is my initial code on getting the values from Database and store it on a combobox:

string Sql = "select company_name from JO.dbo.Comp";
SqlConnection conn = new SqlConnection(connString);
SqlCommand cmd = new SqlCommand(Sql, conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
  comboBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString());

它指出 da.fill(ds)并表示无法在数据库的sysdatabases中找到条目'select company_name from JO'。没有找到与该名称匹配的条目请确保输入的名称正确。

it point out to da.fill(ds) and says "Could not locate entry in sysdatabases for database 'select company_name from JO'. No entry found with that name. Make sure that the name is entered correctly."

希望您的回复感谢!

推荐答案

使用datareader更简单\

Use datareader it is much simpler \

   string Sql = "select company_name from JO.dbo.Comp";
   SqlConnection conn = new SqlConnection(connString);
   conn.Open();
   SqlCommand cmd = new SqlCommand(Sql, conn);
   SqlDataReader DR = cmd.ExecuteReader();

            while (DR.Read())
            {
                combobox1.Items.Add(DR[0]);

            }

这篇关于如何从SQL数据库获取数据存储在组合框中 - 的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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