SiteMap  | 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.

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

namespace WindowsApplication1
    public partial class Form1 : Form
        public Form1()

        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);

                command = new SqlCommand(sql, connection);
                adapter.SelectCommand = command;

                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 ! ");
} (C) 2016    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.