Professional C# 2008 phần 6

Các triệu lặp đi lặp lại bằng cách sử dụng GetInt32 mất giây. Các phí trong indexer số phát sinh trong khi nhận được các kiểu dữ liệu, gọi đoạn mã tương tự như GetInt32, sau đó đấm bốc (trong ví dụ này unboxing) một số nguyên. Vì vậy, nếu bạn biết giản đồ trước, sẵn sàng để sử dụng các con số khó hiểu thay vì tên cột, | Chapter 26 Data Access The million iterations using Getlnt32 took seconds. The overhead in the numeric indexer is incurred while getting the data type calling the same code as Getlnt32 then boxing and in this instance unboxing an integer. So if you know the schema beforehand are willing to use cryptic numbers instead of column names and can be bothered to use a type-safe function for each and every column access you stand to gain somewhere in the region of a tenfold speed increase over using a textual column name when selecting those million copies of the same column . Needless to say there is a tradeoff between maintainability and speed. If you must use numeric indexers define constants within class scope for each of the columns that you will be accessing. The preceding code can be used to select data from any OLE DB database however there are a number of SQL Serverspecific classes that can be used with the obvious portability tradeoff. The following example is the same as the previous one except that in this instance the OLE DB provider and all references to OLE DB classes have been replaced with their SQL counterparts. The example is in the 04_DataReaderSql directory using System using public class DataReaderSql public static int Main string args string source server local integrated security SSPI database northwind string select SELECT ContactName CompanyName FROM Customers SqlConnection conn new SqlConnection source SqlCommand cmd new SqlCommand select conn SqlDataReader aReader while 0 from 1 0 l return 0 Notice the difference If you re typing this do a global replace on OleDb with Sql change the data source string and recompile. It s that easy The same performance tests were run on the indexers for the SQL provider and this time the numeric indexers were both exactly the same at seconds for the million .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.