An Example of Using the Get* Methods phần 2
Số trang: 6
Loại file: pdf
Dung lượng: 30.88 KB
Lượt xem: 10
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:
The GetSql* methods and Sql* types are defined in the System.Data.SqlTypes namespace, and they are specific to SQL Server. In addition, the GetSql* methods are specific to the SqlDataReader class. Using the GetSql
Nội dung trích xuất từ tài liệu:
An Example of Using the Get* Methods phần 2The GetSql* methods and Sql* types are defined in the System.Data.SqlTypesnamespace, and they are specific to SQL Server. In addition, the GetSql* methods arespecific to the SqlDataReader class. Using the GetSql* methods and Sql* types helpsprevent type conversion errors caused by loss of precision in numeric values.The GetSql* methods are also faster than their Get* counterparts. This is because theGetSql*methods dont need to convert between SQL Server types and the standard C#types, which the Get* methods have to do.Tip If you are using SQL Server, always use the GetSql* methods and Sql* types rather than the Get* methods and the standard C# types. I showed you the Get* methods earlier only because they work with non-SQL Server databases.Table 9.6 shows the Sql* types and the values that may be stored in those types. Table 9.6: Sql* TYPESSql* TYPE VALUESSqlBinary A variable-length string of binary data.SqlBoolean An integer with either a 1 or 0 value.SqlByte An 8-bit unsigned integer value between 0 and 28 - 1 (255).SqlDateTimeA date and time between 12:00:00 AM January 1, 1753 and 11:59:59 PM December 31, 9999. This is accurate to 3.33 milliseconds.SqlDecimal Fixed precision and scale numeric value between -1038 + 1 and 1038 - 1.SqlDouble A 64-bit floating-point number between -1.79769313486232E308 and 1.79769313486232E308 with 15 significant figures of precision.SqlGuid A 128-bit integer value (16 bytes) that that is unique across all computers and networks.SqlInt16 A 16-bit signed integer between -215 (-32,768) and 215 - 1 (32,767).SqlInt32 A 32-bit signed integer between-231 (-2,147,483,648) and 231 - 1 (2,147,483,647).SqlInt64 A 64-bit signed integer between -263 (-9,223,372,036,854,775,808) and 263 - 1 (9,223,372,036,854,775,807).SqlMoney A currency value between -922,337,203,685,477.5808 and 922,337,203,685,477.5807. This is accurate to 1/10,000th of a currency unit.SqlSingle A 32-bit floating-point number between -3.402823E38 and 3.402823E38 with seven significant figures of precision.SqlString A variable-length string of characters.Table 9.7 shows the SQL server types, the corresponding Sql* types, and the GetSql*methods used to read a column as the Sql* type. Table 9.7: SQL SERVER TYPES, COMPATIBLE Sql* TYPES, AND GetSql* METHODSSQL SERVER TYPE Sql* TYPE GetSql* METHODbigint SqlInt64 GetSqlInt64()int SqlInt32 GetSqlInt32()smallint SqlInt16 GetSqlInt16()tinyint SqlByte GetSqlByte()bit SqlBoolean GetSqlBoolean()decimal SqlDecimal GetSqlDecimal()numeric SqlDecimal GetSqlDecimal()money SqlMoney GetSqlMoney()smallmoney SqlMoney GetSqlMoney()float SqlDouble GetSqlDouble()real SqlSingle GetSqlSingle()datetime SqlDateTime GetSqlDateTime()smalldatetime SqlDateTime GetSqlDateTime()char SqlString GetSqlString()varchar SqlString GetSqlString()text SqlString GetSqlString()nchar SqlString GetSqlString()nvarchar SqlString GetSqlString()ntext SqlString GetSqlString()binary SqlBinary GetSqlBinary()varbinary SqlBinary GetSqlBinary()image SqlBinary GetSqlBinary()sql_varient object GetSqlValue()timestamp SqlBinary GetSqlBinary()uniqueidentifier SqlGuid GetSqlGuid()Next youll see how to use some of the methods shown in Table 9.7.An Example of Using the GetSql* MethodsLets take a look at an example that reads the ProductID, ProductName, UnitPrice,UnitsInStock, and Discontinued columns from the Products table using the GetSql*methods.To figure out which GetSql* method to use to retrieve a particular column type, you useTable 9.7, shown earlier. For example, the ProductID column is a SQL Server int, andlooking up that type in Table 9.7, you can see you use the GetSqlInt32() method to obtainthe column value as a C# SqlInt32. Table 9.8 summarizes the column names, SQL Servertypes, GetSql* methods, and Sql* return types for the columns retrieved from theProducts table. Table 9.8: Products TABLE COLUMNS, TYPES, AND GetSql* METHODSCOLUMN ...
Nội dung trích xuất từ tài liệu:
An Example of Using the Get* Methods phần 2The GetSql* methods and Sql* types are defined in the System.Data.SqlTypesnamespace, and they are specific to SQL Server. In addition, the GetSql* methods arespecific to the SqlDataReader class. Using the GetSql* methods and Sql* types helpsprevent type conversion errors caused by loss of precision in numeric values.The GetSql* methods are also faster than their Get* counterparts. This is because theGetSql*methods dont need to convert between SQL Server types and the standard C#types, which the Get* methods have to do.Tip If you are using SQL Server, always use the GetSql* methods and Sql* types rather than the Get* methods and the standard C# types. I showed you the Get* methods earlier only because they work with non-SQL Server databases.Table 9.6 shows the Sql* types and the values that may be stored in those types. Table 9.6: Sql* TYPESSql* TYPE VALUESSqlBinary A variable-length string of binary data.SqlBoolean An integer with either a 1 or 0 value.SqlByte An 8-bit unsigned integer value between 0 and 28 - 1 (255).SqlDateTimeA date and time between 12:00:00 AM January 1, 1753 and 11:59:59 PM December 31, 9999. This is accurate to 3.33 milliseconds.SqlDecimal Fixed precision and scale numeric value between -1038 + 1 and 1038 - 1.SqlDouble A 64-bit floating-point number between -1.79769313486232E308 and 1.79769313486232E308 with 15 significant figures of precision.SqlGuid A 128-bit integer value (16 bytes) that that is unique across all computers and networks.SqlInt16 A 16-bit signed integer between -215 (-32,768) and 215 - 1 (32,767).SqlInt32 A 32-bit signed integer between-231 (-2,147,483,648) and 231 - 1 (2,147,483,647).SqlInt64 A 64-bit signed integer between -263 (-9,223,372,036,854,775,808) and 263 - 1 (9,223,372,036,854,775,807).SqlMoney A currency value between -922,337,203,685,477.5808 and 922,337,203,685,477.5807. This is accurate to 1/10,000th of a currency unit.SqlSingle A 32-bit floating-point number between -3.402823E38 and 3.402823E38 with seven significant figures of precision.SqlString A variable-length string of characters.Table 9.7 shows the SQL server types, the corresponding Sql* types, and the GetSql*methods used to read a column as the Sql* type. Table 9.7: SQL SERVER TYPES, COMPATIBLE Sql* TYPES, AND GetSql* METHODSSQL SERVER TYPE Sql* TYPE GetSql* METHODbigint SqlInt64 GetSqlInt64()int SqlInt32 GetSqlInt32()smallint SqlInt16 GetSqlInt16()tinyint SqlByte GetSqlByte()bit SqlBoolean GetSqlBoolean()decimal SqlDecimal GetSqlDecimal()numeric SqlDecimal GetSqlDecimal()money SqlMoney GetSqlMoney()smallmoney SqlMoney GetSqlMoney()float SqlDouble GetSqlDouble()real SqlSingle GetSqlSingle()datetime SqlDateTime GetSqlDateTime()smalldatetime SqlDateTime GetSqlDateTime()char SqlString GetSqlString()varchar SqlString GetSqlString()text SqlString GetSqlString()nchar SqlString GetSqlString()nvarchar SqlString GetSqlString()ntext SqlString GetSqlString()binary SqlBinary GetSqlBinary()varbinary SqlBinary GetSqlBinary()image SqlBinary GetSqlBinary()sql_varient object GetSqlValue()timestamp SqlBinary GetSqlBinary()uniqueidentifier SqlGuid GetSqlGuid()Next youll see how to use some of the methods shown in Table 9.7.An Example of Using the GetSql* MethodsLets take a look at an example that reads the ProductID, ProductName, UnitPrice,UnitsInStock, and Discontinued columns from the Products table using the GetSql*methods.To figure out which GetSql* method to use to retrieve a particular column type, you useTable 9.7, shown earlier. For example, the ProductID column is a SQL Server int, andlooking up that type in Table 9.7, you can see you use the GetSqlInt32() method to obtainthe column value as a C# SqlInt32. Table 9.8 summarizes the column names, SQL Servertypes, GetSql* methods, and Sql* return types for the columns retrieved from theProducts table. Table 9.8: Products TABLE COLUMNS, TYPES, AND GetSql* METHODSCOLUMN ...
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 2Gợi ý tài liệu liên quan:
-
52 trang 429 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 311 0 0 -
74 trang 294 0 0
-
96 trang 290 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 288 0 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 278 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 272 0 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 271 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 269 1 0 -
Bài thuyết trình Ngôn ngữ lập trình: Hệ điều hành Window Mobile
30 trang 262 0 0