-2

我想以列表形式(如组合框)从数据库中获取文本框中列的所有数据。这是我的代码。

    CON.Open();
    SqlCommand CMD = new SqlCommand("SELECT CUST_NAME FROM CUSTOMER WHERE CUST_ID=CUST_NAME", CON);
    SqlDataReader DR = CMD.ExecuteReader();

    while (DR.Read())
    {
        CUST_NAME.Text = DR["CUST_NAME"].ToString();
    }
    CON.Close();

所以请帮助我

4

3 回答 3

1

需要一点改变。

CON.Open();
SqlCommand CMD = new SqlCommand("SELECT CUST_NAME FROM CUSTOMER WHERE CUST_ID=CUST_NAME", CON);
SqlDataReader DR = CMD.ExecuteReader();

while (DR.Read())
{
    CUST_NAME.Text += DR["CUST_NAME"].ToString() + "\r\n";
}
CON.Close();

谢谢

于 2013-06-25T11:38:19.370 回答
1

你应该让你的文本框多行并重新调整它的大小

CUST_NAME.Multiline = true;

然后将您的代码更改为

    CON.Open();
    SqlCommand CMD = new SqlCommand("SELECT CUST_NAME FROM CUSTOMER WHERE CUST_ID=CUST_NAME", CON);
    SqlDataReader DR = CMD.ExecuteReader();

    while (DR.Read())
    {
        CUST_NAME.Text = DR["CUST_NAME"].ToString() + "\r\n";
    }
    CON.Close();
于 2013-06-25T07:25:21.383 回答
0

从 a 绑定到组合框DataReader如下所示:

CON.Open();
SqlCommand CMD = new SqlCommand("SELECT CUST_NAME FROM CUSTOMER WHERE CUST_ID=CUST_NAME", CON);
SqlDataReader DR = CMD.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(DR);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "CUST_NAME";
comboBox1.ValueMember = "CUST_ID";
CON.Close();
于 2013-06-25T07:16:45.157 回答