我将使用 C# 实现 Window 应用程序的数据库访问层。数据库 (.accdb) 位于项目文件中。当两台笔记本(客户端)通过交换机连接到一个access数据库时,会抛出DBConcurrency Exception Error。我的目标是检查首先执行的 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();
}
}