DataTableReader.GetBytes(Int32, Int64, Byte[], Int32, Int32) Methode

Definition

Liest beginnend am jeweiligen Pufferoffset einen Stream von Bytes aus dem angegebenen Spaltenoffset als Array in den Puffer.Reads a stream of bytes starting at the specified column offset into the buffer as an array starting at the specified buffer offset.

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);
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

Parameter

ordinal
Int32

Die nullbasierte Ordnungszahl der Spalte.The zero-based column ordinal.

dataIndex
Int64

Der Index in dem Feld, in dem der Lesevorgang beginnen soll.The index within the field from which to start the read operation.

buffer
Byte[]

Der Puffer, in den der Bytedatenstrom gelesen werden soll.The buffer into which to read the stream of bytes.

bufferIndex
Int32

Der Index innerhalb des Puffers, an dem mit dem Platzieren der Daten begonnen werden soll.The index within the buffer at which to start placing the data.

length
Int32

Die maximale Länge, die in den Puffer kopiert werden soll.The maximum length to copy into the buffer.

Gibt zurück

Die tatsächlich gelesene Anzahl von Bytes.The actual number of bytes read.

Ausnahmen

Der übergebene Index lag außerhalb des Bereichs von 0 bis FieldCount -1.The index passed was outside the range of 0 to FieldCount - 1.

Es wurde versucht, Daten aus einer gelöschten Zeile abzurufen.An attempt was made to retrieve data from a deleted row.

Es wurde versucht, eine Spalte in einem geschlossenen DataTableReader zu lesen oder darauf zuzugreifen.An attempt was made to read or access a column in a closed DataTableReader.

Die angegebene Spalte enthält kein Bytearray.The specified column does not contain a byte array.

Beispiele

Im folgenden Beispiel wird eine DataTableReader auf der Grundlage von Daten in der AdventureWorks-Beispieldatenbank erstellt, und jedes abgerufene Abbild wird in einer separaten Datei in C:\ gespeichert. Pfalz.The following example creates a DataTableReader based on data in the AdventureWorks sample database, and saves each image retrieved to a separate file in the C:\ folder. Um diese Anwendung zu testen, erstellen Sie eine neue Konsolenanwendung, verweisen auf die Assembly "System. Drawing. dll" und fügen den Beispielcode in die neu erstellte Datei ein.In order to test this application, create a new Console application, reference the System.Drawing.dll assembly, and paste the sample code into the newly created file.

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

Hinweise

GetBytes gibt die Anzahl der verfügbaren Bytes im Feld zurück.GetBytes returns the number of available bytes in the field. In den meisten Fällen ist dies die genaue Länge des Felds.Most of the time this is the exact length of the field. Die zurückgegebene Zahl kann jedoch kleiner als die tatsächliche Länge des Felds sein, wenn GetBytes bereits zum Abrufen von Bytes aus dem Feld verwendet wurde.However, the number returned may be less than the true length of the field if GetBytes has already been used to obtain bytes from the field. Dies kann z. b. der Fall sein, wenn das DataTableReader eine große Datenstruktur in einen Puffer liest.This may be the case, for example, when the DataTableReader is reading a large data structure into a buffer

Wenn Sie einen Puffer übergeben, der null (Nothing in Visual Basic) ist, gibt GetBytes die Länge des gesamten Felds in Bytes zurück, nicht die verbleibende Größe basierend auf dem Puffer offset Parameter.If you pass a buffer that is null (Nothing in Visual Basic), GetBytes returns the length of the entire field in bytes, not the remaining size based on the buffer offset parameter.

Es werden keine Konvertierungen ausgeführt. Daher müssen die abgerufenen Daten bereits ein Bytearray sein oder in ein Bytearray umgewandelt werden können.No conversions are performed; therefore the data retrieved must already be a byte array or coercible to a byte array.

Gilt für:

Siehe auch