作成したデータベースを更新するアプリケーションを作成しています。アプリケーションには、データベースからのデータを表示する datagridview があります。アプリで非常に奇妙なことが起こっています。
データベースを更新するコードは次のとおりです
string updateCommandString = "UPDATE RoomsTable SET [Date Checked]=@checkedDate 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());
}
}
}
そのコードを実行し、アプリを閉じてアプリを再度実行すると、データベースに接続されているデータグリッドビューに変更が正しく表示されますが、実際のデータベースを見ると、何も変更されていません。なぜこれが起こっているのかわかりません。
私のSQL更新は、データグリッドビューに接続されているデータベースを更新します。Sow HOW は正しいデータを表示するデータグリッド ビューですが、データベース自体は表示しません。
編集:私はこれまでSQLの経験がありませんでした。
編集: トランザクション コード:
string updateCommandString = "UPDATE RoomsTable SET [Date Checked]=@checkedDate 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());
}
}
}