Hi,
I am wondering what peoples thoughts are on code which looks like the following
pseudocode.
IDbCommand cmd = null;
using (IDbConnection cnn1 = CreateANewConnectionSomehow())
{
// Prepare a statement on one connection
cmd = cnn1.CreateCommand();
cmd.CommandText = "SELECT COUNT(*) FROM SomeTable";
cmd.CommandType = CommandType.Text;
cmd.Prepare();
}
... some time later ...
using (IDbConnection cnn2 = CreateANewConnectionSomehow())
{
// Execute the command on another connection
cmd.Connection = cnn2;
cmd.ExecuteScalar();
}
The CreateANewConnectionSomehow() method creates a IDbConnection using the same
connection string both times (so we are talking about executing agaist the same
database).
I ran across some code burried within a framework which essentially does this.
I
can't seem to find too much documention on this sort of behaviour. I guess i'm
looking for documentation similiar to that available for COM's Threading Model
for
ADO.NET Data Provider objects such as IDbConnections and IDbCommands.
My initial thought is that although it seems to work for the given ADO.NET Data
Provider I am using (SQL Server), it is not really a recommended or reliable
practice. For instance if I was implementing an ADO.NET Data provider I could
think
of scenarios where my implementation of the IDbCommand.Prepare() method may
rely
upon state which is local to the connection the command is currently associated
with.
I'm interested in your thoughts,
Christopher Fairbairn
===================================
This list is hosted by DevelopMentorĀ® http://www.develop.com
View archives and manage your subscription(s) at http://discuss.develop.com