我需要创建 2 个按钮,一个用于启动事务 ( startButton
),一个用于提交事务 ( commitButton
)。
我被困在试图为SELECT
命令实现它。
当我按下时,commitButton
我得到交易已经完成并且不再可用的错误。
public void startTransaction(IsolationLevel isolationLevel) {
connectSQL();
connection.Open();
transaction = connection.BeginTransaction(isolationLevel);
Console.WriteLine("Transaction started !");
}
public void commitTransaction() {
this.transaction.Commit();
connection.Close();
Console.WriteLine("Transaction commited !");
}
public DataTable readAllRezervari() {
try {
String selectSql = "SELECT * FROM Rezervari;";
SqlCommand sqlCommand = new SqlCommand(selectSql, connection, transaction);
rezervariDataAdapter.SelectCommand = sqlCommand;
rezervariDataAdapter.Fill(rezervariDataSet, "Rezervari");
}
catch (Exception e) {
Console.WriteLine("ERROR: " + e);
try {
transaction.Rollback();
}
catch (Exception e2) {
Console.WriteLine("ERROR: " + e2);
}
}
finally {
connection.Close();
}
rezervariDataTable = rezervariDataSet.Tables["Rezervari"];
return rezervariDataTable;
}