ADO.NET ExecuteReader in SqlCommand Object

The ExecuteReader() method in the SqlCommand Object sends the SQL statements to the Connection Object and populates a SqlDataReader Object based on the SQL statement provided. When the ExecuteReader() method is executed, it instantiates a SqlDataReader Object from the System.Data.SqlClient namespace.

SqlDataReader Object

The SqlDataReader Object is a stream-based, forward-only, read-only mechanism for retrieving query results from the data source. It provides a way to efficiently read and navigate through the result set returned by the SQL statement. The SqlDataReader Object is designed for retrieving data and does not support updating or modifying the data in the data source.

It is important to note that you cannot directly create an instance of the SqlDataReader Object in your code. Instead, it is created by calling the ExecuteReader() method of a Command Object, such as the SqlCommand Object. The ExecuteReader() method initiates the execution of the SQL statement, and once executed, it returns a SqlDataReader Object that you can use to read the retrieved data row by row.

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 cnn As SqlConnection Dim cmd As SqlCommand Dim sql As String Dim reader As SqlDataReader connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" sql = "Your SQL Statement Here , like Select * from product" cnn = New SqlConnection(connetionString) Try cnn.Open() cmd = New SqlCommand(sql, cnn) reader = cmd.ExecuteReader() While reader.Read() MsgBox(reader.Item(0) & " - " & reader.Item(1) & " - " & reader.Item(2)) End While reader.Close() cmd.Dispose() cnn.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.