Display Selected range of records in VB.NET

The DataAdapter plays a crucial role as a connector between a DataSet, which represents an in-memory cache of data, and a data source. It facilitates the retrieval and saving of data between these two entities.

When creating an instance of a DataAdapter, the properties related to reading and writing data are set to their initial values. These properties define how the DataAdapter interacts with the data source and the DataSet.

DataSet

A DataSet consists of a collection of tables, relationships, and constraints. In the ADO.NET framework, DataTable objects are utilized to represent individual tables within a DataSet. These DataTables store the actual data retrieved from the data source or modified within the application.

Dim ds As New DataSet

One of the key features of a DataSet is its ability to function in a disconnected mode. This means that once the data is retrieved from the data source and stored in the DataSet, the connection to the data source can be closed. The DataSet retains the data in memory and can be manipulated and queried without being directly connected to the data source.

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

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

  1. 5 is the starting row no.
  2. 3 is no of rows we want to fill.

In certain situations, there may be a need to retrieve only a specific range of rows from the data source. In such cases, the DataAdapter can be configured to fill the DataSet with only the desired range of rows. This selective data retrieval allows for optimized performance and reduces the amount of data transferred between the data source and the application.

The following code snippet demonstrates how to fill a specific range of rows from a DataAdapter into a DataSet:

Full Source VB.NET
imports System.IO Imports System.Data.SqlClient Public Class Form1 Dim cnn As SqlConnection Dim connectionString As String Dim sqlAdp As SqlDataAdapter Dim ds As New DataSet Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer 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 To ds.Tables(0).Rows.Count - 1 MsgBox(ds.Tables(0).Rows(i).Item(0)) Next End Sub End Class