Results 1 to 8 of 8

Thread: Database Connection

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Question Database Connection

    Hi Folks,

    I have written a class in the C# windows forms application. This class has a SqlConnection Object.

    With this class I want to open a connection and this instance of the class (connection) should be available globally for all the forms. Just like using only one connection object through out all the forms in the application.

    And I want to close this connection when the MDI form is closed.

    Any ideas??
    there r no alternatives 4 hardwork.

  2. #2
    Frenzied Member axion_sa's Avatar
    Join Date
    Jan 2002
    Location
    Joburg, RSA
    Posts
    1,724

    Re: Database Connection

    Yes - don't. Open and close your database connections as needed, preferably in a separate data layer (have a look at n tier design); .Net uses what's known as "connection pooling" - it will help prevent unnecessary locks and similar undesirable effects of single connections.

    However, should you still wish to use a single connection, you could use the singleton pattern:
    Code:
    /*
    	In MDI form.
    */
    
    private SqlConnection _sqlConn;
    
    public SqlConnection Connection
    {
    	get
    	{
    		if (_sqlConn == null)
    			// Create & open connection to database.
    			_sqlConn = new SqlConnection("Connection string");
    		return _sqlConn;
    	}
    }
    
    /*
    	In child forms.
    */
    
    private void FooDatabaseFunction()
    {
    	// Create objects etc and reference MDIForm.Connection
    }
    Ps - I haven't used MDI in .Net as yet, so I'm not sure how you reference the parent MDI form, so MDIForm.Connection probably isn't right.

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Database Connection

    Connection pooling is not designed for app's that connect to a database on a local network and stay connected all day long. For those types of applications, having a single connection stay open is my recommended method.

    Do you have a database tier to your structure?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    Frenzied Member mar_zim's Avatar
    Join Date
    Feb 2004
    Location
    Toledo Cebu City.
    Posts
    1,416

    Re: Database Connection

    and before opening your connection. check first its state if it's close or not.
    Code:
    public class xsqlconn
    	{
    		public static void openconn(SqlConnection cn)
    		{
    			if(cn.State==ConnectionState.Closed)
    			{
    				cn.Open();
    			}
    		}
    
    		public static void closeconn(SqlConnection cn)
    		{
    			if(cn.State==ConnectionState.Open)
    			{
    				cn.Close();
    			}
    		}

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Re: Database Connection

    Guys,

    I have this MDI application with SQL Server as the backend.

    From your view points where should I create an instance of this class so that it is accessible by all child forms? Do I have to open the connection in the login form / mdi form once so that it is accessible by all the child forms?

    If not how would I otherwise create an instance of a conn object that will be opened once and closed only once and still accessed by all the child forms in my MDI app?

    For eg. As in VB6.0 I used to declare a conn object in a module and then the conn is opened in the mdi form / login form and thus the conn object is available to all child forms through out the application and the conn is closed when the user exits the application. I guess this should be possible in .Net.

    Please advise.
    there r no alternatives 4 hardwork.

  6. #6
    Hyperactive Member fret's Avatar
    Join Date
    Sep 2004
    Posts
    472

    Re: Database Connection

    for what i did in my project, i used to open the connection in the presents of an mdi and when i open a child form just past the connection to opened child.

    though i'm not sure if it is a good practice.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2001
    Posts
    153

    Re: Database Connection

    Fret,

    Thanks for the reply. I have found a solution from the help of few forum mates.
    there r no alternatives 4 hardwork.

  8. #8
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: Database Connection

    A few variation from what you gathered.

    Code:
    using System;
    using System.Data;
    using System.Data.SqlClient;
    
    namespace WindowsApplication2
    {
    	/// <summary>
    	/// Summary description for Connection.
    	/// </summary>
    	public class Connection
    	{
    		static SqlConnection instance=null;
    		private Connection()
    		{
    			//
    			// TODO: Add constructor logic here
    			//
    		}
    
    		public static SqlConnection GetInstance()
    		{
    			if(instance==null) instance=new SqlConnection();
    			return instance;
    		}
    
    		public static void Close()
    		{
    			if(instance.State==ConnectionState.Open) instance.Close();
    		}
    
    		public static void Open()
    		{
    			if(instance.State==ConnectionState.Closed) instance.Open();
    		}
    	}
    }
    Can be used like
    Code:
    			Connection.GetInstance().ConnectionString="user id=sa;password=password;initial catalog=northwind;data source=localhost";
    			try
    			{
    				Connection.Open();
    			}
    			catch(Exception ex)
    			{
    			}
    			finally
    			{
    				Connection.Close();
    			}
    User of the Connection class has the option to set it from.

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