Results 1 to 4 of 4

Thread: Global Connection Object for the application.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Global Connection Object for the application.

    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.
    there r no alternatives 4 hardwork.

  2. #2
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    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();

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153
    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.
    there r no alternatives 4 hardwork.

  4. #4
    Lively Member
    Join Date
    Jan 2003
    Posts
    71
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width