Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hello, this is Vikash Gohil.
I have developed a web application using ASP.net and SQL Server 2000.
In my application I have created a class and in that class I have created SQL Connection variable.
Then in Constructor for that class ie, Sub New(), I am assigning the connection string and opening the connection.
Then on each web page I am creating the instance of this class.
So my question is, on creating each new instance will it create a new connection to the Database?
If yes, then how can I do that one connection is created for one user, when he logs on and disconnected when he logouts?
Had this confusion in my mind.
Any help would be greatly appreciated.
Thanks In Advance.
Posted

Vikash,
I would not recommend you to use this model for your website.
You need the connection open only for a particular database operation(s), after that you have to close the connection, otherwise you are doomed to some memory consumption problems and database lock issues.
Instead, I would recommend you the following construct:
1. create/open connection
2. do some operations against the database
3. close the connection immediately after.

In c# there is a pattern for doing such things:

C#
using (SqlConnection con= new SqlConnection("connection string"))
{
   con.Open();
   //do what you need here
}


Good luck.
 
Share this answer
 
Comments
VikashGohil 2-Dec-10 7:20am    
Thanks for your reply.
There is one question, If i open and close connection each time, then will this not put an overhead of connection on each request due to which my application response will slow down.
Also can you tell me how can I create an object for my class which I can share across entire application.
I guess i need to do this using Session, but is there any other way and would it be advisable?
Please reply.
Thank you.
public class Connectionclass
{
	
	SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings[" Educational"]);

    public bool opencon()
    {
    bool retval=false;
        try
        {
        if (con.State==ConnectionState.Open)
        {
        con.Close();

        }
            con.Open();
            retval=true;
        }
        catch(Exception ex)
        {
            throw(ex);
        }
        return retval;
            
    
    }

	public bool closecon()
    {
 bool retval=false;
        try
        {
            if(con.State==ConnectionState.Closed)
            {
            retval=true;
            }
            con.Close();
            retval=true;

        }
    catch(Exception ex)

        {  
            throw(ex);
        }
        return retval;
    }
    
    public int ExecuteQuries (string sql)
 
    {
    int value=0;
        try

        {
        con.Open();
        SqlCommand cmd=new SqlCommand(sql,con);
        value=cmd.ExecuteNonQuery();
        
        }
    catch(Exception ex)
        {
            throw(ex);
        }
        finally
        {
        con.Close();
        }
    return value;
    
    
    
    
    }
    
    public int result( string sql)
    {
    int value=0;
        try
        {
            con.Open();
            SqlCommand cmd=new SqlCommand(sql, con);
            value=Convert.ToInt16(cmd.ExecuteScalar());
        
        }
    catch(Exception ex)
        {
        throw(ex);
        }
        finally
        {
        con.Close();
        }
        return value;
    }
    public int ExecuteNonQuries(SqlCommand cmd)
    {
        int i = 0;
        try
        {
            opencon();
            cmd.Connection = con;
            i = cmd.ExecuteNonQuery();

        }
        catch (Exception ex)
            {
                throw(ex);
        }
        

            finally
        {
            con.Close();
        }
        return i;
    }

    public DataSet retdata(SqlCommand cmd, DataSet ds)
    {
        try
        {
            opencon();
            cmd.Connection=con;
            SqlDataAdapter  adp =new SqlDataAdapter(cmd);
            adp.Fill(ds);
        }
    catch( Exception ex)
        {    
   
              throw(ex) ;   
    }
    finally
        {
        con.Close();
        
        }
        return ds;
    }

     public Boolean FillGrid(ref GridView gv,string sql)
    {
        bool value = false;
        try
        {
            SqlDataAdapter da = new SqlDataAdapter(sql, con);
            DataSet ds = new DataSet();
            con.Open();
            da.Fill(ds);
            gv.DataSource = ds;
            gv.DataBind();
            value = true;
        }
        catch (Exception ex)
        {
            throw (ex);
        }
        finally
        {
            con.Close();
        }
        return value;
    }
     public bool FillDropDownList(string sql, ref DropDownList ddl, string TextField, string ValueField)
     {
         bool value = false;
         try
         {
             SqlDataAdapter da = new SqlDataAdapter(sql, con);
             DataSet ds = new DataSet();
             con.Open();
             da.Fill(ds);
             ddl.DataSource = ds;
             ddl.DataTextField = TextField;
             ddl.DataValueField = ValueField;
             ddl.DataBind();
             ddl.Items.Add("-SELECT-");
             ddl.Items.FindByText("-SELECT-").Selected = true;
             value = true;
         }
         catch (Exception ex)
         {
             throw (ex);
         }
         finally
         {
             con.Close();
         }
         return value;
}
 
Share this answer
 
Just Open connection for first time and then keep connection object in Application object and you can reuse connection object for all users.

so in above senario you can keep only one connection alive at a time.
 
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