Database connections
-
I'm using Npgsql to connect to postgresql in my dot net app. I create a new connection every time I need to perform a database operation. I store the username and password and if the user goes to another windows form, I pass the username and password to the constructor and then create another connection. This seems wrong to me. Is there a better way to do this? :)
-
I'm using Npgsql to connect to postgresql in my dot net app. I create a new connection every time I need to perform a database operation. I store the username and password and if the user goes to another windows form, I pass the username and password to the constructor and then create another connection. This seems wrong to me. Is there a better way to do this? :)
I think so; instantiate the connection once, open it and close it as needed, pass it around as needed.
-
I'm using Npgsql to connect to postgresql in my dot net app. I create a new connection every time I need to perform a database operation. I store the username and password and if the user goes to another windows form, I pass the username and password to the constructor and then create another connection. This seems wrong to me. Is there a better way to do this? :)
Hi Here's my suggestion Have a DataHelper(or DataManager) class that is responsible to do DB management and other stuffs (like opening connections) make it singleton. It can have a OpenConnection method that opens a new connection and returns it every time you want an open connection.For example
using(SqlDBConnection cnn=DataManager.Instance.OpenConnection()) { //do something with connection. cnn.Close() }//Since SqlDBConnection is IDisposable it will dispose automatically
Hamidian -
Hi Here's my suggestion Have a DataHelper(or DataManager) class that is responsible to do DB management and other stuffs (like opening connections) make it singleton. It can have a OpenConnection method that opens a new connection and returns it every time you want an open connection.For example
using(SqlDBConnection cnn=DataManager.Instance.OpenConnection()) { //do something with connection. cnn.Close() }//Since SqlDBConnection is IDisposable it will dispose automatically
HamidianOh I forgot that you were not using SQL Server.Then replace the SqlDbConnection with OleDbConnection. :)
-
I'm using Npgsql to connect to postgresql in my dot net app. I create a new connection every time I need to perform a database operation. I store the username and password and if the user goes to another windows form, I pass the username and password to the constructor and then create another connection. This seems wrong to me. Is there a better way to do this? :)
It doesn't seem to bad to me. This is (in effect) the way that you would be working with the connections if you were working in a technology like ASP.NET. If the connection is pulled from the Connection Pool then you shouldn't have too many problems.
Deja View - the feeling that you've seen this post before.