【问题标题】:Fill DropDownList from database从数据库中填充 DropDownList
【发布时间】:2011-04-23 08:34:00
【问题描述】:

我是 C# 新手,并试图根据数据库值填充 DropDownList。我尝试如下所示连接到数据库 - 使用该语句进行测试,它说已连接。我可以假设这是正确的吗?我在正确的轨道上吗?另外,如何从表中选择值并用字段填充 DropDownList?

protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection connection = new SqlConnection (
    "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:customers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

     try
     {
         connection.Open();
         TextBox1.Text = "connected";
     }
     catch (Exception)
     {
         TextBox1.Text = " not connected";
     }
 }

【问题讨论】:

  • 我现在明白了,很抱歉浪费了任何人的时间 :)
  • 你在正确的轨道上。要将项目添加到组合框,只需 comboBox1.Items.Add(object)
  • @Ken:您可以在这个问题上点击删除吗,因为您不需要答案,因此如果没有相关答案,它将以低质量结束。
  • 由于您将 SQL 硬编码到您的代码中,您不妨使用 SqlDataSource 控件。 msdn.microsoft.com/en-us/library/dz12d98w(v=VS.100).aspx

标签: c# asp.net sql-server ado.net drop-down-menu


【解决方案1】:
protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection connection = new SqlConnection (
    "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:customers.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

     try
     {
          SqlDataReader dReader;
          SqlCommand cmd = new SqlCommand();
          cmd.Connection = connection;
          cmd.CommandType = CommandType.Text;
          cmd.CommandText ="Select distinct [Name] from [Names]" +
          " order by [Name] asc";
         connection.Open();

         dReader = cmd.ExecuteReader();
         if (dReader.HasRows == true)
         {
              while (dReader.Read())
              //Names collection is a combo box.
              namesCollection.Add(dReader["Name"].ToString());

         }
         else
         {
              MessageBox.Show("Data not found");
         }
           dReader.Close()
         TextBox1.Text = "connected";
     }
     catch (Exception)
     {
         TextBox1.Text = " not connected";
     }
 }

Hope that helps................

【讨论】:

    【解决方案2】:

    非常简单:----

    SqlConnection con = new SqlConnection(); 
    DataSet ds = new DataSet(); 
    con.ConnectionString = @"Data Source=TOP7\SQLEXPRESS;Initial Catalog=t1;Persist Security Info=True;User ID=Test;Password=t123";
    string query = "Select * from tbl_User"; 
    SqlCommand cmd = new SqlCommand(query, con); 
    cmd.CommandText = query;
    con.Open(); 
    SqlDataAdapter adpt = new SqlDataAdapter(cmd);
    adpt.Fill(ds);
    comboBox1.Items.Clear();
    comboBox1.DisplayMember = "UserName";
    comboBox1.ValueMember = "UserId";
    comboBox1.DataSource = ds.Tables[0];
    
    ------------------------------------------------------------------------
    

    【讨论】:

      【解决方案3】:
                  using (SqlConnection con = new SqlConnection("Data Source = NIPOON; Initial Catalog = CustomerOrders; Integrated Security = true"))
                  {
                      SqlCommand cmd = new SqlCommand("SELECT Name FROM Customer", con);
                      con.Open();
      
                      dropDownList.DataSource = cmd.ExecuteReader();
                      dropDownList.DataTextField = "Name";
                      dropDownList.DataValueField = "Name";
                      dropDownList.DataBind();
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-27
        • 1970-01-01
        相关资源
        最近更新 更多