SqlConnection.CreateCommand()

  • Thread starter Thread starter Bob Grommes
  • Start date Start date
B

Bob Grommes

Folks,

I Google in vain for the answer to a seemingly simple question. I am
thwarted by vague documentation.

Why would one do this:

// conn is an already-initialized SqlConnection
SqlCommand cmd = conn.CreateCommand()

as opposed to:

SqlCommand cmd = new SqlCommand();

.... especially when you can pass a string to SqlCommand's ctor and create
the command string in one go if you wish, and CreateCommand() has no such
override?

MSFT docs on CreateCommand describe what it does (evidently just news up an
Sq1Command) but not why a method has been dedicated to this purpose. The
only reason I can think of for its existence is that if you're trying to
write a generic interface to arbitrary back ends, you can use this to create
an command object that's appropriate for the data library you're using. Is
that all it amounts to? Or are the docs failing to mention some other
benefit?

--Bob
 
Hi Bob,

Yes, I suppose it exists for the sake of building a generic database code.
 
When the 2.0 version of ADO.NET arrives along with CLR stored procedures and
functions, you'll get a better idea of why this is done...

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
Back
Top