c# - Open new database connection in scope of TransactionScope without enlisting in the transaction -
is possible open new sqlconnection inside transactionscope, without referencing other connection in transaction? inside transaction, need run command should not take part in transaction.
void test() { using (var t = new transactionscope()) using (var c = new sqlconnection(constring)) { c.open(); try { using (var s = new sqlcommand("update table set column1 = 1"); { s.executescalar(); // if fails } t.complete(); } catch (exception ex) { saveerrortodb(ex); // don't want run in same transaction } } } // don't want involved in transaction, because generate // distributed transaction, don't want. want error go // db not caring run inside transactionscope of previous function. void saveerrortodb(exception ex) { using (var db = new sqlconnection(constring)) { db.open(); using (var cmd = new sqlcommand("insert errorlog (msg) values (" + ex.message + ")) { cmd.executenonquery(); } } }
found myself finally:
the other sqlconnection must initialized "enlist=false", connection not enlisted in same transaction:
using (var db = new sqlconnection(constring + ";enlist=false")) { ...
Comments
Post a Comment