如何在单个文本框中存储所有员工姓名

时间:2016-03-11 10:15:46

标签: c# asp.net

在处理此代码时,我在文本框中获取单个EmployeeName,而不是我希望单个文本框中的数据集中的所有员工姓名。谢谢您的帮助。

public void loadnow()
{

    cn = new SqlConnection(str);
    cn.Open();
    cmd = new SqlCommand("select EmployeeName=[EmployeeFirstName]+' '+[EmployeeLastName] from EmployeeMaster ", cn);
    cmd.ExecuteNonQuery();
    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {

        foreach (DataRow myRow in ds.Tables[0].Rows)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(ds.Tables[0].Rows[0]["EmployeeName"].ToString());
            sb.Append(Environment.NewLine);
            TextBox1.Text = sb.ToString();

        }

        cn.Close();
    }
}

2 个答案:

答案 0 :(得分:0)

尝试:

 TextBox1.Text += sb.ToString();

答案 1 :(得分:0)

试试这个

public void loadnow()
{
    cn = new SqlConnection(str);
    cn.Open();
    cmd = new SqlCommand("select EmployeeName = [EmployeeFirstName] + ' ' + [EmployeeLastName] from EmployeeMaster", cn);
    cmd.ExecuteNonQuery();
    da = new SqlDataAdapter(cmd);
    ds = new DataSet();
    da.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {
        StringBuilder sb = new StringBuilder();

        foreach (DataRow myRow in ds.Tables[0].Rows)
        {
            sb.Append(myRow["EmployeeName"].ToString());
            sb.Append(Environment.NewLine);
        }

        TextBox1.Text = sb.ToString();

        cn.Close();
    }
}