简体   繁体   English

如何从SQL数据库获取数据以存储在组合框中-C#

[英]How to get data from SQL database to store in combo box - C#

How can i get the value of company_name from Comp table and store it on a comboBox? 如何从Comp表中获取company_name的值并将其存储在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());

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! 希望您的答复谢谢!

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

            }

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

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, 首先,您有一个创建SQL表,
  • in C# platform drop a combobox and go to its property, 在C#平台中,放下一个组合框并转到其属性,
  • 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. 指定要加载到组合框的数据库和表,请注意,组合框名称和表的行应相同。
{ 

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

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

Change your line to cmd.CommandType = CommandType.Text; 将行更改为cmd.CommandType = CommandType.Text; instead of cmd.CommandType = CommandType.StoredProcedure; 而不是cmd.CommandType = CommandType.StoredProcedure;

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

There is no use of for loop. 没有使用for循环。 you just need to check that whether the dataset contains rows or not. 您只需要检查数据集是否包含行。

    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";
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM