Selected range of records from database | C#

The DataAdapter plays a crucial role as a connector between a DataSet and a data source, facilitating the retrieval and storage of data. When working with the DataAdapter, it adds or refreshes rows in the DataSet to match those in the data source. This synchronization is achieved using the DataSet name, and a DataTable named "Table" is created if it does not already exist.

DataTable objects

In ADO.NET, DataTable objects are utilized to represent tables within a DataSet. The DataSet itself is designed to operate in a disconnected mode, meaning it can hold and manipulate data independently of the data source.

DataSet ds = new DataSet();

In certain situations, there may be a need to retrieve only a specific range of rows from the data source. In such cases, it is possible to fill the DataSet from the DataAdapter with only the desired range of rows.

DataAdapter.Fill(Dataset, 5, 3, "tablename");

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.

Full Source C#
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() { InitializeComponent(); } 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); cnn.Open(); sqlAdp = new SqlDataAdapter("select * from users", cnn); cnn.Close(); 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()); } } } }


By specifying the starting index and the number of rows to retrieve, the Fill method of the DataAdapter populates the DataSet with only the desired range of rows. The "Table" parameter specifies the name of the DataTable to be created or refreshed within the DataSet. This approach provides flexibility in retrieving data from the data source, allowing for selective retrieval of specific ranges of rows as needed.