Using the same connection over many forms

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,
I'm writing a client-server winfroms application, I've notice that every
time I use the wizard to create a bound datagrid, I create a new
sqlconnection.
I wish to create one connection in the main form, and use it in the rest.
(b/c changing something in the connection string - like pwd user or db will
make me go over all the classes now, and find all the connections the wizards
creatred)
what is the best way to do it ?
Regards,
Dani
 
I define the connection string in either the application config file
(app.config) or the registry and then use a class which exposes a static
method that returns the connection string.

For e.g.

public class Settings
{
public static ConnectionString
{
get
{
//Get the connection string either by using registry or by
using ConfigurationSettings.AppSettings class
}
}
}

Now in all the classes where i need a connection i simply use

SqlConnection conn = new SqlConnection(Settings.ConnectionString);

Sijin Joseph
http://www.indiangeek.net
http://weblogs.asp.net/sjoseph
 
Back
Top