Open Means Open Means

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
            {
            }
        }
    }
}

No comments

Login to post a comment

Show
  • Create an account
  • Forgot your username?
  • Forgot your password?
  • About Us
  • Faqs
  • Contact Us
  • Disclaimer
  • Terms & Conditions