Click here to Skip to main content
15,905,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi... Friends

I used to make connection object every time, then
I open it and after doing it I Close it. This take lot of time. Is there anyway to get rid off this.
Posted

makes a global Connection in web.config page.. Add this code


<appSettings>
<add key="mas" value="data source=ARSHAD\SQLEXPRESS; initial catalog=ug; integrated security=true;"/>
</appSettings>
<connectionStrings/>


In C# page.. Declare Connection & write it before page load i.e. makes it lobally..

SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["mas"]);

after that use only con (i.e. connection obj) where u need for connection.. by this there is no need to makes connection again & again..

Plz vote me or mark as solution if this is ur solution.
 
Share this answer
 
Comments
Varinder Raii 2-Mar-12 7:02am    
where is web.config.page. in Windows Application
make a static class

C#
class ClsMain
    {
        
        public static SqlConnection Con = null; 

    }


make a fuction to make connection
C#
public static SqlConnection GetConnection(string connectionString)
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();           

            return connection;
        }


use this line initialize connection
C#
clsMain.Con=GetConnection("connection string"); 
 
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