DataTableReader.GetBytes(Int32, Int64, Byte[], Int32, Int32) Yöntem

Tanım

Belirtilen sütun uzaklığında başlayan bayt akışını, belirtilen arabellek uzaklığından başlayan bir dizi olarak arabelleğe okur.

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

Parametreler

ordinal
Int32

Sıfır tabanlı sütun sıralı.

dataIndex
Int64

Okuma işleminin başlatıldığı alan içindeki dizin.

buffer
Byte[]

Bayt akışının okunduğu arabellek.

bufferIndex
Int32

Verilerin yerleştirildiği arabellek içindeki dizin.

length
Int32

Arabelleğe kopyalanacak maksimum uzunluk.

Döndürülenler

Okunan gerçek bayt sayısı.

Özel durumlar

Geçirilen dizin 0 FieldCount - 1 aralığının dışındaydı.

Silinen bir satırdan veri alma girişiminde bulunuldu.

Kapalı bir içindeki bir sütunu okuma veya sütuna erişme girişiminde bulunuldu DataTableReader.

Belirtilen sütun bayt dizisi içermiyor.

Örnekler

Aşağıdaki örnek AdventureWorks örnek veritabanındaki verileri temel alan bir DataTableReader oluşturur ve alınan her görüntüyü C:\ klasöründeki ayrı bir dosyaya kaydeder. Bu uygulamayı test etmek için yeni bir Konsol uygulaması oluşturun, System.Drawing.dll derlemesine başvurun ve örnek kodu yeni oluşturulan dosyaya yapıştırın.

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

Açıklamalar

GetBytes alandaki kullanılabilir bayt sayısını döndürür. Çoğu zaman bu alanın tam uzunluğudur. Ancak, alandan bayt almak için zaten kullanılmışsa GetBytes , döndürülen sayı alanın gerçek uzunluğundan küçük olabilir. Örneğin, büyük bir veri yapısını bir arabelleğe okurken DataTableReader bu durum söz konusu olabilir

(Nothing Visual Basic'te) olan null bir arabellek geçirirseniz, GetBytes arabellek uzaklığı parametresine göre kalan boyutu değil, alanın tamamının uzunluğunu bayt cinsinden döndürür.

Dönüştürme yapılmaz; bu nedenle, alınan verilerin zaten bir bayt dizisi olması veya bir bayt dizisine zorlanabilmesi gerekir.

Şunlara uygulanır

Ayrıca bkz.