PDA

Click to See Complete Forum and Search --> : Global Connection Object for the application.


Venkrishna
Jun 23rd, 2003, 06:03 AM
Guys,

I am new to C# Programming. I want to have a single connection object accessible across the application. The Object should be created when the application starts and should be available to all the module / functions of the windows applicaiton. am I right in doing this way or any other new concept in achieving this in C#?
I am not interested to use any design time objects. The object should also be destroyed when the application is closed.

Also I have created one class for the project and this class has a property called "appConn" whcih I want to use in my application.

Any help material regarding this and code is appreciated.

Thanks,
venkrishna.

ggprogram
Jun 23rd, 2003, 08:17 AM
In thinking through the design of a little application I am working on, I decided to isolate all functions related to the OleDb namespace to one class so that if I ever want to change to another dbms, I only have to modify this one class. That being said, all you really need to do is set up a class that has the connection as a private instance variable and create a property to return the connection.

public class dbConnect
{

private OleDbConnection conn;
private string connString;

public dbConnect()
{


connString = "Provider=Microsoft.Jet.OLEDB.4.0;Password="""";User ID=Admin;Data Source=C:\Documents and Settings\Glenn\My Documents\My Data Sources\RentalInfo.mdb, etc";
//
conn = new OleDbConnection(connString);

}

public OleDbConnection Connect
{
get
{ return conn;}
}


Then in your code, something like this:

MyObj = new DbConnect();
MyConn = MyObj.Connnect;
MyConn.Open();

Venkrishna
Jun 23rd, 2003, 09:44 AM
Hi ggprogram,

Thanks for the code. Happy to see that I have written similar code except that you have a read-only connection object and that you have used the constructor to establish a connection.

I will have to try your code but I am wondering if the Mycon object is available through the application?

Will get back with some issues later.

Thanks,
venkrishna.

ggprogram
Jun 23rd, 2003, 10:25 AM
Originally posted by Venkrishna
I will have to try your code but I am wondering if the Mycon object is available through the application?
venkrishna.

If you want to treat it as a "global" you might look into static methods.