Last Comment

Calendar

July 2010
SunMonTueWedThuFriSat
 << < > >>
    123
45678910
11121314151617
18192021222324
25262728293031

Who's Online?

Member: 0
Visitor: 1

Announce

rss Syndication

03 Dec 2007 - 06:26:52 am
"Using" keyword in Data Access Base classes..
Introduction

this example demonstrates use of "Using" keyword which disposes all resources used inside it and
in Data Access base classes we don't have to worried about connection and adapter ispose, they will
be disposed automatically immediatly after use..

internal DataTable GetNetworkSettingsByNetworkID(int networkID)
        {
            try
            {
                using (Connection = new MySqlConnection(connectionString))
                {
                    using (MySqlCommand command = new MySqlCommand())
                    {
                        command.CommandText = SQL_GET_NETWORKSETTINGS_BY_NETWORKID;
                        command.CommandType = CommandType.StoredProcedure;
                        command.Connection = Connection;
                        command.Parameters.Add(new MySqlParameter("?_NetworkID", MySqlDbType.Int32));
                        command.Parameters[0].Value = networkID;
                        using (MySqlDataAdapter dataAdapter = new MySqlDataAdapter(command))
                        {
                            using (DataTable dtNetworkSettings = new DataTable())
                            {
                                dataAdapter.Fill(dtNetworkSettings);
                                return dtNetworkSettings;
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                PacketUtils.WriteLogError(ex, "NetworkDA::GetNetworkSettingsByNetworkID");
                throw ex;
            }
            finally
            {
                if (Connection != null)
                {
                    if (Connection.State == ConnectionState.Open)
                        Connection.Close();
                    Connection.Dispose();
                }
            }
        }


justchirag · 169 views · 0 comments
Categories: ASP.NET
Tags: Tags using

Permanent link to full entry

http://patelslab.yourliveblog.com/Patel-s-Lab-b1/Using-keyword-in-Data-Access-Base-classes-b1-p13.htm

Comments

No Comment for this post yet...


Leave a comment

New feedback status: Awaiting moderation





Your URL will be displayed.

 
Please enter the code written in the picture.


Comment text

Options
   (Set cookies for name, email and url)