3

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

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

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'。未找到具有该名称的条目。确保名称输入正确。”

希望得到您的回复谢谢!

4

10 回答 10

13

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

            }
于 2013-07-31T04:46:15.940 回答
2

我想向您介绍一种将 SQL 数据放入组合框的非常简单的方法:

  • 首先你有一个创建一个 SQL 表,
  • 在 C# 平台中删除一个组合框并转到它的属性,
  • 在属性菜单中单击“数据源”
  • 指定要加载到组合框的数据库和表,注意,组合框名称和表的行应该相同。
于 2014-03-07T11:20:05.913 回答
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();
}
于 2013-07-31T02:42:19.110 回答
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();
        }
    }
}
于 2018-09-17T10:48:40.953 回答
0

There is no use of for loop. 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"));
    }
于 2013-07-31T13:37:04.137 回答
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"));
}
于 2013-07-31T05:22:43.450 回答
0

您是否尝试过使用实体框架进行数据库访问和 dto 创建?

于 2013-07-31T04:05:33.760 回答
0

将您的行更改cmd.CommandType = CommandType.Text;cmd.CommandType = CommandType.StoredProcedure;

于 2013-07-31T04:56:01.860 回答
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());
于 2014-12-26T11:22:51.360 回答
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";
  }
于 2015-09-24T18:50:21.670 回答