An Example of Using the Get* Methods phần 1
Số trang: 6
Loại file: pdf
Dung lượng: 24.09 KB
Lượt xem: 12
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
An Example of Using the Get* Methods Lets take a look at an example that reads the ProductID, ProductName, UnitPrice, UnitsInStock, and Discontinued columns from the Products table using the Get* methods.
Nội dung trích xuất từ tài liệu:
An Example of Using the Get* Methods phần 1An Example of Using the Get* MethodsLets 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 Serverint, 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 fivecolumns from the Products table. Table 9.5: Products TABLE COLUMNS, TYPES, AND METHODSCOLUMN SQL SERVER COLUMN GET* C# RETURNNAME TYPE METHOD TYPEProductID int GetInt32() intProductName nvarchar GetString() stringUnitPrice money GetDecimal() decimalUnitsInStock smallint GetInt16() shortDiscontinued bit GetBoolean() boolLets assume that you already have a SqlDataReader object namedproductsSqlDataReader and that it may be used to read the five columns from theProducts table. The following while loop uses the Get* methods and returned C# typesshown 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, eachof which is used to store the result from the Get* method. For example, the productIDvariable is used to store the ProductID column value, and since ProductID is of the SQLServer int type, the appropriate C# type for the productID variable is int. To get theProductID column value as a C# int, you call the GetInt32() method. Similarly, theproductName variable is a C# string that is used to store the ProductName column value.This column is of the nvarchar SQL Server type, and to get the Product-Name columnvalue, the GetString() method is used.Of course, this code depends on your knowing the type of the database column. If youdont know the type of a column, you can get it using Visual Studio .NETs ServerExplorer. For example, Figure 9.1 shows the details of the ProductID column of theProducts table. As you can see, ProductID is an int.Figure 9.1: Obtaining the type of a column using Visual Studio .NETs Server ExplorerBefore closing this section, I will show you how to get the .NET type and database typeof a column using C#. You get the .NET type used to represent a column using theGetFieldType() method of your DataReader object. For example:Console.WriteLine(ProductID .NET type = + productsSqlDataReader.GetFieldType(productIDColPos));This example displays:ProductID .NET type = System.Int32As you can see, the System.Int32 .NET type is used to represent the ProductID column.The System.Int32 .NET type corresponds to the C# int type. You can see this typecorrespondence in Table 9.3, shown earlier.You can get the database type for a column using the GetDataTypeName() method ofyour DataReader object. For example:Console.WriteLine(ProductID database type = + productsSqlDataReader.GetDataTypeName(productIDColPos));This example displays:ProductID database type = intAs you can see, the ProductID column is of the SQL Server int type.Listing 9.2 uses the code examples shown in this section.Listing 9.2: STRONGLYTYPEDCOLUMNVALUES.CS/* StronglyTypedColumnValues.cs illustrates how to read column values as C# types using the Get* methods*/using System;using System.Data;using System.Data.SqlClient;class StronglyTypedColumnValues{ public static void Main() { SqlConnection mySqlConnection = new SqlConnection( server=localhost;database=Northwind;uid=sa;pwd=sa ); SqlCommand mySqlCommand = mySqlConnection.CreateCommand();mySqlCommand.CommandText = SELECT TOP 5 ProductID, ProductName, UnitPrice, + UnitsInStock, Discontinued + FROM Products + ORDER BY ProductID;mySqlConnection.Open();SqlDataReader productsSqlDataReader = mySqlCommand.ExecuteReader();int productIDColPos = productsSqlDataReader.GetOrdinal(ProductID);int productNameColPos = productsSqlDataReader.GetOrdinal(ProductName);int unitPriceColPos = productsSqlDataReader.GetOrdinal(UnitPrice);int unitsInStockColPos = productsSqlDataReader.GetOrdinal(UnitsInStock);int discontinuedColPos = productsSqlDataReader.GetOrdinal(Discontinued);// use the GetFieldType() method of the DataReader object// to obtain the .NET type of a columnConsole.WriteLine(ProductID .NET type = + productsSqlDataReader.GetFieldType(productIDColPos));Console.WriteLine(ProductName .NET type = + productsSqlDataReader.GetFieldType(productNameColPos));Console.WriteLine(UnitPrice .NET type = + productsSqlDataReader.GetFieldType(unitPriceColPos));Console.WriteLine(UnitsInStock .NET type = + productsSqlDataReader.GetFieldType(unitsInStockColPos));Console.WriteLine(Discontinued .NET type = + productsSqlDataReader.GetFieldType(discontinuedColPos));// use the GetDa ...
Nội dung trích xuất từ tài liệu:
An Example of Using the Get* Methods phần 1An Example of Using the Get* MethodsLets 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 Serverint, 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 fivecolumns from the Products table. Table 9.5: Products TABLE COLUMNS, TYPES, AND METHODSCOLUMN SQL SERVER COLUMN GET* C# RETURNNAME TYPE METHOD TYPEProductID int GetInt32() intProductName nvarchar GetString() stringUnitPrice money GetDecimal() decimalUnitsInStock smallint GetInt16() shortDiscontinued bit GetBoolean() boolLets assume that you already have a SqlDataReader object namedproductsSqlDataReader and that it may be used to read the five columns from theProducts table. The following while loop uses the Get* methods and returned C# typesshown 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, eachof which is used to store the result from the Get* method. For example, the productIDvariable is used to store the ProductID column value, and since ProductID is of the SQLServer int type, the appropriate C# type for the productID variable is int. To get theProductID column value as a C# int, you call the GetInt32() method. Similarly, theproductName variable is a C# string that is used to store the ProductName column value.This column is of the nvarchar SQL Server type, and to get the Product-Name columnvalue, the GetString() method is used.Of course, this code depends on your knowing the type of the database column. If youdont know the type of a column, you can get it using Visual Studio .NETs ServerExplorer. For example, Figure 9.1 shows the details of the ProductID column of theProducts table. As you can see, ProductID is an int.Figure 9.1: Obtaining the type of a column using Visual Studio .NETs Server ExplorerBefore closing this section, I will show you how to get the .NET type and database typeof a column using C#. You get the .NET type used to represent a column using theGetFieldType() method of your DataReader object. For example:Console.WriteLine(ProductID .NET type = + productsSqlDataReader.GetFieldType(productIDColPos));This example displays:ProductID .NET type = System.Int32As you can see, the System.Int32 .NET type is used to represent the ProductID column.The System.Int32 .NET type corresponds to the C# int type. You can see this typecorrespondence in Table 9.3, shown earlier.You can get the database type for a column using the GetDataTypeName() method ofyour DataReader object. For example:Console.WriteLine(ProductID database type = + productsSqlDataReader.GetDataTypeName(productIDColPos));This example displays:ProductID database type = intAs you can see, the ProductID column is of the SQL Server int type.Listing 9.2 uses the code examples shown in this section.Listing 9.2: STRONGLYTYPEDCOLUMNVALUES.CS/* StronglyTypedColumnValues.cs illustrates how to read column values as C# types using the Get* methods*/using System;using System.Data;using System.Data.SqlClient;class StronglyTypedColumnValues{ public static void Main() { SqlConnection mySqlConnection = new SqlConnection( server=localhost;database=Northwind;uid=sa;pwd=sa ); SqlCommand mySqlCommand = mySqlConnection.CreateCommand();mySqlCommand.CommandText = SELECT TOP 5 ProductID, ProductName, UnitPrice, + UnitsInStock, Discontinued + FROM Products + ORDER BY ProductID;mySqlConnection.Open();SqlDataReader productsSqlDataReader = mySqlCommand.ExecuteReader();int productIDColPos = productsSqlDataReader.GetOrdinal(ProductID);int productNameColPos = productsSqlDataReader.GetOrdinal(ProductName);int unitPriceColPos = productsSqlDataReader.GetOrdinal(UnitPrice);int unitsInStockColPos = productsSqlDataReader.GetOrdinal(UnitsInStock);int discontinuedColPos = productsSqlDataReader.GetOrdinal(Discontinued);// use the GetFieldType() method of the DataReader object// to obtain the .NET type of a columnConsole.WriteLine(ProductID .NET type = + productsSqlDataReader.GetFieldType(productIDColPos));Console.WriteLine(ProductName .NET type = + productsSqlDataReader.GetFieldType(productNameColPos));Console.WriteLine(UnitPrice .NET type = + productsSqlDataReader.GetFieldType(unitPriceColPos));Console.WriteLine(UnitsInStock .NET type = + productsSqlDataReader.GetFieldType(unitsInStockColPos));Console.WriteLine(Discontinued .NET type = + productsSqlDataReader.GetFieldType(discontinuedColPos));// use the GetDa ...
Tìm kiếm theo từ khóa liên quan:
kĩ thuật lập trình công nghệ thông tin lập trình ngôn ngữ lập trình C Shark C# sybex - c.sharp database programming An Example of Using the Get* Methods phần 1Gợi ý tài liệu liên quan:
-
52 trang 411 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 291 0 0 -
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 286 0 0 -
74 trang 276 0 0
-
96 trang 275 0 0
-
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 265 1 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 261 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 255 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 251 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 245 0 0