What's wrong with my SqlCommand?

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
 
C

Christof Nordiek

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.
 
M

Marc Gravell

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top