0

I have some code that goes like this:

SqlTransaction trans = conn.BeginTransaction("MyTransfer");

cmdStep1.ExecuteScalar();
cmdStep2.ExecuteScalar();
cmdStep3.ExecuteScalar();
QueryTheDatabaseForSomeData() // here it calls another method that queries the database
trans.Commit();

My code hangs on the select query (QueryTheDatabaseForSomeData()) that is in the middle of the ADO SqlTransaction, as shown in the psuedo-code above.

This code seemed to work before, but it has suddenly stopped working.

Can someone tell me if it is possible to structure the code like this, so that there is select query function in the middle of the ADO SqlTransaction?

Thanks

  • Yes. you can do a "select" within an ADO.NET transaction. I suspect the problem might be a "deadlock", e.g. https://stackoverflow.com/a/39209839/421195. SUGGESTION: Enable tracing to determine exactly where things go wrong: https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/data-tracing. See also the NOLOCK table hint: https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-table – paulsm4 Jul 29 '23 at 00:24
  • You can select within a transaction but you must set the transaction for the select SqlCommand if it's using the same SqlConnection – PrfctByDsgn Aug 04 '23 at 12:56

0 Answers0