Click here to Skip to main content
12,945,401 members (80,099 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I'm beginner
I'm making collage project in c# winform
i want a single class that contain the code for SqlConnection string as well as for opening Connection !
and i want to access this connection in all form ...
And the Connection should open once in whole Application and close after closing the application..

Thanks in advance!
Posted 6-Feb-13 3:27am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

That is the wrong way to do it. You should open your connection immediately before accessing the database, and close it immediately afterwards. Leaving the connection open all the time your application is running is very bad practice, and in the case of multi-user applications can seriously impact performance and security.
usmslm102 6-Feb-13 9:34am
Thanks For Reply...
i mentioned above i'm student and making final yr project
so my internal guide told me to this ...
i stuck plzzz Help!
Kishor Deshpande 6-Feb-13 9:36am
Agreed and the other topic was already discussed to added link for the same in my solution.
My 5 for opening and closing connection answer :)
Richard MacCutchan 6-Feb-13 9:52am
Well your internal guide is wrong. And no one here is going to write your project for you.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Refer link given below:
Code Project Question: Sql Connection in seperate class
Refer link given below to create a data access component:
Creating DataAccess Component for SQL Server in C#
usmslm102 6-Feb-13 9:40am
not Helpful
how to access in another form !
Kishor Deshpande 6-Feb-13 9:45am
Well better way of doing it is to save in web.config and access it through ConfigurationManager.ConnectionStrings property.
Fetch this value once in reusable data access component to set ConnectionString value and use it in only that component cause you are going to access database through single file :)
Let me know if you have any query..
Richard MacCutchan 6-Feb-13 9:54am
And a 5 back from me; those links are useful for my own researches.
Kishor Deshpande 6-Feb-13 9:58am
Thanks Richard :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Try using Static class and variables for this.

public static class DataAccessClass
    static SqlConnection objConn = new SqlConnection();

    public static SqlConnection MyConnection
        get { return objConn; }
    public static void OpenConnection()
        string ConnectionSring = "MYCONNECTIONSTRING";
        objConn.ConnectionString = ConnectionSring;

    public static void CloseConnection()


This is very basic structure i am describing here.
I am not doing your homework so you have to add your code and exception handling by yourself.
Once you initiate the connection


Your connection is open for throughout the application and you don't have to open it again.

I suggest that YOU MUST NEVER IMPLEMENT THIS as it is a bad practice to leave Connection open.

Better way is to open connection execute your database operations and close it immediately.
Pete O'Hanlon 6-Feb-13 11:32am
Seriously? And what happens when he develops an application that has thousands of users, all connecting to the same database? What happens if the database crashes? Your code is extremely bad practice, and should be discouraged.
Irbaz Haider Hashmi 6-Feb-13 11:50am
Actually i told him the solution.
Above solution suggests that its no a good practice.
But his internal guide is forcing him to do so. So for an academic learning purpose i think its okay for him to know how it can be implemented.

Pete O'Hanlon 6-Feb-13 11:58am
No - never teach someone that a bad practice is acceptable. Once a bad practice is learned, it's very hard to unlearn. It's much better to show someone the right way to do it, rather than the wrong way.
Irbaz Haider Hashmi 6-Feb-13 12:08pm
Updated that line of in the solution . :)
Kishor Deshpande 6-Feb-13 22:07pm
True, Pete is right cause bad practices are easy to learn and once you start following easy ways of getting your things done, you will never look for best practices..
I appreciate that you have mentioned that it is bad practice.
Can you please update your answer with better approach rather than suggesting a solution and telling person not to follow that?
Irbaz Haider Hashmi 7-Feb-13 2:43am
Rate this: bad
Please Sign up or sign in to vote.

Solution 5

Everything that has been said before is 100% valid about opening and closing your connections. However one thing that hasn't been pointed out is that you probably shouldn't store you connection string inside a class because you will have to constantly rebuild your app if there is a change to the connection string.

You should take a look at storing the connection string in the config file so you can easily modify the connection string without having to rebuild or redeploy your app. Take a look at this article for details on how to accomplish that as well as how to access it in your application.[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,899
CHill60 3,170
Maciej Los 2,443
Jochen Arndt 1,910
ppolymorphe 1,775

Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 7 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100