Home All Groups Group Topic Archive Search About

What's wrong with my SqlCommand?

Author
14 Dec 2006 3:50 AM
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

Author
14 Dec 2006 7:45 AM
Christof Nordiek
Show quote Hide quote
"Jason Huang" <JasonHuang8***@hotmail.com> schrieb im Newsbeitrag
news:OkRmKMzHHHA.3872@TK2MSFTNGP06.phx.gbl...
> 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.
Are all your drivers up to date? click for free checkup

Author
14 Dec 2006 8:08 AM
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

Bookmark and Share