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

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


GetChars は、フィールド内で利用可能な文字数を返します。ほとんどの場合、これは正確なフィールド長です。ただし、既に GetChars を使用してフィールドから文字を取得している場合は、返される文字数が、フィールドの実際の長さよりも小さくなることがあります。
フィールドの末尾に達した場合は、実際に読み込まれる文字数が、要求した文字数より少なくなることがあります。null (Microsoft Visual Basic では Nothing) のバッファを渡すと、GetChars は、バッファ オフセット パラメータに基づく残りのサイズではなく、フィールド全体の長さを文字単位で返します。

GetChars メソッドの例を次に示します。TestGetChars メソッドでは、1 番目の列にファイル名、2 番目の列に文字配列が入力された、2 つのデータ列を持つ DataTableReader が渡されることを想定しています。さらに TestGetChars では、DataTableReader で文字配列からデータを読み取るときに使用されるバッファのサイズを指定できます。TestGetChars では DataTableReader の 1 番目の列のデータをファイル名として使用して、DataTableReader の各行のデータに対応するファイルを作成します。
このプロシージャでは、DataTable 内に文字配列として格納されていたデータを読み取るという GetChars の使用方法を示します。他の種類のデータが存在すると、GetChars メソッドは InvalidCastException をスローします。
Imports System Imports System.Data Imports System.IO Module Module1 Private Sub TestGetChars( _ ByVal reader As DataTableReader, ByVal bufferSize As Integer) ' The filename is in column 0, and the contents are in column 1. Const FILENAME_COLUMN As Integer = 0 Const DATA_COLUMN As Integer = 1 Dim buffer() As Char Dim offset As Integer Dim charsRead As Integer Dim fileName As String Dim currentBufferSize As Integer While reader.Read ' Reinitialize the buffer size and the buffer itself. currentBufferSize = bufferSize ReDim buffer(bufferSize - 1) ' For each row, write the data to the specified file. ' First, verify that the FileName column isn't null. If Not reader.IsDBNull(FILENAME_COLUMN) Then ' Get the file name, and create a file with ' the supplied name. fileName = reader.GetString(FILENAME_COLUMN) ' Start at the beginning. offset = 0 Using outputStream As New StreamWriter(fileName, False) Try ' Loop through all the characters in the input field , ' incrementing the offset for the next time. If this ' pass through the loop reads characters, write them to ' the output stream. Do charsRead = Cint(reader.GetChars(DATA_COLUMN, offset, _ buffer, 0, bufferSize)) If charsRead > 0 Then outputStream.Write(buffer, 0, charsRead) offset += charsRead End If Loop While charsRead > 0 Catch ex As Exception Console.WriteLine(fileName & ": " & ex.Message) End Try End Using End If End While Console.WriteLine("Press Enter key to finish.") Console.ReadLine() End Sub Sub Main() Dim table As New DataTable table.Columns.Add("FileName", GetType(System.String)) table.Columns.Add("Data", GetType(System.Char())) table.Rows.Add("File1.txt", "0123456789ABCDEF".ToCharArray) table.Rows.Add("File2.txt", "0123456789ABCDEF".ToCharArray) Dim reader As New DataTableReader(table) TestGetChars(reader, 7) End Sub End Module
using System; using System.Data; using System.IO; class Class1 { static void Main() { DataTable table = new DataTable(); table.Columns.Add("FileName", typeof(string)); table.Columns.Add("Data", typeof(char[])); table.Rows.Add(new object[] { "File1.txt", "0123456789ABCDEF".ToCharArray() }); table.Rows.Add(new object[] { "File2.txt", "0123456789ABCDEF".ToCharArray() }); DataTableReader reader = new DataTableReader(table); TestGetChars(reader, 7); } private static void TestGetChars(DataTableReader reader, int bufferSize) { // The filename is in column 0, and the contents are in column 1. const int FILENAME_COLUMN = 0; const int DATA_COLUMN = 1; char[] buffer; long offset; int charsRead = 0; string fileName; int currentBufferSize = 0; while (reader.Read()) { // Reinitialize the buffer size and the buffer itself. currentBufferSize = bufferSize; buffer = new char[bufferSize]; // For each row, write the data to the specified file. // First, verify that the FileName column isn't null. if (!reader.IsDBNull(FILENAME_COLUMN)) { // Get the file name, and create a file with // the supplied name. fileName = reader.GetString(FILENAME_COLUMN); // Start at the beginning. offset = 0; using (StreamWriter outputStream = new StreamWriter(fileName, false)) { try { // Loop through all the characters in the input field, // incrementing the offset for the next time. If this // pass through the loop reads characters, write them to // the output stream. do { charsRead = (int)reader.GetChars(DATA_COLUMN, offset, buffer, 0, bufferSize); if (charsRead > 0) { outputStream.Write(buffer, 0, charsRead); offset += charsRead; } } while (charsRead > 0); } catch (Exception ex) { Console.WriteLine(fileName + ": " + ex.Message); } } } } Console.WriteLine("Press Enter key to finish."); Console.ReadLine(); } }

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.GetChars メソッドを検索する場合は、下記のリンクをクリックしてください。

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