-1
我将使用C#实现Window应用程序的数据库访问层。数据库(.accdb)位于项目文件中。当涉及通过交换机连接到一个访问数据库的两个笔记本(客户端)时,它会引发DBConcurrency异常错误。我的目标是检查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();
}
}