Sponsored Ad

Saturday, July 23, 2011

Sample Program to Dispose Dataset by Using Block in C#

If you want to dispose dataset object after a certain operations. You can initialize dataset object inside the using block and the dataset object will automatically dispose once the block finish. Even the dataset object will not be available after the using block.

You can run the below code and check the this is one the good way to avoid the headache of disposing dataset.

Sample Program to Dispose Dataset by Using Block in C#

C# Example to create dataset object in using block:

using System;
using System.Data;

namespace DemoConsoleApplication
{
    class DataSet_Using_Block
    {
        static void Main(string[] args)
        {
            //Create a object of dataset in using block
            using (DataSet oDs = new DataSet("DemoDataset"))
            {
                Console.WriteLine("DataSet Created.");
                Console.WriteLine("DataSet Name inside using block:" + oDs.DataSetName);
            }

            try
            {
                //The below code will not compile as oDs is not visible.
               // Console.WriteLine("DataSet Name outside of using block:" + oDs.DataSetName);
            }
            catch(Exception ex)
            {
                Console.WriteLine("Error Message: " + ex.Message );
            }

            Console.ReadLine();
        }
    }
}

No comments:

Post a Comment

Sponsored Ad

Development Updates