DataTableReader.GetBytes メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DataTableReader.GetBytes メソッドの意味・解説 

DataTableReader.GetBytes メソッド

メモ : このメソッドは、.NET Framework version 2.0新しく追加されたものです。

指定したバッファ オフセット開始位置として、指定したオフセットから始まる文字ストリームバッファ配列として読み込みます。

名前空間: System.Data
アセンブリ: System.Data (system.data.dll 内)
構文構文

例外例外
例外種類条件

ArgumentOutOfRangeException

渡されインデックスが 0 から FieldCount - 1 の範囲にありません。

DeletedRowInaccessibleException

削除した行からデータ取得しようとしました

InvalidOperationException

閉じている DataTableReader の列を読み取るアクセスしようとしました

InvalidCastException

指定した列にはバイト配列含まれていません。

解説解説

GetBytes は、フィールド内の利用可能バイト数を返します。ほとんどの場合、これは正確なフィールド長です。ただし、フィールドからバイト取得するために GetBytes が既に使用されている場合返される数値はそのフィールド正確な長さよりも小さいことがあります。これは、DataTableReader が、大きデータ構造体をバッファ読み込んでいるときなどに起こります

null 参照 (Visual Basic では Nothing)(Visual Basic では Nothing) のバッファを渡すと、GetBytes は、バッファ オフセット パラメータに基づく残りサイズではなくフィールド全体長さバイト単位返します

変換実行されません。そのため、取得するデータバイト配列であるか、バイト配列強制変換できる必要があります

使用例使用例

AdventureWorks サンプル データベースデータ基づいて DataTableReader作成し取得された各イメージを C:\ フォルダ個別ファイルとして保存する例を次に示します。このアプリケーションテストするには、新しコンソール アプリケーション作成し、System.Drawing.dll アセンブリ参照して新しく作成したファイルにこのサンプル コード貼り付けます。

Imports System
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
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);
    }
}

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
DataTableReader クラス
DataTableReader メンバ
System.Data 名前空間
GetChars


このページでは「.NET Framework クラス ライブラリ リファレンス」からDataTableReader.GetBytes メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からDataTableReader.GetBytes メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からDataTableReader.GetBytes メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

DataTableReader.GetBytes メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DataTableReader.GetBytes メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS