自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#修改DataReader默認行為

開發(fā) 后端
這里介紹C#修改DataReader默認行為,當訪問 BLOB 字段中的數(shù)據(jù)時,請使用 DataReader 的 GetBytes 類型化訪問器,該訪問器將使用二進制數(shù)據(jù)填充 byte 數(shù)組。

學習C#時,經(jīng)常會遇到C#修改DataReader默認行為問題,這里將介紹C#修改DataReader默認行為問題的解決方法。

DataReader默認行為是在整個數(shù)據(jù)行可用時立即以行的形式加載傳入數(shù)據(jù)。但是,對于二進制大對象 (BLOB) 則需要進行不同的處理,因為它們可能包含數(shù)十億字節(jié)的數(shù)據(jù),而單個行中無法包含如此多的數(shù)據(jù)。Command.ExecuteReader 方法具有一個重載,它將采用 CommandBehavior 參數(shù)來用C#修改DataReader默認行為。您可以將 CommandBehavior.SequentialAccess 傳遞到 ExecuteReader 方法來用C#修改DataReader默認行為,以便讓 DataReader 按照順序在接收到數(shù)據(jù)時立即將其加載,而不是加載數(shù)據(jù)行。這是加載 BLOB 或其他大數(shù)據(jù)結構的理想方案。

在將 DataReader 設置為使用 SequentialAccess 時,務必要注意訪問所返回字段的順序。DataReader 的默認行為是在整個行可用時立即加載該行,這使您能夠在讀取下一行之前按任何順序訪問所返回的字段。但是,當使用 SequentialAccess 時,必須按順序訪問由 DataReader 返回的不同字段。例如,如果查詢返回三個列,其中第三列是 BLOB,則必須在訪問第三個字段中的 BLOB 數(shù)據(jù)之前返回第一個和第二個字段的值。如果在訪問第一個或第二個字段之前訪問第三個字段,則第一個和第二個字段值將不再可用。這是因為 SequentialAccess 已修改 DataReader,使其按順序返回數(shù)據(jù),當 DataReader 已經(jīng)讀取超過特定數(shù)據(jù)時,該數(shù)據(jù)將不可用。

當訪問 BLOB 字段中的數(shù)據(jù)時,請使用 DataReader 的 GetBytes 類型化訪問器,該訪問器將使用二進制數(shù)據(jù)填充 byte 數(shù)組。您可以指定要返回的特定數(shù)據(jù)緩沖區(qū)大小以及從返回的數(shù)據(jù)中讀取的第一個字節(jié)的起始位置。GetBytes 將返回 long 值,它表示所返回的字節(jié)數(shù)。如果向 GetBytes 傳遞空的 byte 數(shù)組,所返回的長值將是 BLOB 中字節(jié)的總數(shù)。您可以選擇將字節(jié)數(shù)組中的某索引指定為所讀取數(shù)據(jù)的起始位置。

以下示例從 Microsoft SQL Server 中的 pubs 示例數(shù)據(jù)庫中返回發(fā)行者 ID 和徽標。發(fā)行者 ID (pub_id) 是字符字段,而徽標則是圖形,即 BLOB。請注意,由于必須按順序訪問字段,所以將在訪問徽標之前訪問當前數(shù)據(jù)行的發(fā)行者 ID。

