We help IT Professionals succeed at work.

C# SqlCommand ExecuteNonQuery value

jkavx
jkavx used Ask the Experts™
on
I'm confused about the value returned by the SqlCommand ExecuteNonQuery method.  I'm doing something like this:
    string sSql = "CREATE TABLE #TMP_X (COLA nvarchar(255), COLB nvarchar(max))";
    SqlCommand cmd = new SqlCommand(sSql, SqlConn);
    int iRowCt = cmd.ExecuteNonQuery();

The value of iRowCt is -1, but the table is created and records can be inserted to it.  If I execute the same command directly in isql or in a stored proc, @@ROWCOUNT = 0.  What exactly is ExecuteNonQuery returning, and is the -1 return value that I'm getting indicative of any problem?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Expert of the Quarter 2010
Expert of the Year 2010
Commented:
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery.aspx

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

DDL generates no rowcount, therefore you get -1