Blog of a Filipino Developer about C#, VB.NET, ASP.NET, Java, PHP, SQL Server, MySql and Oracle RSS 2.0
 Tuesday, August 23, 2005

I saw a post in DevPinoy today asking for a demonstration on the disconnected data paradigm in ADO.NET is. With that i decided to create a sample program to illustrate how this works in ADO.NET. Before I continue i would like first to show you the running example to give you a quick overview.

[ C# Sample ]

using System;
using System.Data;
using System.Data.SqlClient;

namespace DisconnectedDemoCS
{
    class DemoApp
    {
        [STAThread]
        static void Main(string[] args)
        {
            //the connection string to our sql server
            string connectionString = "server=[yourserver];uid=[yourusername];pwd=[yourpassword];database=[yourdatabase]";
            //assign the sql statement to use get data from our database
            string sqlStatement = "Select * from [MyTable]";
            //create a new instance of an SqlConnection and assign our connection string to it
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            //open our connection
            sqlConnection.Open();
            //create a new instance of a dataset object
            DataSet dataSet = new DataSet();
            //assign the sql statement and connectio to use for this operation
            SqlDataAdapter sqlAdapter = new SqlDataAdapter(sqlStatement,sqlConnection);
            //fill our dataset with the data accumulated from the SQL statement
            sqlAdapter.Fill(dataSet);
            //close our connection to the database
            sqlConnection.Close();

            //assign the data from our DataSet to a new DataTable
            DataTable dataTable = dataSet.Tables[0];
            //Loop thru each row in in our datatable
            foreach(DataRow dataRow in dataTable.Rows)
            {
                //loop thru each of the column in our datatable
                foreach(Object rowVal in dataRow.ItemArray)
                {
                    //display the data in the column
                    Console.Write(rowVal.ToString() + ",");
                }
                //force a carriage return
                Console.WriteLine("");
            }
            //wait for the user to initiate program exit
            Console.ReadLine();
        }
    }
}

[ VB.NET ]

Imports System
Imports System.Data
Imports System.Data.SqlClient

Module DemoApp

    Sub Main()

        'the connection string to our sql server
        Dim connectionString As String = "server=[yourserver];uid=[yourusername];pwd=[yourpassword];database=[yourdatabase]"
        'assign the sql statement to use get data from our database
        Dim sqlStatement As String = "Select * from [MyTable]"
        'create a new instance of an SqlConnection and assign our connection string to it
        Dim sqlConnection As SqlConnection = New SqlConnection(connectionString)
        'open our connection
        sqlConnection.Open()
        'create a new instance of a dataset object
        Dim dataSet As DataSet = New DataSet
        'assign the sql statement and connectio to use for this operation
        Dim sqlAdapter As SqlDataAdapter = New SqlDataAdapter(sqlStatement, sqlConnection)
        'fill our dataset with the data accumulated from the SQL statement
        sqlAdapter.Fill(DataSet)
        'close our connection to the database
        sqlConnection.Close()
        'assign the data from our DataSet to a new DataTable
        Dim dataTable As DataTable = dataSet.Tables(0)
        'Loop thru each row in in our datatable
        For Each dataRow As DataRow In dataTable.Rows
            'loop thru each of the column in our datatable
            For Each rowVal As Object In dataRow.ItemArray
                'display the data in the column
                Console.Write(rowVal.ToString() & ",")
            Next
            'force a carriage return
            Console.WriteLine("")
        Next
        'wait for the user to initiate program exit
        Console.ReadLine()
    End Sub
End Module

As you can see on the demonstration above, we can still work with our resultset even if we have closed our connection to the database. This goes to show us how different ADO.NET is from its data access predecessors. Having the ability to manipulate data even if there is no connection present.

You can download the solution this sample here: Disconnected ADO.NET Demo.zip (14.78 KB)

Comments are closed.
On this page
Archive
<December 2008>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2008
Keith Rull
Sign In
Statistics
Total Posts: 260
This Year: 57
This Month: 0
This Week: 0
Comments: 116
Themes
Pick a theme:
Ads
All Content © 2008, Keith Rull
DasBlog theme 'Business' created by Christoph De Baene (delarou)