Results 1 to 11 of 11

Thread: Global Db connection

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    London
    Posts
    63

    Question Global Db connection

    I want to add a global Database connection object to my project. I have created the class and all is working. I have done the following in a form

    namespace Forms
    {
    public class frmLogin : System.Windows.Forms.Form
    {
    //'DataAccess object
    public clsDataAccess gDB = new clsDataAccess();

    ....

    BUT
    This teh objects is only accessible from that form. I have to do this on each form that I open in order to access the db.

    Is there a a way to create a global object of type clsDataAccess so that I can access the same object from any form or class.

    Alternatively, if I just create one object, how can I pass that object to another form??

    Any help appreciated.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Let any class (for good design it should not be one of the form classes) contain it as a public static element.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    London
    Posts
    63
    Thats Fantastic CornedBee.

    It worked!

    Cheers.

  4. #4
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Re: Global Db connection

    Genie,

    Looks like I am almost near to what you have achieved regarding this problem. Can you please post the code into the thread you have raised in vbforums?

    If you dont mind can you post the code for the class you have created for global connection and how you have instanciated the connection in the first form using this object.
    there r no alternatives 4 hardwork.

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    London
    Posts
    63

    Re: Global Db connection

    I don't have that code anymore (been more than 2 years since that post!), but all you need to do is create any class and declare the object as a public static. Then you can access it from anywhere, without even having to instantiate that particular class.

  6. #6
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Re: Global Db connection

    Genie,

    I have written this code.

    Code:
    using System;
    using System.Configuration;
    using System.Data.SqlClient;
    
    public class clsConn
    	{
    
    		public static SqlConnection ConnAA;
    
    		public clsConn()
    		{
    			//
    			// TODO: Add constructor logic here
    			//
    
    			ConnAA = new SqlConnection();
    			ConnAA.ConnectionString = ConfigurationSettings.AppSettings["WlConnString"];
    			ConnAA.Open();
    		}
    
    	}
    Will this be enough to access the connection object
    as clsConn.ConnAA? or do I have to written additional lines of code?

    Please advise.
    there r no alternatives 4 hardwork.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Global Db connection

    You should initialize ConnAA to null in its definition, and in the constructor check if it is null and only create the connection object in that case.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Re: Global Db connection

    Cornedbee,

    From what you have told I have altered the code as below.

    Code:
    using System;
    using System.Configuration;
    using System.Data.SqlClient;
    
    public class clsConn
    	{
    
    		public static SqlConnection ConnAA = null;
    
    		public clsConn()
    		{
    
    		if (ConnAA == null)
    			{
    				ConnAA = new SqlConnection();
    				ConnAA.ConnectionString  = ConfigurationSettings.AppSettings["WlConnString"];
    				ConnAA.Open();
    			}
    		}
    	}
    Any more changes required? Please advise.
    there r no alternatives 4 hardwork.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Global Db connection

    Looks good. In a multi-threaded app, it could cause problems, but that's pretty unlikely.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Re: Global Db connection

    CornedBee,

    Correct me if I am wrong. Do I have to go for a singleton approach to avoid such a problem?
    there r no alternatives 4 hardwork.

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Global Db connection

    This is, in a way, a singleton approach. You're using the connection as a singleton.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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