如何从 SQL 数据库中获取数据以存储在组合框中 - C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17960283/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get data from SQL database to store in combo box - C#
提问by user2632704
How can i get the value of company_name from Comp table and store it on a comboBox?
如何从 Comp 表中获取 company_name 的值并将其存储在组合框上?
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());
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."
它指出da.fill(ds)
并说"Could not locate entry in sysdatabases for database 'select company_name from JO'
。未找到具有该名称的条目。确保正确输入名称。”
hope for your reply thanks!
希望您的回复谢谢!
回答by JoeCo
If you set up your connection string to be something of this sort:
如果您将连接字符串设置为此类:
string SqlConnectionString = "Data Source=[SERVER];Initial Catalog=[DATABASE];"
Then using that set up, you can set your string 'Sql' as:
然后使用该设置,您可以将字符串 'Sql' 设置为:
string Sql = "select company_name from dbo.Comp";
This could be a possible set up you could use to read out the values.
这可能是您可以用来读出值的可能设置。
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();
}
回答by Cubicle.Jockey
Have you ever tried Entity Frameworkfor database access and dto creation?
您是否尝试过使用实体框架进行数据库访问和 dto 创建?
回答by odlan yer
Use datareader it is much simpler \
使用 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]);
}
回答by Prash
Change your line to cmd.CommandType = CommandType.Text;
instead of cmd.CommandType = CommandType.StoredProcedure;
将您的线路更改为cmd.CommandType = CommandType.Text;
而不是cmd.CommandType = CommandType.StoredProcedure;
回答by Janki
Try this
尝试这个
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"));
}
回答by bhoopendra.sahoo
There is no use of for loop. you just need to check that whether the dataset contains rows or not.
没有使用 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"));
}
回答by Safiullah
I would like to introduce you a very simple way to SQL data into a combobox as:
我想向您介绍一种非常简单的将 SQL 数据放入组合框的方法,如下所示:
- first you have a create a SQL table,
- in C# platform drop a combobox and go to its property,
- in the property menu click on "DataSource"
- specify the database and table to load into combobox, Note, the combobox name and table's row should be the same.
- 首先你有一个创建 SQL 表,
- 在 C# 平台中放置一个组合框并转到其属性,
- 在属性菜单中单击“数据源”
- 指定要加载到组合框的数据库和表,注意,组合框名称和表的行应该相同。
回答by VENKATESH
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());
回答by Nitin...
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";
}
回答by saurabh kumar
{
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();
}
}
}