tailieunhanh - Reading Rows from a SqlDataReader Object
Reading Rows from a SqlDataReader Object You read the rows from a DataReader object using the Read() method. This method returns the Boolean true value when there is another row to read, otherwise it returns false. | Reading Rows from a SqlDataReader Object You read the rows from a DataReader object using the Read method. This method returns the Boolean true value when there is another row to read otherwise it returns false. You can read an individual column value in a row from a DataReader by passing the name of the column in square brackets. For example to read the CustomerlD column you use productsSqlDataReader ProductID . You can also specify the column you want to get by passing a numeric value in brackets. For example productsSqlDataReader 0 also returns the ProductID column value. Tip The difference between these two ways of reading a column value is performance using numeric column positions instead of column names results in faster execution of the code. Let s take a look at two code snippets that illustrate these two ways of reading column values. The first code snippet uses the column names to read the column values while productsSqlDataReader ProductID productsSqlDataReader ProductName productsSqlDataReader UnitPrice productsSqlDataReader Discontinued The second code snippet uses the numeric column positions to read the column values while productsSqlDataReader 0 productsSqlDataReader 1 productsSqlDataReader 2 productsSqlDataReader 3 Although the second code snippet is faster it is less flexible since you have to hard-code the numeric column positions. If the column positions in the SELECT statement are changed you need to change the hard-coded column positions in the code-and this is a maintenance nightmare. Also hard-coding the column positions makes your programs more difficult to read. There is a solution to this problem you can call the GetOrdinal method of your DataReader object. The GetOrdinal method returns the position of a column given its name this position is
đang nạp các trang xem trước