Đang chuẩn bị liên kết để tải về tài liệu:
An Example of Using the Get* Methods phần 1

Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ

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 9.4 shown earlier. For example the ProductID column is a SQL Server int and looking up that SQL Server type in Table 9.4 you can see you use the GetInt32 method to obtain the column value as a C int. Table 9.5 summarizes the column names SQL Server types Get methods and C return types required to retrieve the five columns from the Products table. Table 9.5 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 9.5 to obtain the column values from productsSqlDataReader while productsSqlDataReader.Read int productID productsSqlDataReader.GetInt32 productIDColPos Console.WriteLine productID productID string productName productsSqlDataReader.GetString productNameColPos Console.WriteLine productName productName decimal unitPrice productsSqlDataReader.GetDecimal unitPriceColPos Console.WriteLine unitPrice unitPrice short unitsInStock productsSqlDataReader.GetInt16 unitsInStockColPos Console.WriteLine unitsInStock unitsInStock bool discontinued productsSqlDataReader.GetBoolean discontinuedColPos Console.WriteLine 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