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

定義

指定したバッファー オフセットを開始位置として、指定した列オフセットから始まる文字のストリームをバッファーに配列として読み込みます。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

パラメーター

ordinal
Int32 Int32 Int32 Int32

0 から始まる列序数。The zero-based column ordinal.

dataIndex
Int64 Int64 Int64 Int64

読み取り操作を開始する位置を示すフィールド内のインデックス。The index within the field from which to start the read operation.

buffer
Byte[]

バイトのストリームを読み込むバッファー。The buffer into which to read the stream of bytes.

bufferIndex
Int32 Int32 Int32 Int32

バッファー内でデータの配置を開始するインデックス。The index within the buffer at which to start placing the data.

length
Int32 Int32 Int32 Int32

バッファーにコピーする最大長。The maximum length to copy into the buffer.

戻り値

実際に読み取られたバイト数。The actual number of bytes read.

例外

渡されたインデックスが 0 から FieldCount - 1 の範囲にありません。The index passed was outside the range of 0 to FieldCount - 1.

削除した行からデータを取得しようとしました。An attempt was made to retrieve data from a deleted row.

閉じている DataTableReader の列を読み取るかアクセスしようとしました。An attempt was made to read or access a column in a closed DataTableReader.

指定した列にはバイト配列が含まれていません。The specified column does not contain a byte array.

次の例ではDataTableReader 、AdventureWorks サンプルデータベースのデータに基づいてを作成し、取得した各イメージを C:\ の別のファイルに保存します。フォルダー.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. このアプリケーションをテストするには、新しいコンソールアプリケーションを作成し、system.string アセンブリを参照して、新しく作成したファイルにサンプルコードを貼り付けます。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

注釈

GetBytesフィールドの使用可能なバイト数を返します。GetBytes returns the number of available bytes in the field. ほとんどの場合、フィールドの正確な長さです。Most of the time this is the exact length of the field. ただし、がフィールドからバイトを取得するために既に使用されGetBytesている場合、返される数値は、フィールドの実際の長さよりも小さくなることがあります。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. たとえば、 DataTableReaderが大きなデータ構造体をバッファーに読み込んでいる場合などです。This may be the case, for example, when the DataTableReader is reading a large data structure into a buffer

( null GetBytes Visual Basic) のバッファーを渡した場合、は、バッファーオフセットパラメーターに基づいて、残りのサイズではなく、フィールド全体の長さをバイト単位で返します。NothingIf 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.

変換は実行されません。したがって、取得されるデータは、既にバイト配列であるか、またはバイト配列に対して強制可能である必要があります。No conversions are performed; therefore the data retrieved must already be a byte array or coercible to a byte array.

適用対象

こちらもご覧ください