B
Ben
Hi I have a question concerning SqlConnections and when they should be
opened and closed.
Right now I have a database utilities class. I instantiate it when in
page_load and dispose it in page_prerender, which opens and closes class
level sqlconnection.
Now, say I need to run a SqlCommand, all I do is:
SqlCommand oCmd = DBUtils.CreateSqlCommand("spName");
And I get a SqlCommand, type storedprocedure, with the connection set to the
classes database. I add parameters etc, and execute. Everything seems
great. I've been told (#mscorlib and #asp.net on efnet) that this isn't a
good approach. I should be using late open and early close on my database
connection. Especially to take advantage of connection pooling. This means
I need code like...
using (SqlCommand oCmd = DBUtils.CreateSqlCommand("spName"))
{
oCmd.Connection.Open();
oCmd.ExecuteScalar();
oCmd.Connection.Close();
}
Assuming all things are equal except the connection in dbutils is closed.
Is that a better approach? To me that doesn't guarantee the connection
closes or anything.
Maybe I should use...
using (SqlConnection oConn = DBUtils.DatabaseReference)
{
oCmd.Connection = oConn
oCmd.Connection.Open();
oCmd.ExecuteScalar();
oCmd.Connection.Close();
}
Or something like that??? Basically I'm trying to farm out as much of the
database coding as possible. Is this possible? What is everyone else
doing?
What if I have a class in my page that needs to access the database? What
do I do now? Pass reference to my database class?
Please, any help / advise will be greatly appreciated!
Thanks,
Ben
opened and closed.
Right now I have a database utilities class. I instantiate it when in
page_load and dispose it in page_prerender, which opens and closes class
level sqlconnection.
Now, say I need to run a SqlCommand, all I do is:
SqlCommand oCmd = DBUtils.CreateSqlCommand("spName");
And I get a SqlCommand, type storedprocedure, with the connection set to the
classes database. I add parameters etc, and execute. Everything seems
great. I've been told (#mscorlib and #asp.net on efnet) that this isn't a
good approach. I should be using late open and early close on my database
connection. Especially to take advantage of connection pooling. This means
I need code like...
using (SqlCommand oCmd = DBUtils.CreateSqlCommand("spName"))
{
oCmd.Connection.Open();
oCmd.ExecuteScalar();
oCmd.Connection.Close();
}
Assuming all things are equal except the connection in dbutils is closed.
Is that a better approach? To me that doesn't guarantee the connection
closes or anything.
Maybe I should use...
using (SqlConnection oConn = DBUtils.DatabaseReference)
{
oCmd.Connection = oConn
oCmd.Connection.Open();
oCmd.ExecuteScalar();
oCmd.Connection.Close();
}
Or something like that??? Basically I'm trying to farm out as much of the
database coding as possible. Is this possible? What is everyone else
doing?
What if I have a class in my page that needs to access the database? What
do I do now? Pass reference to my database class?
Please, any help / advise will be greatly appreciated!
Thanks,
Ben