Click here to Skip to main content
15,898,588 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
please tell me correct way, how to open and close the sql connection in asp.net
Posted
Comments
ArunRajendra 1-Aug-13 0:12am    
Google it you will get tons of answers.
Mithra Madhu 1-Aug-13 1:07am    
if u don't mind can u explain this code
how it will work???

private static void OpenConnection(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
Console.WriteLine("State: {0}", connection.State);
}
}

1 solution

C#
private static void OpenConnection(string connectionString)
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("State: {0}", connection.State);
    }
}


if you want to check by condition then...:)

C#
String strConn = System.Configuration.ConfigurationManager.ConnectionStrings["conStr"].ToString();
OracleConnection con = new OracleConnection(strConn);
if(if(con.State == ConnectionState.Open)
{
lable1.text= "Connection is Open";
}
else
{
lable1.text= "Connection is close";
}
 
Share this answer
 
v2

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