I
Imran Aziz
Hello All,
Like in C++ I tried to use constructor to open a database connection and
distructor to close the database connection, it now turns out that one
cannot create distrutors in C# classes.
Here is my code
public class DBLayer
{
private string strError;
private SqlConnection conn;
private SqlDataAdapter mySqlTagsAdapter;
private string strQuery;
public DBLayer()
{
strQuery = "";
conn = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
strError = "";
try
{
conn.Open();
}
catch (Exception ex)
{
strError = "Unable to open the database " + ex.ToString();
}
}
~DBLayer()
{
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
{
try
{
conn.Close();
}
catch (Exception ex)
{
strError = "Unable to close the database " + ex.ToString();
};
}
}
}
}
Can you suggest how to sort out this issue now, I have already got lot of
classes and code that uses this approach and now the connections are not
being closed, can you please suggest a solution how to sort this out. How
can I close connection in the classes when the calling asp.net page ends
execution.
Regards,
Imran.
Like in C++ I tried to use constructor to open a database connection and
distructor to close the database connection, it now turns out that one
cannot create distrutors in C# classes.
Here is my code
public class DBLayer
{
private string strError;
private SqlConnection conn;
private SqlDataAdapter mySqlTagsAdapter;
private string strQuery;
public DBLayer()
{
strQuery = "";
conn = new
SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
strError = "";
try
{
conn.Open();
}
catch (Exception ex)
{
strError = "Unable to open the database " + ex.ToString();
}
}
~DBLayer()
{
if (conn != null)
{
if (conn.State != ConnectionState.Closed)
{
try
{
conn.Close();
}
catch (Exception ex)
{
strError = "Unable to close the database " + ex.ToString();
};
}
}
}
}
Can you suggest how to sort out this issue now, I have already got lot of
classes and code that uses this approach and now the connections are not
being closed, can you please suggest a solution how to sort this out. How
can I close connection in the classes when the calling asp.net page ends
execution.
Regards,
Imran.