SqlDataReader class
You can read rows from a SQL Server database with the SqlDataReader class object, and read rows from a database that supports OLE DB with the OleDbDataReader class object, such as Oracle and access ; Read rows from an ODBC-enabled database using the OdbcDataReader class object.
The DataReader object allows you to read data in a forward, read-only way, and sometimes the DataReader object is also called a hose cursor. The DataReader object uses a simplified way of reading data, but improves performance while sacrificing a lot of features. such as sorting, paging, and so on that are supported in the dataset. These features will be described in more detail in a later section.
Properties of SqlDataReader
Property |
Description |
Depth |
whose return type is int, gets the value that represents the current line's embedded depth |
FieldCount |
The return type is int to get the number of columns in the current row |
Iscolsed |
The return type is bool, obtaining a Boolean value indicating whether to turn off data reading |
RecordsAffected |
The return type is int, and gets the number of rows that were added, modified, or deleted by the execution SQL statement. |
The method of SqlDataReader
Method |
Description |
Reader () |
The return type is bool, moves the data reader to the next row of the result set, and reads the row. The Boolean value returned by this method indicates whether there are multiple rows in the result set |
GetValue () |
Whose return type is object, returns the value of the specified column |
GetValues () |
The return type is int, and the values of all columns in the current row are copied to the specified object array. The int that this method returns is the number of elements in the array |
NextResult () |
The return type is bool, and the data reader is moved to the next row of the result set. The Boolean value returned by this method indicates whether there are multiple rows in the result set |
Close () off |
SqlDataReader objects |
GetInt32 (), GetChar (), Gatedatatime (), getxxx () |
Returns the value of the specified column, and the returned type is the appropriate data type. For example, GetInt32 () returns the numeric value of an integral type. Note that if you assign a return value to a variable that does not match a type, a InvalidCastException exception is thrown |
Executing a query with the ExecuteReader () method
The following is an example of executing a SELECT statement with the ExecuteReader () method. This method returns the result set with the DataReader object and can then read the rows returned by the database with this object.