tailieunhanh - An Example of Using the Get* Methods phần 1

An Example of Using the Get* Methods Let's take a look at an example that reads the ProductID, ProductName, UnitPrice, UnitsInStock, and Discontinued columns from the Products table using the Get* methods. | An Example of Using the Get Methods Let s take a look at an example that reads the ProductID ProductName UnitPrice UnitsInStock and Discontinued columns from the Products table using the Get methods. To figure out which Get method to use to retrieve a particular SQL Server column type you use Table shown earlier. For example the ProductID column is a SQL Server int and looking up that SQL Server type in Table you can see you use the GetInt32 method to obtain the column value as a C int. Table summarizes the column names SQL Server types Get methods and C return types required to retrieve the five columns from the Products table. Table Products TABLE COLUMNS TYPES AND METHODS COLUMN NAME SQL SERVER COLUMN TYPE GET METHOD C RETURN TYPE ProductID int GetInt32 int ProductName nvarchar GetString string UnitPrice money GetDecimal decimal UnitsInStock smallint GetInt16 short Discontinued bit GetBoolean bool Let s assume that you already have a SqlDataReader object named productsSqlDataReader and that it may be used to read the five columns from the Products table. The following while loop uses the Get methods and returned C types shown in Table to obtain the column values from productsSqlDataReader while int productID productIDColPos productID productID string productName productNameColPos productName productName decimal unitPrice unitPriceColPos unitPrice unitPrice short unitsInStock unitsInStockColPos unitsInStock unitsInStock bool discontinued discontinuedColPos discontinued discontinued As you can see five variables of the appropriate type are created in this while loop each of which is used to store the result from the Get method. For example the productID variable is used to

TÀI LIỆU LIÊN QUAN
10    158    1
6    184    1
7    162    1
5    157    1
6    160    1
6    152    1
6    150    1
6    206    1
7    154    1
5    121    1