Handle database connections with care

Last post 04-22-2008 1:07 AM by ManishKadu. 7 replies.

Sort Posts:

  • Handle database connections with care

    12-19-2004, 11:22 AM
    • Loading...
    • bdesmet
    • Joined on 08-04-2002, 10:39 AM
    • Belgium
    • Posts 1,651
    Please make sure that you always close the underlying connection to a database and that you're using connection pooling to improve the processing speed of database connection creation etc. A few tips:

    1. Use web.config to store the connection string:
    <configuration>
    
    <appSettings>
    <add key="dsn" value="server=server; uid=user; pwd=password; database=db" />
    </appSettings>
    </configuration>

    2. Always use this connection string and don't make modifications to it. If you're not doing this (i.e. using different DSNs) you'll disable the connection pooling feature:
    string dsn = System.Configuration.ConfigurationSettings.AppSettings["dsn"];

    3. Don't apply tricks to try to manage connections etc, just go ahead and use it (no wrapper classes or so involved):
    SqlConnection conn = new SqlConnection(dsn);

    4. Make the time between conn.Open() and conn.Close() as short as possible. E.g. don't do this:
    SqlConnection conn = new SqlConnection(dsn);
    
    conn.Open();
    SqlCommand cmd = new SqlCommand(query, conn);
    cmd.Parameters.Add(...);
    //and more cmd parameters actions etc
    SqlDataReader reader = cmd.ExecuteReader();
    //use the reader and do a lot of other work, not relying on the database
    conn.Close();

    but rather, use:
    SqlConnection conn = new SqlConnection(dsn);
    
    SqlCommand cmd = new SqlCommand(query, conn);
    cmd.Parameters.Add(...);
    //and more cmd parameters actions etc
    conn.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    //use the reader
    conn.Close();
    //do a lot of other work, not relying on the database

    5. Always close the connection when you're done, even when an exception occurs. To do this, use the try...finally pattern:
    SqlConnection conn = new SqlConnection(dsn);
    
    try
    {
    //some init depending on conn
    conn.Open();
    //minimum number of lines of code depending on the open connection
    }
    finally
    {
    if (conn.ConnectionState == ConnectionState.Open)
    conn.Close();
    }

    or the using pattern in C#:
    using (SqlConnection conn = new SqlConnection(dsn))
    
    {
    //...
    conn.Open();
    }

    which will close the db connection automatically when leaving the using block.
    Bart De Smet [MVP]



    Visit www.msdn.be, www.bartdesmet.net
  • Re: Handle database connections with care

    01-24-2008, 3:10 AM
    • Loading...
    • sam_soumya
    • Joined on 01-10-2008, 11:42 AM
    • Posts 76

     Hello sir,

                    It is a nice post .May you please help me for my following database related requirement .

    Let there are two field   1. id--->auto increment or 1 increment serially.

                                      2.name-->varchar

           the requirement is  : if i delete a record ,it will  be  rearrenged.  Like there are 5 records from id 1...to 5.If i delete the 3rd one then the id's will be

    rearrenged again from 1...to 4 . So,what should i do in this case.

     

    Thanks. 

                 
     


     

    Thanks & Regards.
    Soumya
  • Re: Handle database connections with care

    01-24-2008, 5:29 PM
    • Loading...
    • sayyazahmad
    • Joined on 01-17-2008, 11:46 PM
    • US
    • Posts 35
    If you are using SQL Server 2005, you can use Row_Number() instead while retrieving the data.Try this query: Select EmployeeID, EmployeeName,          Row_Number()          Over ( Order by EmployeeID, EmployeeName ) As RowNumber

    From Employees

    Please Mark As Answer if it helps you!
  • Re: Handle database connections with care

    01-24-2008, 11:31 PM
    • Loading...
    • sam_soumya
    • Joined on 01-10-2008, 11:42 AM
    • Posts 76

     Thank you very much Sir for your Reply.It is working fine..

    Thanks & Regards.
    Soumya
  • Re: Handle database connections with care

    02-11-2008, 7:02 PM
    • Loading...
    • deddy_one
    • Joined on 11-01-2007, 10:03 AM
    • Posts 65

    The using pattern in C# is new for me.

    thanks for sharing this

  • Re: Handle database connections with care

    02-17-2008, 2:50 AM

    I'm not entirely understanding point #1. Whose UserName and Passwords would be hard coded in the web.config? Wouldn't each user need to login with their own UserName and Password to gain access? Do you add Keys for each? That would seem very inefficient if you had lots of users.

  • Re: Handle database connections with care

    03-05-2008, 2:16 PM
    • Loading...
    • sudipta
    • Joined on 02-25-2008, 3:33 PM
    • India
    • Posts 340

    Hi bdesmet,

    Nice post. Everybody should always take out some time to make such valuable posts.

    Please click "Mark As Answer" if this hepled in solving your problem.
  • Re: Handle database connections with care

    04-22-2008, 1:07 AM
    • Loading...
    • ManishKadu
    • Joined on 04-22-2008, 5:04 AM
    • Posts 3

    Nice post

    Filed under:
Page 1 of 1 (8 items)