DataTableReader.GetBytes(Int32, Int64, Byte[], Int32, Int32) Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
A megadott oszlopeltolástól kezdődő bájtfolyamot beolvassa a pufferbe tömbként a megadott puffereltolástól kezdve.
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
Paraméterek
- ordinal
- Int32
A nulla alapú oszloprend.
- dataIndex
- Int64
Azon mező indexe, amelyből az olvasási műveletet el szeretné indítani.
- buffer
- Byte[]
A puffer, amelybe beolvassa a bájtok adatfolyamát.
- bufferIndex
- Int32
A puffer azon indexe, amelynél el szeretné kezdeni az adatok elhelyezését.
- length
- Int32
A pufferbe másolandó maximális hossz.
Válaszok
Az olvasási bájtok tényleges száma.
Kivételek
Az átadott index a 0 FieldCount és 1 közötti tartományon kívül volt.
Egy törölt sor adatainak lekérésére tett kísérlet történt.
Egy bezárt DataTableReaderoszlop olvasására vagy elérésére tett kísérletet.
A megadott oszlop nem tartalmaz bájttömböt.
Példák
Az alábbi példa létrehoz egy DataTableReader , az AdventureWorks-mintaadatbázisban lévő adatokon alapuló képet, és menti a beolvasott képeket egy külön fájlba a C:\ mappában. Az alkalmazás teszteléséhez hozzon létre egy új konzolalkalmazást, hivatkozzon a System.Drawing.dll szerelvényre, és illessze be a mintakódot az újonnan létrehozott fájlba.
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
Megjegyzések
GetBytes A mezőben elérhető bájtok számát adja vissza. Legtöbbször ez a mező pontos hossza. A visszaadott szám azonban kisebb lehet, mint a mező valódi hossza, ha GetBytes már felhasználták bájtok lekérésére a mezőből. Ez lehet például akkor, ha a DataTableReader rendszer nagy adatstruktúrát olvas be egy pufferbe
Ha olyan puffert ad át, amely null (Nothing Visual Basic), GetBytes a teljes mező hosszát adja vissza bájtban, nem pedig a puffereltolási paraméter alapján fennmaradó méretet.
Nem történik átalakítás; ezért a lekért adatoknak már bájttömbnek vagy bájttömbre kényszerítőnek kell lenniük.