Net-informations.com
SiteMap  | About    

How to Dataset rows count - Sql Server

The DataSet Object contains copy of the data we requested through the SQL statement. The DataSet consists of DataTableCollection and their DataRelationCollection . The DataTableCollection contains zero or more DataTable objects. A DataSet represents a complete set of data including the tables that contain, order, and constrain the data, as well as the relationships between the tables.

The Dataset may comprise data for one or more members, corresponding to the number of rows. The DataRow Class represents the actual data contained in a table. You use the DataRow and its properties and methods to retrieve, evaluate, and manipulate the data in a table. The following C# source code shows how to find the number of rows in a table that resides in the Dataset.




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 ! ");
            }
        }
    }
}

 CSharp Dataset - Related Contents


 CSharp Dataset - Related Programs