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

时间:2013-07-31 01:45:47

标签: c# sql sql-server combobox

如何从Comp表中获取company_name的值并将其存储在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'。找不到具有该名称的条目。确保正确输入名称。“

希望你的回复谢谢!

10 个答案:

答案 0 :(得分:11)

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

            }

答案 1 :(得分:2)

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

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

{ 

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

答案 4 :(得分:0)

你有没有尝试Entity Framework进行数据库访问和创建dto?

答案 5 :(得分:0)

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

答案 6 :(得分:0)

试试这个

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

答案 7 :(得分: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"));
    }

答案 8 :(得分: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());

答案 9 :(得分:0)

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