CSharp.Net-Informations.com
   Home      .Net Framework      VB.NET      C#                                                                      About


  C# Datset with Sql Server Data Provider

The DataSet contains the copy of the data we requested through the SQL statement. The DataSet is a memory-resident representation of data that provides a consistent relational programming model regardless of the data source.

We can use Dataset in combination with SqlDataAdapter Class . Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter. The C# SqlDataAdapter object allows us to populate Data Tables in a DataSet.

         C# Source Code Download           Print Source Code
         C# Datset with Sql Server Data Provider - Download
        
C# Tutorial

using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection connection ;
            SqlCommand command ;
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataSet ds = new DataSet();
            int i = 0;
            string sql = null;

            connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
            sql = "Your SQL Statement Here";

            connection = new SqlConnection(connetionString);

            try
            {
                connection.Open();
                command = new SqlCommand(sql, connection);
                adapter.SelectCommand = command;
                adapter.Fill(ds);
                adapter.Dispose();
                command.Dispose();
                connection.Close();

                for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
                {
                    MessageBox.Show(ds.Tables[0].Rows[i].ItemArray[0] + " -- " + ds.Tables[0].Rows[i].ItemArray[1]);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! ");
            }
        }
    }
}



CSharp Dataset Related Contents
*     What is C# ADO.NET Dataset
*     C# Datset with OLEDB Data Provider
*     Find Tables in a Dataset - Sql Server
*     Find Tables in a Dataset - OLEDB
*     How to Dataset rows count - Sql Server
*     How to Dataset rows count - OLEDB
*     How to find Column Definition SqlServer
*     How to find Column Definition OLEDB
*     How to Dyanamic Dataset in C#
*     C# Dataset with multiple tables - Sql Server
*     C# Dataset with multiple tables - OLEDB
*     C# Dataset table relations
*     C# Dataset merge tables - Sql Server
*     C# Dataset merge tables - OLEDB


   Home      VB.NET      C#
CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
*     C# DataGridView Tutorial
   Home      VB.NET      C#
More Source Code :   
Mail to :  feedback@net-informations.com
  |  Home   |  VB.NET   |  C#   |  SiteMap   |  Terms of Use   |  About   |
net-informations.com (C) 2010
All Rights Reserved. All other trademarks are property of their respective owners.