如何从SQL数据库中获取数据存储在combobox中 – C#

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

这是我从数据库获取值并将其存储在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)并说"Could not locate entry in sysdatabases for database 'select company_name from JO' 。找不到具有该名称的条目。确保正确输入了名称。”

希望你的回复谢谢!

使用datareader它简单得多

  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]); } 

如果您将连接字符串设置为此类型:

 string SqlConnectionString = "Data Source=[SERVER];Initial Catalog=[DATABASE];" 

然后使用该设置,您可以将字符串’Sql’设置为:

 string Sql = "select company_name from dbo.Comp"; 

这可能是您可以用来读出值的可能设置。

 using (SqlConnection saConn = new SqlConnection(this.ConnectionString)) { saConn.Open(); string query = "select DBName from dbo.Company"; SqlCommand cmd = new SqlCommand(query, saConn); using (SqlDataReader saReader = cmd.ExecuteReader()) { while (saReader.Read()) { string name = saReader.GetString(0); combobox1.Add(name); } } saConn.Close(); } 

我想向您介绍一种将SQL数据转换为combobox的简单方法:

  • 首先你有一个创建SQL表,
  • 在C#平台下放一个combobox并转到它的属性,
  • 在属性菜单中单击“DataSource”
  • 指定要加载到combobox中的数据库和表,注意,combobox名称和表的行应该相同。

您是否曾尝试使用Entity Framework进行数据库访问和dto创建?

将您的行更改为cmd.CommandType = CommandType.Text; 而不是cmd.CommandType = CommandType.StoredProcedure;

试试这个

 string Sql = "select Company_ID,company_name from JO.dbo.Comp"; SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(Sql, conn); cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { comboBox1.DataSource = ds.Tables[0]; comboBox1.DataTextField = "company_name"; comboBox1.DataValueField = "Company_ID"; comboBox1.DataBind(); comboBox1.Items.Insert(0, new ListItem("--Select--", "0")); } 

没有使用for循环。 您只需要检查数据集是否包含行。

  string Sql = "select Company_ID,company_name from JO.dbo.Comp"; SqlConnection conn = new SqlConnection(connString); SqlCommand cmd = new SqlCommand(Sql, conn); cmd.CommandType = CommandType.Text; SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { comboBox1.DataSource = ds.Tables[0]; comboBox1.DataTextField = "company_name"; comboBox1.DataValueField = "Company_ID"; comboBox1.DataBind(); comboBox1.Items.Insert(0, new ListItem("Select", "0")); } 
 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()); 
 public System.Data.DataTable EmployeeViewAll() { DataTable dtbl = new DataTable(); try { // Here it shuld be your database Connection String string connectionString = "Server = .; database = HKS; Integrated Security = true"; using (SqlConnection sqlCon = new System.Data.SqlClient.SqlConnection(connectionString)) { SqlDataAdapter SqlDa = new SqlDataAdapter("employeeViewAll", sqlCon); SqlDa.SelectCommand.CommandType = CommandType.StoredProcedure; SqlDa.Fill(dtbl); } return dtbl; } catch (Exception) { throw; } } public void ComboFill() { DataTable dt = new DataTable(); eSP SP = new eSP(); d = SP.EmployeeViewAll(); comboBox1.DataSource = dt; comboBox1.DisplayMember = "department"; comboBox1.ValueMember = "empName"; } 

{

SqlConnection con = new SqlConnection(“Data Source = Server_Name; Initial

Catalog = Database_Name; integrated security = true“);

  SqlCommand cmd; SqlDataReader dr; private void CashMemoForm_Load(object sender, EventArgs e) { con.Open(); cmd = new SqlCommand("Select Column_Name From Table_Name", con); dr = cmd.ExecuteReader(); while (dr.Read()) { comboBox1.Items.Add(dr[0]).ToString(); } 

}

}