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
中的列。
指定的列不包含字节数组。
示例
以下示例基于 AdventureWorks 示例数据库中的数据创建 DataTableReader ,并将检索到的每个图像保存到 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
已用于从字段中获取字节,则返回的数字可能小于字段的真实长度。 例如,当 将大型数据结构读取到缓冲区中时 DataTableReader ,可能会发生这种情况
如果传递在 Visual Basic) 中 (Nothing
的缓冲区null
,GetBytes
则返回整个字段的长度(以字节为单位),而不是基于缓冲区偏移参数的剩余大小。
不执行任何转换;因此,检索到的数据必须已是字节数组或可强制转换为字节数组。