Click here to Skip to main content
15,897,519 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

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

(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.
Posted

1 solution

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;
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900