从数据库填充 DropDownList

发布于 2024-11-03 00:17:26 字数 564 浏览 0 评论 0 原文

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

I am new to C# and trying to populate a DropDownList based on a database value. I tried connecting to database as shown below - tested with the statement and it says connected. Can I assume this is correct? Am I on the right track? Also, how do I then select value from the table and fill DropDownList with a field?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

◇流星雨 2024-11-10 00:17:26
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................
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................
破晓 2024-11-10 00:17:26

就这么简单:----

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

------------------------------------------------------------------------

It's So Much Simple :----

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

------------------------------------------------------------------------
〆凄凉。 2024-11-10 00:17:26
            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();
            }
            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();
            }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文