DataTableReader.GetBytes(Int32, Int64, Byte[], Int32, Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
從指定的資料行位移開始,將位元組的資料流讀入緩衝區,做為從指定的緩衝區位移開始的陣列。
public:
override long GetBytes(int ordinal, long dataIndex, cli::array <System::Byte> ^ buffer, int bufferIndex, int length);
public override long GetBytes (int ordinal, long dataIndex, byte[]? buffer, int bufferIndex, int length);
public override long GetBytes (int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length);
override this.GetBytes : int * int64 * byte[] * int * int -> int64
Public Overrides Function GetBytes (ordinal As Integer, dataIndex As Long, buffer As Byte(), bufferIndex As Integer, length As Integer) As Long
參數
- ordinal
- Int32
以零為基底的資料行序數。
- dataIndex
- Int64
要開始讀取作業之欄位中的索引。
- buffer
- Byte[]
要將位元組資料流讀取到其中的緩衝區。
- bufferIndex
- Int32
緩衝區中的索引,從該處開始放置資料。
- length
- Int32
複製入緩衝區的最大長度。
傳回
讀取的實際位元組數目。
例外狀況
傳遞的索引超出 0 到 FieldCount - 1 的範圍。
嘗試從已刪除的資料列擷取資料。
嘗試在關閉的 DataTableReader
中讀取或存取資料行。
指定的資料行不包含位元組陣列。
範例
下列範例會 DataTableReader 根據AdventureWorks範例資料庫中的數據建立 ,並將擷取的每個影像儲存到 C:\ 資料夾中的個別檔案。 若要測試此應用程式,請建立新的控制台應用程式、參考 System.Drawing.dll 元件,然後將範例程式代碼貼到新建立的檔案中。
using System;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
class Class1
{
[STAThread]
static void Main(string[] args)
{
TestGetBytes();
}
static private void TestGetBytes()
{
// Set up the data adapter, using information from
// the AdventureWorks sample database.
SqlDataAdapter photoAdapter = SetupDataAdapter(
"SELECT ThumbnailPhotoFileName, ThumbNailPhoto " +
"FROM Production.ProductPhoto");
// Fill the DataTable.
DataTable photoDataTable = new DataTable();
photoAdapter.Fill(photoDataTable);
using (DataTableReader reader = new DataTableReader(photoDataTable))
{
while (reader.Read())
{
String productName = null;
try
{
// Get the name of the file.
productName = reader.GetString(0);
// Get the length of the field. Pass null
// in the buffer parameter to retrieve the length
// of the data field. Ensure that the field isn't
// null before continuing.
if (reader.IsDBNull(1))
{
Console.WriteLine(productName + " is unavailable.");
}
else
{
long len = reader.GetBytes(1, 0, null, 0, 0);
// Create a buffer to hold the bytes, and then
// read the bytes from the DataTableReader.
Byte[] buffer = new Byte[len];
reader.GetBytes(1, 0, buffer, 0, (int)len);
// Create a new Bitmap object, passing the array
// of bytes to the constructor of a MemoryStream.
using (Bitmap productImage = new
Bitmap(new MemoryStream(buffer)))
{
String fileName = "C:\\" + productName;
// Save in gif format.
productImage.Save(fileName, ImageFormat.Gif);
Console.WriteLine("Successfully created " + fileName);
}
}
}
catch (Exception ex)
{
Console.WriteLine(productName + ": " + ex.Message);
}
}
}
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
static private SqlDataAdapter SetupDataAdapter(String sqlString)
{
// Assuming all the default settings, create a SqlDataAdapter
// working with the AdventureWorks sample database that's
// available with SQL Server.
String connectionString =
"Data Source=(local);Initial Catalog=AdventureWorks;" +
"Integrated Security=true";
return new SqlDataAdapter(sqlString, connectionString);
}
}
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.IO
Imports System.Drawing.Imaging
Module Module1
Sub Main()
TestGetBytes()
End Sub
Private Sub TestGetBytes()
' Set up the data adapter, using information from
' the AdventureWorks sample database.
Dim photoAdapter As SqlDataAdapter = _
SetupDataAdapter("SELECT ThumbnailPhotoFileName, " & _
"ThumbNailPhoto FROM Production.ProductPhoto")
' Fill the DataTable.
Dim photoDataTable As New DataTable
photoAdapter.Fill(photoDataTable)
' Create the DataTableReader.
Using reader As DataTableReader = New DataTableReader(photoDataTable)
Dim buffer() As Byte
Dim productName As String
While reader.Read()
Try
' Get the name of the file.
productName = reader.GetString(0)
' Get the length of the field. Pass Nothing
' in the buffer parameter to retrieve the length
' of the data field. Ensure that the field isn't
' null before continuing.
If reader.IsDBNull(1) Then
Console.WriteLine( _
productName & " is unavailable.")
Else
' Retrieve the length of the necessary byte array.
Dim len As Long = reader.GetBytes(1, 0, Nothing, 0, 0)
' Create a buffer to hold the bytes, and then
' read the bytes from the DataTableReader.
ReDim buffer(CInt(len))
reader.GetBytes(1, 0, buffer, 0, CInt(len))
' Create a new Bitmap object, passing the array
' of bytes to the constructor of a MemoryStream.
Using productImage As New Bitmap(New MemoryStream(buffer))
Dim fileName As String = "C:\" & productName
' Save in gif format.
productImage.Save( _
fileName, ImageFormat.Gif)
Console.WriteLine("Successfully created " & _
fileName)
End Using
End If
Catch ex As Exception
Console.WriteLine(productName & ": " & _
ex.Message)
End Try
End While
End Using
Console.WriteLine("Press Enter to finish.")
Console.ReadLine()
End Sub
Private Function SetupDataAdapter( _
ByVal sqlString As String) As SqlDataAdapter
' Assuming all the default settings, create a SqlDataAdapter
' working with the AdventureWorks sample database that's
' available with SQL Server.
Dim connectionString As String = _
"Data Source=(local);" & _
"Initial Catalog=AdventureWorks;" & _
"Integrated Security=true"
Return New SqlDataAdapter(sqlString, connectionString)
End Function
End Module
備註
GetBytes
會傳回欄位中可用的位元組數目。 在大部分情況下,這是欄位的確切長度。 不過,如果 GetBytes
已經用來從欄位元組取得位元組,傳回的數位可能小於欄位的 true 長度。 例如,當將大型數據結構讀取到緩衝區時 DataTableReader ,這可能是這種情況
如果您在 Visual Basic) 中傳遞 null
(Nothing
緩衝區, GetBytes
則會以位元組為單位傳回整個欄位的長度,而不是根據緩衝區位移參數的剩餘大小。
不會執行轉換;因此,擷取的數據必須已經是位元組陣列,或可強制轉換成位元組陣列。