How to Dataset rows count - Sql Server

The DataSet object acts as a container that holds a copy of the data retrieved through a SQL statement or query. It consists of two main components: the DataTableCollection and the DataRelationCollection.


The DataTableCollection is responsible for storing and managing zero or more DataTable objects. Each DataTable represents a structured table of data, including the columns and rows that make up the data set. The DataTables within the DataSet define the structure, order, and constraints of the data.

Furthermore, the DataSet represents a comprehensive set of data that encompasses the tables, their organization, and the relationships between them. It provides a complete representation of the data retrieved from the data source.

The following C# source code shows how to find the number of rows in a table that resides in the Dataset.

Full Source C#
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(); 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, "SQL Temp Table"); adapter.Dispose(); command.Dispose(); connection.Close(); MessageBox.Show ("Number of row(s) - " + ds.Tables[0].Rows.Count); } catch (Exception ex) { MessageBox.Show("Can not open connection ! "); } } } }

Within each DataTable, the actual data is contained in DataRow objects. The DataRow class represents a single row of data within a table. It provides properties and methods that allow you to retrieve, evaluate, and manipulate the data within the table. Using the DataRow class, you can access specific columns, retrieve values, modify data, and perform various operations on individual rows.


Using the DataSet, DataTableCollection, and DataRow class, developers can effectively manage and manipulate data retrieved from a data source. The DataSet offers a versatile and flexible way to work with complete sets of data, allowing for comprehensive data access and manipulation.