-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();
}
}