Find Tables in a Dataset - Sql Server

The DataSet contains DataTableCollection and their DataRelationCollection . The DataTableCollection contains zero or more DataTable objects. The Dataset may comprise data for one or more members, corresponding to the number of rows. The SqlDataAdapter Object allows us to populate DataTables in a DataSet. Build and fill each DataTable in a DataSet with data from a data source using a DataAdapter.

ADO.NET enables you to create DataTable objects and add them to an existing DataSet. In some situations we have to find the number of tables resides in a Dataset Object . The following C# source code shows how to find the 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();
            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;
                adapter.Fill(ds, "SQL Temp Table");

                foreach (DataTable tables in ds.Tables)
                    MessageBox.Show (tables.TableName);

            catch (Exception ex)
                MessageBox.Show("Can not open connection ! ");
} (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About