【发布时间】:2011-06-25 03:08:46
【问题描述】:
我在 MS Access 中有一个表,其中包含:(FoodID、FoodName、Price)。
在 C# 中,我有三个文本框(txtId、txtName、txtPrice)和一个按钮(btnSearch)。 我的问题是,在 C# 中,我只需在 (txtId) 中键入 FoodID,然后单击按钮 Search 它会在 txtName 和 txtPrice 中显示 FoodName 和 Price(来自表访问)。我从你那里得到了源代码,但它在 (OleDbDataReader dr = cmd.ExecuteReader();) 上出错,它的消息是“标准表达式中的数据类型不匹配”。
请帮我解决这个问题。这是我给你的全部源代码。
System.Data.OleDb.OleDbConnection conn = new OleDbConnection();
conn.ConnectionString = "your connection string";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "select FoodName, Price from tablename where FoodID = '" + txtId + "' ";
conn.Open();
OleDbDataReader dr = cmd.ExecuteReader();//error this line!
while(dr.Read())
{
txtName.Text = dr["FoodName"].ToString();
txtPrice.Text = dr["Price"].ToString();
}
dr.Close();
conn.Close();
【问题讨论】:
标签: c# .net database ms-access ado.net