我正在尝试从 MS-Access 2010 数据库表中填充文本框。该表有 2 个字段,Night 和 Seats。我想将 Seats 字段中的当前值放入文本框中,假设在 Night 字段中选择了日期的当前值。我现在的代码是:
//connect to the database to get how many seats are available
System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Duncan\Documents\Visual Studio 2012\Projects\TheatreUI\TheatreUI\bin\Debug\PlayHouse.accdb";
OleDbCommand cmd = con.CreateCommand();
//open the connection
con.Open();
// read from the Nights Table in the database
cmd.CommandText = "SELECT Seats FROM Nights WHERE Night = '" + System.DateTime.Now.ToShortDateString() + "';";
MessageBox.Show(System.DateTime.Now.ToShortDateString());
OleDbDataReader reader = cmd.ExecuteReader();
MessageBox.Show(reader["Seats"].ToString());
SeatsText.Text = reader["Seats"].ToString();
//close the connection
con.Close();
此代码不仅没有正确(或根本没有)填充文本字段,而且似乎从数据库中完全删除了今天日期的记录。第一个消息框显示正确的日期,但第二个显示为空白。如何修复此代码,使其填充文本框并且不删除数据库中的条目?