DataTableReader.GetChars(Int32, Int64, Char[], Int32, Int32) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定した列の値を文字配列として返します。
public:
override long GetChars(int ordinal, long dataIndex, cli::array <char> ^ buffer, int bufferIndex, int length);
public override long GetChars(int ordinal, long dataIndex, char[] buffer, int bufferIndex, int length);
override this.GetChars : int * int64 * char[] * int * int -> int64
Public Overrides Function GetChars (ordinal As Integer, dataIndex As Long, buffer As Char(), bufferIndex As Integer, length As Integer) As Long
パラメーター
- ordinal
- Int32
0 から始まる列の序数。
- dataIndex
- Int64
読み取り操作を開始するフィールド内のインデックス。
- buffer
- Char[]
文字のストリームを読み取るバッファー。
- bufferIndex
- Int32
データの配置を開始するバッファー内のインデックス。
- length
- Int32
バッファーにコピーする最大長。
返品
読み取られた実際の文字数。
例外
渡されたインデックスが 0 から FieldCount - 1 の範囲外でした。
削除された行からデータを取得しようとしました。
閉じた DataTableReader内の列の読み取りまたはアクセスが試行されました。
指定した列に文字配列が含まれていません。
例
次の例では、 GetChars メソッドを示します。
TestGetChars メソッドは、データの 2 つの列 (最初の列のファイル名と 2 番目の列の文字の配列) で満たされたDataTableReaderが渡されることを想定しています。 さらに、 TestGetChars では、 DataTableReaderの文字配列からデータを読み取る際に使用するバッファー サイズを指定できます。
TestGetCharsは、DataTableReaderの最初の列に指定されたデータをファイル名として使用して、DataTableReader内のデータの各行に対応するファイルを作成します。
この手順では、DataTableに格納されたデータを文字配列として読み取るGetChars メソッドの使用方法を示します。 その他の種類のデータにより、 GetChars メソッドは InvalidCastExceptionをスローします。
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();
}
}
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
注釈
GetChars は、フィールドで使用可能な文字数を返します。 ほとんどの場合、これはフィールドの正確な長さです。 ただし、 GetChars がフィールドから文字を取得するために既に使用されている場合は、返される数がフィールドの実際の長さよりも小さい場合があります。
フィールドの末尾に達した場合、読み取られる実際の文字数は、要求された長さよりも小さくすることができます。 null (Visual Basic では Nothing) のバッファーを渡すと、GetChars はバッファー オフセット パラメーターに基づく残りのサイズではなく、フィールド全体の長さを文字で返します。
変換は実行されません。したがって、取得するデータは、既に文字配列であるか、文字配列に強制的に変換できる必要があります。