How to ADO.NET SqlDataReader

The SqlDataReader Object in ADO.NET provides a connection-oriented data access to SQL Server data sources. When you call the ExecuteReader() method in the SqlCommand Object, it sends the SQL statements to the SqlConnection Object and populates a SqlDataReader Object based on the executed SQL statement.

SqlDataReader Object

The SqlDataReader Object is specifically designed for retrieving data from SQL Server databases. It allows you to efficiently read and navigate through the result set returned by the SQL statement. The SqlDataReader Object provides a forward-only and read-only mechanism to access the data rows sequentially.

To obtain a SqlDataReader Object, you need to call the ExecuteReader() method on a SqlCommand Object, passing in the SQL statement to be executed. This method sends the SQL statement to the associated SqlConnection Object and returns a SqlDataReader Object that contains the retrieved data.

Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader()

When the ExecuteReader method is executed in the SqlCommand Object, it instantiates a SqlDataReader Object from the SqlClient namespace. This SqlDataReader Object represents the result set returned by the executed SQL statement.

To read data from the SqlDataReader Object, it is important to note that the SqlDataReader should always be open and positioned before the first record. This means that you need to call the Read() method on the SqlDataReader Object to advance to the first row of data before accessing any data values.

The Read() method in the SqlDataReader Object is used to move the reader to the next row and retrieve the data from that row. Each time you call the Read() method, it advances the SqlDataReader Object to the next available row in the result set. If there are no more rows, the Read() method returns false, indicating that there is no additional data to read.

By calling the Read() method in a loop, you can iterate through the result set and access the data from each row. Each time the Read() method is called, it moves the SqlDataReader Object to the next row, allowing you to retrieve the data values using the appropriate GetXXX methods, where XXX represents the data type of the column.

SqlDataReader.Read()
Full Source VB.NET
Imports System.Data.SqlClient Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim connetionString As String Dim sqlCnn As SqlConnection Dim sqlCmd As SqlCommand Dim sql As String connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" sql = "Your SQL Statement Here , like Select * from product" sqlCnn = New SqlConnection(connetionString) Try sqlCnn.Open() sqlCmd = New SqlCommand(sql, sqlCnn) Dim sqlReader As SqlDataReader = sqlCmd.ExecuteReader() While sqlReader.Read() MsgBox(sqlReader.Item(0) & " - " & sqlReader.Item(1) & " - " & sqlReader.Item(2)) End While sqlReader.Close() sqlCmd.Dispose() sqlCnn.Close() Catch ex As Exception MsgBox("Can not open connection ! ") End Try End Sub End Class
connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password"
sql = "Your SQL Statement Here , like Select * from product"

You have to replace the string with your realtime variables.