Selected range of records from database

The DataAdapter serves as a bridge between a DataSet and a data source for retrieving and saving data. The DataAdapter adds or refreshes rows in the DataSet to match those in the data source using the DataSet name, and creates a DataTable named "Table". In ADO.NET, DataTable objects are used to represent the tables in a DataSet. The DataSet is designed to be used in a disconnected mode.

In some situations we need to retrieve data only specific range of rows. In this situations we can fill the Dataset from DataAdapter only that specific range of rows. The following piece of code shows how to fill specific range of rows from DataAdapter to Dataset.

The above code will fill the Dataset starting from 5th row and 3 rows.

5 is the starting row no

3 is no of rows we want to fill.

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

namespace WindowsFormsApplication1
	public partial class Form1 : Form
		SqlConnection cnn ;
		string connectionString = null;
		SqlDataAdapter sqlAdp ;
		DataSet ds = new DataSet();

		public Form1()

		private void button1_Click(object sender, EventArgs e)
			int i;
			connectionString = "Data Source=servername; Initial Catalog=databasename; User ID=userid; Password=password"
			cnn = new SqlConnection(connectionString);
			sqlAdp = new SqlDataAdapter("select * from users", cnn);

			sqlAdp.Fill(ds, 5, 3, "users");

			//5 is starting row no.
			//3 is no of rows to retrieve

			for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
				MessageBox.Show (ds.Tables[0].Rows[i].ItemArray[0].ToString());
} (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About