用访问数据库中的忠诚点填充文本框;
本文关键字:填充 文本 忠诚 访问 数据库 | 更新日期: 2023-09-27 17:51:11
我试图填充一个文本框的信息,通过使用customerID这是通过一个文本框输入搜索这是我使用下面的代码
private void txtCustomerID_TextChanged(object sender, EventArgs e)
{
string strCon = Properties.Settings.Default.PID2dbConnectionString;
OleDbConnection conn = new OleDbConnection(strCon);
String sqlPoints = "SELECT points FROM customer WHERE [customerID]=" + txtCustomerID.Text;
txtPoints.Text = sqlPoints;
}
但是文本框"txtPoints"只输出sqlpoints的文本而不是数据库中的信息?我不太确定我哪里做错了。
任何帮助都是感激的,提前感谢。
您没有在数据库上执行SQL语句。相反,你把它分配给txtPoints.Text
。您需要在DB服务器上使用例如OleDbCommand对象来执行它。
你需要做的是像下面这样的事情(注意这是伪代码-我还没有测试过它的运行)
using (OleDbConnection conn = new OleDbConnection(strCon))
{
String sqlPoints = "SELECT points FROM customer WHERE [customerID]=" + txtCustomerID.Text;
// Create a command to use to call the database.
OleDbCommand command = new OleDbCommand(sqlPoints, conn)
// Create a reader containing your results
using(OleDbReader reader = command.ExecuteReader())
{
reader.Read(); // Advance to the first row.
txtPoints.Text = reader[0].ToString(); // Read the contents of the first column
}
}
还要注意我使用using。这将确保在完成数据库连接后正确关闭数据库连接。