Hi,
To make data-life a little easier, and not having to declare a
SqlConnection on each page, I came up with this Sql-Data-class:
' ---------------------------------
using System;
using System.Data;
using System.Data.SqlClient;
namespace MyFirstMaybeLeakyClasses
{
public class Data {
public Data()
{
oConn.Open()
}
private static SqlConnection oConn;
public static SqlConnection Connection {
get {
if (oConn == null)
oConn = new SqlConnection([connectionstring));
if (oConn.State != ConnectionState.Open)
oConn.Open();
return oConn;
}
}
}
}
' ---------------------------------
What are the cons of using a SqlConnection like this (as a static)? And
should I use it like this at all?
I understood each user-request will instantiate a new connection, so
multiple readers on the same connection shouldn't be a problem. Or am I
mistaking?
From time to time it throws a SqlException stating multiple
SqlDataReaders are trying to use the same connection. Eventhough I'm
using a DataAdapter... I suspect this happens whenever more that one
user is requesting the page(s).
Finally I have this problem that sometimes it complains it can't get a
connection from the connectionpool. I am closing the connection at the
end of each page. Does a connectionclose take several minutes to fully
terminate the connection or could it be something else?
Thanks in advance!