交易范围和MSDTC

问题描述:

尊敬的先生,
我正在C#中使用ADO,在事务范围内(在{}内部),我调用了许多需要连接到数据库的函数,其中约有5个函数使用ExecuteReader,ExecuteNonQuery,...
但最后一个使用SQL数据适配器

Dear sirs,
i am using ADO with C#, in transaction scope (inside using{ }) i call many functions that need to connect to database, about 5 of them use ExecuteReader , ExecuteNonQuery,...
but last one uses SQL Data adapter

conn.Open();
dbAdapter.Fill(datatable);
conn.Close()


(我知道我可以用打开和关闭连接填充dbAdapter)
它正确打开了一个连接,但是在(dbAdapter.Fill(datatable);)上它引发了一个异常"MSDTC不可用",并且我不想运行MSDTC服务,该问题是:如果连接打开了,为什么会引发错误,并且为什么不这样做,如果在填充之前没有再次打开连接(),并且如果我在填充之前没有打开连接,这可能会导致其他错误(例如ExecuteReader,ExecuteNonQuery ...),或者不是
并且有更好的方法来代替事务作用域

问候.


(i know that i can fill the dbAdapter with open and close connection)
it open a connection correctly but on (dbAdapter.Fill(datatable);) it throws an exception "MSDTC not available " and i don''t want to run the MSDTC service that question is : why throw an error if the connection opened and why not if the connection is not opened again () before filling and if i remove the open before filling may this make errors on other (like ExecuteReader , ExecuteNonQuery,...) or not
and is there a better way to use instead of transaction scope

Regards.

尝试此可承诺的事务

SqlConnection concTx =新的SqlConnection(ConfigurationManager.ConnectionStrings ["Constr"].ToString());
cTx = new CommittableTransaction();
试试
{
concTx.EnlistTransaction(cTx);
//执行多个查询功能
cTx.Commit();
cTx.Dispose();
concTx.Close();

}
捕获(TransactionException te)
{
cTx.Rollback();
cTx.Dispose();
concTx.Close();

}
捕获(SqlException sqE)
{
cTx.Rollback();
cTx.Dispose();
concTx.Close();

}

catch(ex ex例外)
{
cTx.Rollback();
cTx.Dispose();
concTx.Close();

}

注意:仅将单个"concTx"连接用于所有此操作不要忘记头文件
使用System.Transactions;
Try this Commitable trasaction

SqlConnection concTx = new SqlConnection(ConfigurationManager.ConnectionStrings["Constr"].ToString());
cTx = new CommittableTransaction();
try
{
concTx.EnlistTransaction(cTx);
//excecute your query function morethan one
cTx.Commit();
cTx.Dispose();
concTx.Close();

}
catch (TransactionException te)
{
cTx.Rollback();
cTx.Dispose();
concTx.Close();

}
catch (SqlException sqE)
{
cTx.Rollback();
cTx.Dispose();
concTx.Close();

}

catch (Exception ex)
{
cTx.Rollback();
cTx.Dispose();
concTx.Close();

}

Note:Use ONly this Single "concTx" connection for all this operation Dont forgot the header file
using System.Transactions;