数据库中文本框中的数据
本文关键字:数据 中文 文本 数据库 | 更新日期: 2023-09-27 18:19:43
我想以类似列表的组合框的形式从数据库中获取文本框中一列的所有数据。这是我的密码。
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();
所以请帮我
您应该将文本框设置为多行,并重新调整其大小
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();
需要一个小的更改。
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();
感谢
从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();