Geeks With Blogs


Add to Google

Tim Hibbard CEO for EnGraph software

I find it frustrating that the SqlCommand object doesn't automatically set the CommandType to StoredProcedure on instantiation.  If you look in Reflector, you'll see that _commandType isn't initialized to anything.  I thought Microsoft wanted us using stored procedures.

Anyways, I got tired of always writing this code:

SqlCommand CMD = new SqlCommand(spName); CMD.CommandType = CommandType.StoredProcedure;

So I wrote a little function that I keep in my DAL classes:

private SqlCommand NewSqlCommand(string sqlText) { SqlCommand rv = new SqlCommand(sqlText); rv.CommandType = CommandType.StoredProcedure; return rv; }

So now when I want a new SqlCommand object, I use this following code:

SqlCommand CMD = NewSqlCommand(spName);

I know I'm only saving one line of code , but I think my physical therapist would be proud!


Technorati tags: , ,
Posted on Monday, March 5, 2007 3:52 PM .NET | Back to top

Comments on this post: Why doesn't SqlClient.SqlCommand CommandType default to StoredProcedure?

# re: Why doesn't SqlClient.SqlCommand CommandType default to StoredProcedure?
Requesting Gravatar...
Why not return an IDbCommand so you can isolate all the SQL-specific code in just the DAL classes? In case you move away from MS Sql Server... someday.
Left by Bill on Mar 05, 2007 6:29 PM

Your comment:
 (will show your gravatar)

Copyright © Tim Hibbard | Powered by: