Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi...
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

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.
 
Share this answer
 
Comments
Usama Ansari 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!
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.
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 :)
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#
 
Share this answer
 
v3
Comments
Usama Ansari 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.
Link: http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.connectionstrings.aspx
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 :)
Try using Static class and variables for this.


C#
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;
        objConn.Open();
    }



    public static void CloseConnection()
    {
        objConn.Close();
    }



}



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

C#
DataAccessClass.OpenConnection();


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.
 
Share this answer
 
v3
Comments
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..
Irbaz:
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?
Thanks..
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.

http://msdn.microsoft.com/en-us/library/ms254494.aspx[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900