Tuesday, 20 January 2015

How To create Connection in Asp .Net website

Define All Module level variables
    SqlDataAdapter Da;
    SqlConnection Con;
    SqlCommand Cmd;
    DataSet ds;

Note:   add the connection string in web config file in website-
      <connectionStrings>
  <add name="ConnectionString" connectionString="data source=MANISH-PC\SQLEXPRESS;database=Demo;uid=sa;pwd=server"
   providerName="System.Data.SqlClient" />

 </connectionStrings>



    private void OpenConnection()
    {
        if (Con == null)
        {
         Con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
        }
        // lets open the connection

        if (Con.State == ConnectionState.Closed)
        {
            Con.Open();
        }

        Cmd = new SqlCommand();

        //Set active connection with the command object

        Cmd.Connection = Con;

    }



    private void CloseConnection()
    {
        if (Con.State == ConnectionState.Open)
        {
            Con.Close();
        }
    }
    /// <summary>
    /// This is to release the memory from the connection object
    /// </summary>
    private void DisposeConnection()
    {
        if (Con != null)
        {
            Con.Dispose();
            Con = null;
        }
    }

No comments:

Post a Comment

Working with 3- Tier Architecture in C#

  Introduction In this article we will learn Use to 3- Tier architecture in C#.NET application. 3-Tier architecture is very famous and ...