我正在编写一个应用程序,用于更新一个新创建的数据库。该应用程序上有一个datagridview,用于显示数据库中的数据。 我的应用程序正在进行一些非常奇怪的事情。SQL更新查询没有更新数据库
这是当我运行的代码,收出关的应用程序,并再次运行应用程序,更新数据库
string updateCommandString = "UPDATE RoomsTable SET [Date Checked][email protected] WHERE ID = @id";
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\spreston\documents\visual studio 2012\Projects\roomChecksProgram\roomChecksProgram\roomsBase.accdb"))
{
using (OleDbCommand updateCommand = new OleDbCommand())
{
updateCommand.Connection = conn;
updateCommand.CommandText = updateCommandString;
updateCommand.CommandType = CommandType.Text;
updateCommand.Parameters.AddWithValue("@checkedDate",
this.dateTimePicker1.Value.ToShortDateString());
updateCommand.Parameters.AddWithValue("@id", row.roomID);
try
{
conn.Open();
updateCommand.ExecuteNonQuery();
conn.Close();
conn.Dispose();
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
现在的代码,改变的是正确地显示在我的datagridview连接到数据库,但是当我看到实际的数据库时,根本没有任何改变。我不知道为什么会发生这种情况。
我的sql更新更新连接到datagrid视图的数据库。 SOW HOW是datagrid视图显示正确的数据,但不显示数据库本身。
编辑: 我以前也没有那种用SQL的经验。
编辑: 交易代码:
string updateCommandString = "UPDATE RoomsTable SET [Date Checked][email protected] WHERE ID = @id";
using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\users\spreston\documents\visual studio 2012\Projects\roomChecksProgram\roomChecksProgram\roomsBase.accdb"))
{
OleDbTransaction transaction = null;
using (OleDbCommand updateCommand = new OleDbCommand())
{
updateCommand.Connection = conn;
updateCommand.Transaction = transaction;
updateCommand.CommandText = updateCommandString;
updateCommand.CommandType = CommandType.Text;
updateCommand.Parameters.AddWithValue("@checkedDate", this.dateTimePicker1.Value.ToShortDateString());
updateCommand.Parameters.AddWithValue("@id", row.roomID);
try
{
conn.Open();
transaction = conn.BeginTransaction();
updateCommand.ExecuteNonQuery();
transaction.Commit();
conn.Close();
conn.Dispose();
}
catch(OleDbException ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
}
我不知道,如果'dateTimePicker1.Value.ToShortDateString()'是必要的。将DateTime转换为字符串是文化特定的。 – nrodic
为什么使用conn.Dispose?当您自动使用“使用”对象时Dispose –
您是否有可能碰到内部异常但它不是OldDbException?如果将捕获扩展到“例外”,你会发现什么? – dougajmcdonald