簡體   English   中英

如何從SQL數據庫獲取數據以存儲在組合框中-C#

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

如何從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'條目。找不到該名稱的條目。請確保該名稱輸入正確。”

希望您的答復謝謝!

使用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數據導入組合框的非常簡單的方法,如下所示:

  • 首先,您有一個創建SQL表,
  • 在C#平台中,放下一個組合框並轉到其屬性,
  • 在屬性菜單中,單擊“數據源”
  • 指定要加載到組合框的數據庫和表,請注意,組合框名稱和表的行應相同。
{ 

    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創建?

將行更改為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";
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM