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.
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.
sql = "Your SQL Statement Here , like Select * from product"
You have to replace the string with your realtime variables.
- ADO.NET Connection Object
- ADO.NET SQL Server Connection
- ADO.NET OLEDB Connection
- ADO.NET ODBC Connection
- ADO.NET Command
- ADO.NET ExecuteNonQuery in SqlCommand Object
- ADO.NET ExecuteNonQuery in OleDbCommand Object
- ADO.NET ExecuteScalar in SqlCommand Object
- ADO.NET ExecuteScalar in OleDbCommand Object
- ADO.NET ExecuteReader in SqlCommand Object
- ADO.NET ExecuteReader in OleDbCommand Object
- How to ADO.NET DataReader
- How to ADO.NET OleDbDataReader
- How to Multiple Result Sets in ADO.NET
- Getting Schema Informations from SqlDataReader
- Getting Schema Informations from OleDbDataReader
- What is DataAdapter
- What is SqlDataAdapter
- What is OleDbDataAdapter
- Vb.NET ExecuteReader and ExecuteNonQuery