C#修改DataReader默認行為的代碼:

  1. Dim pubsConn As SqlConnection = New SqlConnection
    (Data 
    Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;)  
  2. Dim logoCMD As SqlCommand = New SqlCommand
    (SELECT pub_id, logo FROM pub_info, pubsConn)  
  3.  
  4. Dim fs As FileStream ' Writes the BLOB to a file (*.bmp).  
  5. Dim bw As BinaryWriter ' Streams the binary data to the FileStream object.  
  6.  
  7. Dim bufferSize As Integer = 100 ' The size of the BLOB buffer.  
  8. Dim outbyte(bufferSize - 1) As Byte ' The BLOB byte() 
    buffer to be filled by GetBytes.  
  9. Dim retval As Long ' The bytes returned from GetBytes.  
  10. Dim startIndex As Long = 0 ' The starting position in the BLOB output.  
  11.  
  12. Dim pub_id As String = ' The publisher id to use in the file name.  
  13.  
  14. ' Open the connection and read data into the DataReader.  
  15. pubsConn.Open()  
  16. Dim myReader As SqlDataReader = logoCMD.ExecuteReader
    (CommandBehavior.SequentialAccess)  
  17.  
  18. Do While myReader.Read()  
  19. ' Get the publisher id, which must occur before getting the logo.  
  20. pub_id = myReader.GetString(0)  
  21.  
  22. ' Create a file to hold the output.  
  23. fs = New FileStream(logo & pub_id & .bmp, FileMode.OpenOrCreate, FileAccess.Write)  
  24. bw = New BinaryWriter(fs)  
  25.  
  26. ' Reset the starting byte for a new BLOB.  
  27. startIndex = 0 
  28.  
  29. ' Read bytes into outbyte() and retain the number of bytes returned.  
  30. retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)  
  31.  
  32. ' Continue reading and writing while there are bytes beyond the size of the buffer.  
  33. Do While retval = bufferSize 
  34. bw.Write(outbyte)  
  35. bw.Flush()  
  36.  
  37. ' Reposition the start index to the end of the last buffer and fill the buffer.  
  38. startIndexstartIndex = startIndex + bufferSize  
  39. retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)  
  40. Loop  
  41.  
  42. ' Write the remaining buffer.  
  43. bw.Write(outbyte)  
  44. bw.Flush()  
  45.  
  46. ' Close the output file.  
  47. bw.Close()  
  48. fs.Close()  
  49. Loop  
  50.  
  51. ' Close the reader and the connection.  
  52. myReader.Close()  
  53. pubsConn.Close()  
  54. [C#]  
  55. SqlConnection pubsConn = new SqlConnection
    (Data 
    Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;);  
  56. SqlCommand logoCMD = new SqlCommand(SELECT pub_id, logo FROM pub_info, pubsConn);  
  57.  
  58. FileStream fs; 
  59. // Writes the BLOB to a file (*.bmp).  
  60. BinaryWriter bw; 
  61. // Streams the BLOB to the FileStream object.  
  62.  
  63. int bufferSize = 100
  64. // Size of the BLOB buffer.  
  65. byte[] outbyte = new byte[bufferSize]; 
  66. // The BLOB byte[] buffer to be filled by GetBytes.  
  67. long retval; // The bytes returned from GetBytes.  
  68. long startIndex = 0
  69. // The starting position in the BLOB output.  
  70.  
  71. string pub_id = ; 
  72. // The publisher id to use in the file name.  
  73.  
  74. // Open the connection and read data into the DataReader.  
  75. pubsConn.Open();  
  76. SqlDataReader myReader = logoCMD.ExecuteReader(CommandBehavior.SequentialAccess);  
  77.  
  78. while (myReader.Read())  
  79. {  
  80. // Get the publisher id, which must occur before getting the logo.  
  81. pub_id = myReader.GetString(0);  
  82.  
  83. // Create a file to hold the output.  
  84. fs = new FileStream(logo + pub_id + .bmp, FileMode.OpenOrCreate, FileAccess.Write);  
  85. bw = new BinaryWriter(fs);  
  86.  
  87. // Reset the starting byte for the new BLOB.  
  88. startIndex = 0;  
  89.  
  90. // Read the bytes into outbyte[] and retain the number of bytes returned.  
  91. retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);  
  92.  
  93. // Continue reading and writing while there are bytes beyond the size of the buffer.  
  94. while (retval == bufferSize)  
  95. {  
  96. bw.Write(outbyte);  
  97. bw.Flush();  
  98.  
  99. // Reposition the start index to the end of the last buffer and fill the buffer.  
  100. startIndex+= bufferSize;  
  101. retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);  
  102. }  
  103.  
  104. // Write the remaining buffer.  
  105. bw.Write(outbyte);  
  106. bw.Flush();  
  107.  
  108. // Close the output file.  
  109. bw.Close();  
  110. fs.Close();  
  111. }  
  112.  
  113. // Close the reader and the connection.  
  114. myReader.Close();  
  115. pubsConn.Close();  

【編輯推薦】

  1. C#泛型支持簡單描述
  2. C#實現(xiàn)泛型類簡單分析
  3. C# Singleton設計模式淺談
  4. C#對接口成員訪問分析
  5. C#實現(xiàn)插件構架淺析
責任編輯:佚名 來源: 博客園
相關推薦

2009-09-11 12:07:12

C# WinForm控

2021-01-28 05:14:40

C#接口簽名

2009-08-28 16:19:30

C#實現(xiàn)修改動態(tài)鏈接庫

2009-08-25 17:15:50

C#隱藏C#重寫C#重載

2009-08-25 17:21:31

C#索引

2009-09-11 12:31:15

C# WinForm控設置默認值

2009-09-02 17:10:45

C#語言入門

2020-06-17 09:01:37

C語言代碼開發(fā)

2011-04-07 09:40:57

DataReader鏈接關閉

2009-08-25 17:59:49

C#入門

2009-08-13 17:04:09

C#語言C#程序

2009-08-27 16:11:03

C# delegateC# event

2009-08-19 16:50:32

Visual C#C#語言特性

2009-08-24 09:55:26

C#接口轉(zhuǎn)換

2009-08-18 10:30:30

C#枚舉

2009-08-24 11:02:52

C#接口映射

2009-08-26 10:34:15

C#類型C#變量

2016-10-13 13:33:41

反射特性c#

2011-04-07 09:20:12

DataReader關閉問題

2009-11-04 12:45:33

ADO.NET Dat
點贊
收藏

51CTO技術棧公眾號