C# を使用して Window アプリケーションのデータベース アクセス層を実装します。データベース (.accdb) はプロジェクト ファイルにあります。スイッチを介して 1 つのアクセス データベースに接続する 2 つのノートブック (クライアント) になると、DBConcurrency Exception エラーがスローされます。私の目標は、最初に実行された sql のタイムスタンプを確認してから、 sql を実行することです。これを達成するためのガイドラインを教えてください。
以下は私のコードです
protected void btnTransaction_Click(object sender, EventArgs e)
{
string custID = txtID.Text;
string CompName = txtCompany.Text;
string contact = txtContact.Text;
string city = txtCity.Text;
string connString = ConfigurationManager.ConnectionStrings["CustomersDatabase"].ConnectionString;
OleDbConnection connection = new OleDbConnection(connString);
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
OleDbTransaction transaction = connection.BeginTransaction();
command.Transaction = transaction;
try
{
command.CommandText = "INSERT INTO Customers(CustomerID, CompanyName, ContactName, City, Country) VALUES(@CustomerID, @CompanyName, @ContactName, @City, @Country)";
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@CustomerID", custID);
command.Parameters.AddWithValue("@CompanyName", CompName);
command.Parameters.AddWithValue("@ContactName", contact);
command.Parameters.AddWithValue("@City", city);
command.ExecuteNonQuery();
command.CommandText = "UPDATE Customers SET ContactName = @ContactName2 WHERE CustomerID = @CustomerID2";
command.CommandType = CommandType.Text;
command.Parameters.AddWithValue("@CustomerID2", custIDUpdate);
command.Parameters.AddWithValue("@ContactName2", contactUpdate);
command.ExecuteNonQuery();
adapter.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
transaction.Commit();
lblMessage.Text = "Transaction successfully completed";
}
catch (Exception ex)
{
transaction.Rollback();
lblMessage.Text = "Transaction is not completed";
}
finally
{
connection.Close();
}
}