this is simple example to open and close connection in .net just use it with out hesitation and do not forget to include required name space like

using System;

using System.Data.SqlClient;
using System.Data;

 

namespace Common
{
    public class Common
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connection"]);
       

        public SqlConnection opendb()
        {
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                else
                {
                    con.Close();
                    con.Dispose();
                    con.Open();
                }
                return con;
            }
            catch
            {
            }
        }

        public SqlConnection closedb()
        {
            try
            {
                if (con.State == ConnectionState.Open)
                {
                    con.Close();
                }
                return con;
            }
            catch
            {
            }
        }
    }
}


Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

Comments (0)

There are no comments posted here yet