DataTableReader.GetBytes メソッド
アセンブリ: System.Data (system.data.dll 内)

Public Overrides Function GetBytes ( _ ordinal As Integer, _ dataIndex As Long, _ buffer As Byte(), _ bufferIndex As Integer, _ length As Integer _ ) As Long
Dim instance As DataTableReader Dim ordinal As Integer Dim dataIndex As Long Dim buffer As Byte() Dim bufferIndex As Integer Dim length As Integer Dim returnValue As Long returnValue = instance.GetBytes(ordinal, dataIndex, buffer, bufferIndex, length)
public override long GetBytes ( int ordinal, long dataIndex, byte[] buffer, int bufferIndex, int length )
public: virtual long long GetBytes ( int ordinal, long long dataIndex, array<unsigned char>^ buffer, int bufferIndex, int length ) override
public override function GetBytes ( ordinal : int, dataIndex : long, buffer : byte[], bufferIndex : int, length : int ) : long
戻り値
実際に読み込んだバイト数。


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

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からDataTableReader.GetBytes メソッドを検索する場合は、下記のリンクをクリックしてください。

- DataTableReader.GetBytes メソッドのページへのリンク