What's wrong with my SqlCommand?

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

In my codes:
=======================================
myConn.Open();
SqlTransaction MyTrans= myConn.BeginTransaction();

SqlCommand myCommand = new SqlCommand("ZZZ15UserLog",myConn);
myCommand.CommandType= CommandType.StoredProcedure;
SqlDataReader myReader;

myCommand.Transaction=MyTrans;
try
{
myCommand.ExecuteReader();
MyTrans.Commit();
}
catch { }
========================================

That will result in some error, however, if I change the ExecuteReader() to
ExecuteNonQuery(),
it will be fine.
Would someone tell me what have I done wrong with my code?
Thanks for help.


Jason
 
Jason Huang said:
Hi,

In my codes:
=======================================
myConn.Open();
SqlTransaction MyTrans= myConn.BeginTransaction();

SqlCommand myCommand = new SqlCommand("ZZZ15UserLog",myConn);
myCommand.CommandType= CommandType.StoredProcedure;
SqlDataReader myReader;

myCommand.Transaction=MyTrans;
try
{
myCommand.ExecuteReader();
MyTrans.Commit();
}
catch { }
========================================

That will result in some error,
<snip>
What is the error? The message and the type of the exception will give
information about what went wrong.

If ExecuteReader failed but ExecuteNonQuery not, then maybe the procedure
doesn't return any result.
 
I would hazard a guess that the exception relates to concurrent
commands, and that the reader needs to be Close()d / Dispose()d to
release the connection. In this case, ExecuteNonQuery would seem more
appropriate.

Marc
 
Back
Top