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. 8 14531
Hi Imran,
One way to accomplish this by making a disposable object and use the
"using" construct. The idea is that the object's Dispose( ) method
will be called when the using block is exited (much like a dtor for a
stack object in C++).
John
Imran Aziz wrote:
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.
Close connection manually (or use using statement for it)
PS: and forget about descructors in .net :)
--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
"Imran Aziz" wrote:
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.
Hi,
You can have a destructor in C# too.
But that is another matter, you should keep your connection open the minimun
time possible ! , I would suggest you to just open/close the connection in
each method that you need it.
like
void DoSomething()
{
using (SqlConnection con = new SqlConnection(......) )
{
}
}
This will allow your app to scale. If you keep connections open you are
holding resources for nothing. ADO.NET use a connection pooling that handles
the connection status for you.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Imran Aziz" <im***@tb2.netwrote in message
news:e$**************@TK2MSFTNGP04.phx.gbl...
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.
Thanks for the response, I am going to close them explicitly now.
"Michael Nemtsev" <Mi************@discussions.microsoft.comwrote in
message news:E1**********************************@microsof t.com...
Close connection manually (or use using statement for it)
PS: and forget about descructors in .net :)
--
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour
"At times one remains faithful to a cause only because its opponents do
not
cease to be insipid." (c) Friedrich Nietzsche
"Imran Aziz" wrote:
>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.
Thanks for the response John, I am going to close them explicitly now.
"John Duval" <Jo********@gmail.comwrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Hi Imran,
One way to accomplish this by making a disposable object and use the
"using" construct. The idea is that the object's Dispose( ) method
will be called when the using block is exited (much like a dtor for a
stack object in C++).
John
Imran Aziz wrote:
>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.
Hi,
"Imran Aziz" <im***@tb2.netwrote in message
news:O8**************@TK2MSFTNGP04.phx.gbl...
Thanks for the response, I am going to close them explicitly now.
The recommended coding is wrapping the conenction class with an using
statement, this will assure that even if an exception occur the connection
is closed.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Thanks for that, I will surly use this from now on, the issue at the moment
however is that I have got around 15 classes already using this technique,
the constructor opens the connections hence in order to use the using option
I will endup altering all methods which is quite a big task, so for now I am
just closing the connection making sure I close it in try/catch blocks as
well .
Thanks a lot,
Imran.
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.uswrote
in message news:uo**************@TK2MSFTNGP03.phx.gbl...
Hi,
"Imran Aziz" <im***@tb2.netwrote in message
news:O8**************@TK2MSFTNGP04.phx.gbl...
>Thanks for the response, I am going to close them explicitly now.
The recommended coding is wrapping the conenction class with an using
statement, this will assure that even if an exception occur the connection
is closed.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Thank you very much for the detailed explanation, I am following this now.
Imran.
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.uswrote
in message news:e8**************@TK2MSFTNGP04.phx.gbl...
Hi,
You can have a destructor in C# too.
But that is another matter, you should keep your connection open the
minimun time possible ! , I would suggest you to just open/close the
connection in each method that you need it.
like
void DoSomething()
{
using (SqlConnection con = new SqlConnection(......) )
{
}
}
This will allow your app to scale. If you keep connections open you are
holding resources for nothing. ADO.NET use a connection pooling that
handles the connection status for you.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Imran Aziz" <im***@tb2.netwrote in message
news:e$**************@TK2MSFTNGP04.phx.gbl...
>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.
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: C Sharp beginner |
last post by:
I'm sorry about this verbose posting. This is a follow-up
to my yesterday's posting. Thanks William for your reply.
I understand it is a good practice to open connections as
late as possible and...
|
by: Macca |
last post by:
Hi,
I have an windows forms application that accesses a SQL database
I have a few questions as to connecting to the database.
This application will run 24 hours a day. It is a monitoring...
|
by: Michael |
last post by:
Hello,
In my past ASP pages, at the top I used an include file to open an ado
connection for the entire page, then at the bottom, I would have another
include file to close the connection.
...
|
by: mescano |
last post by:
I am currently implementing a singleton pattern for accessing a
database. Is it advisable to close the connection to the database at
all -- thus leaving it open or should it be closed. If closed,...
|
by: fniles |
last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader.
As I read data from tblA, I want to populate tblB. I use SQLDataReader for
both tables. I do not use thread.
When I ExecuteReader on tblB, I...
|
by: Usman Jamil |
last post by:
Hi
I've a class that creates a connection to a database, gets and loop on a
dataset given a query and then close the connection. When I use netstat
viewer to see if there is any connection open...
|
by: =?Utf-8?B?RyBIdXN0aXM=?= |
last post by:
I am creating a new MS Access DB in code, which works fine. I then try to
open it & set a password. I keep getting an error that the DB is already open
by my machine's Admin account. In reality it...
|
by: Tony Johansson |
last post by:
Hello!
How does this connection pool function actually ?
As I have been told is that when the connection is returned to the pool
the connection to the database is still open. What you do is that...
|
by: Author |
last post by:
a .net 1.1 app has a class whose constructor opens a db connection to
sql svr two thousand. this class has more than a dozen of methods.
most of them don't do db stuff.
I am wondering if this...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |