如何从数据库中获取多个数据以在C#windows应用程序中进行标记

时间:2016-07-01 12:04:14

标签: c#

private void button1_Click(object sender, EventArgs e)
{
    con.Open();
    SqlDataAdapter da = new SqlDataAdapter(
        "select  CustomerName,CompanyName,ContactNo from tbl_LeadFollowUp where WorkType='internet shopping'", con);
    DataSet ds = new DataSet();
    DataTable dt = new DataTable();
    da.Fill(ds);
    da.Fill(dt);

    foreach (DataRow row in dt.Rows)
    {
        textBox1.Text = (row["CompanyName"].ToString());
        textBox2.Text = (row["CustomerName"].ToString());
        label2.Text = (row["ContactNo"].ToString());
     }
     con.close();
}

1 个答案:

答案 0 :(得分:1)

您必须使用DataGridview将多个数据从数据库绑定到UI。

您无法将多个数据迭代为单个标签。它会被覆盖而不是追加

private void button1_Click(object sender, EventArgs e)
{
  con.Open();
  SqlDataAdapter da = new SqlDataAdapter("select  CustomerName,CompanyName,ContactNo from tbl_LeadFollowUp where WorkType='internet shopping'", con);
  DataSet ds = new DataSet();
  DataTable dt = new DataTable();
  da.Fill(ds);
  da.Fill(dt);
  dataGridView1.DataSource = da;
  dataGridView1.DataMember = "Lead";
  con.close();
}