Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WinForm
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 6-Feb-13 3:27am
Rate this: bad
good
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.
  Permalink  
Comments
usmslm102 at 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 at 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 at 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 :)
Rate this: bad
good
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#
  Permalink  
v3
Comments
usmslm102 at 6-Feb-13 9:40am
   
not Helpful
how to access in another form !
Kishor Deshpande at 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 at 6-Feb-13 9:54am
   
And a 5 back from me; those links are useful for my own researches.
Kishor Deshpande at 6-Feb-13 9:58am
   
Thanks Richard :)
Rate this: bad
good
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;
        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
 
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.
  Permalink  
v3
Comments
Pete O'Hanlon at 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 at 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 at 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 at 6-Feb-13 12:08pm
   
Updated that line of in the solution . :)
Kishor Deshpande at 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..
Irbaz Haider Hashmi at 7-Feb-13 2:43am
   
Done.
Rate this: bad
good
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.
 
http://msdn.microsoft.com/en-us/library/ms254494.aspx[^]
  Permalink  

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

  Print Answers RSS
0 CHill60 280
1 Sarvesh Kumar Gupta 238
2 Sergey Alexandrovich Kryukov 203
3 OriginalGriff 163
4 DamithSL 153


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