[英]Using Transaction in ADO.net SQL
我是ADO的新手,所以我想問一下我是否正確使用了事務。 這里的代碼片段
string SQL1 = "INSERT INTO tbl_cust(cust_id,cust_name) values ('000001','YoungMcD') ";
string SQL2 = "UPDATE tbl_cust SET custname='OldMcDonald' WHERE cust_id='000001'";
string SQL3 = "SELECT * FROM tbl_supplier WHERE supplier_code ='000001'";
// write connstring
string conn = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
// end of connection string
// setting connection
SqlConnection db = new SqlConnection(conn);
SqlTransaction transaction1;
db.Open();
transaction1 = db.BeginTransaction();
try
{
// insert to table
SqlCommand Com1 = new SqlCommand(SQL1, db, transaction1);
Com1.ExecuteNonQuery();
SqlCommand Com2 = new SqlCommand(SQL2, db, transaction1);
Com2.ExecuteNonQuery();
SqlCommand Com3 = new SqlCommand(SQL3, db, transaction1);
Com3.ExecuteNonQuery();
transaction1.Commit();
db.Close();
}
catch
{
transaction1.Rollback();
db.Close();
msg = "error";
goto endret;
}
對於交易,我應該使用
SqlCommand Com1 = new SqlCommand(SQL1, db, transaction1);
代替
SqlCommand Com1 = new SqlCommand(SQL1, db);
因為我已經在try{}
語句之前聲明開始交易
編輯:
我明白了,First語法適用,但是如何有效使用ADO? 我發現這種方式太簡單了。
我發現自己繼續這樣做是為了插入參數,例如:
string SQL1 = "INSERT INTO tbl_cust(cust_id,cust_name) values ('" + param1 +"','"+ param2 +"') ";
自去年以來發生了很多事情。在這里,我試圖簡化答案。
string ConnStr = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
string SQL1 = "INSERT INTO tbl_cust(cust_id,cust_name) values ('000001','YoungMcD') ";
string SQL2 = "UPDATE tbl_cust SET custname='OldMcDonald' WHERE cust_id='000001'";
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlTransaction transaction = null;
try
{
conn.Open();
transaction = conn.BeginTransaction();
using (SqlCommand cmd = new SqlCommand(SQL1, conn, transaction)) { cmd.ExecuteNonQuery(); }
using (SqlCommand cmd = new SqlCommand(SQL2, conn, transaction)) { cmd.ExecuteNonQuery(); }
transaction.Commit();
savestats = true;
}
catch (Exception ex)
{
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
}
}
}
之所以在try{}
之外聲明事務,是因為我們可以在catch{}
回滾它。
這段代碼的conn.Open()
是,無論什么原因在conn.Open()
發生錯誤,然后嘗試進行transaction.Rollback()
conn.Open()
transaction.Rollback()
將導致異常。
這就是為什么要添加另一個try{}
catch{}
來處理它的原因。
您應該使用一個命令,並將連接包裝在“使用”塊中,以便正確處理它。 另外,在執行事務提交后,您應該通過執行SqlDataReader從tbl_supplier中進行讀取。 我假設您只是想知道提交事務后受影響的行數。
這是代碼的簡化版本。
var conn = System.Configuration.ConfigurationManager.ConnectionStrings["connstr"].ConnectionString;
string SQL1 = "INSERT INTO tbl_cust(cust_id,cust_name) values ('000001','YoungMcD') ";
string SQL2 = "UPDATE tbl_cust SET custname='OldMcDonald' WHERE cust_id='000001'";
using (SqlConnection connection = new SqlConnection(conn))
{
connection.Open();
SqlTransaction sqlTran = connection.BeginTransaction();
SqlCommand command = connection.CreateCommand();
command.Transaction = sqlTran;
try
{
command.CommandText = SQL1;
int rowsAffected = command.ExecuteNonQuery();
command.CommandText = SQL2;
rowsAffected += command.ExecuteNonQuery();
transaction.Commit();
}
catch (Exception ex1)
{
// Attempt to roll back the transaction.
try
{
transaction.Rollback();
}
catch (Exception ex2)
{
// This catch block will handle any errors that may have occurred
// on the server that would cause the rollback to fail, such as
// a closed connection.
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.