成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C#修改DataReader默認行為

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

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

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

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

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

以下示例從 Microsoft SQL Server 中的 pubs 示例數據庫中返回發行者 ID 和徽標。發行者 ID (pub_id) 是字符字段,而徽標則是圖形,即 BLOB。請注意,由于必須按順序訪問字段,所以將在訪問徽標之前訪問當前數據行的發行者 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#實現泛型類簡單分析
  3. C# Singleton設計模式淺談
  4. C#對接口成員訪問分析
  5. C#實現插件構架淺析
責任編輯:佚名 來源: 博客園
相關推薦

2025-05-12 03:10:00

接口方法代碼

2009-09-11 12:07:12

C# WinForm控

2021-01-28 05:14:40

C#接口簽名

2009-08-28 16:19:30

C#實現修改動態鏈接庫

2009-08-25 17:15:50

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

2020-06-17 09:01:37

C語言代碼開發

2009-09-02 17:10:45

C#語言入門

2009-08-25 17:21:31

C#索引

2009-09-11 12:31:15

C# WinForm控設置默認值

2009-08-13 17:04:09

C#語言C#程序

2009-08-25 17:59:49

C#入門

2009-08-27 16:11:03

C# delegateC# event

2011-04-07 09:40:57

DataReader鏈接關閉

2009-08-18 10:30:30

C#枚舉

2009-08-26 10:34:15

C#類型C#變量

2009-08-24 11:02:52

C#接口映射

2016-10-13 13:33:41

反射特性c#

2009-08-24 09:55:26

C#接口轉換

2009-08-19 16:50:32

Visual C#C#語言特性

2011-04-07 09:20:12

DataReader關閉問題
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美一级免费 | 91.xxx.高清在线| 日本一二区视频 | 一区二区播放 | 久久激情网 | 粉嫩一区二区三区国产精品 | av日韩精品 | 欧美三级视频 | 久久国产精品首页 | 天天操天天摸天天干 | 久久久毛片 | 中文字幕在线免费观看 | 国产95在线 | 91网站在线看 | 精品国产一区二区国模嫣然 | 一级a性色生活片久久毛片波多野 | 欧美极品少妇xxxxⅹ免费视频 | 中文字幕一区二区三区四区五区 | 久久久青草 | 欧美性久久 | 嫩呦国产一区二区三区av | 精品一区二区三区在线观看 | 日韩三级在线 | 亚洲精品久久久蜜桃 | 国产色在线 | 一区二区av | 黑人精品xxx一区一二区 | 亚洲精品久久久久久久久久久久久 | 欧美日韩一 | 欧美精品一区二区三区蜜桃视频 | 日韩一区二区在线观看 | 天天操天天玩 | 欧美精品区 | 人干人人| 日韩看片 | 日韩一区二区三区在线看 | 午夜成人免费视频 | 欧美一区不卡 | av一级久久| 日韩精品免费播放 | 91久久精|