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

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

DataTableReader.GetChars メソッド

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

指定した列の値を文字配列として返します。

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

例外例外
例外の種類条件

ArgumentOutOfRangeException

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

DeletedRowInaccessibleException

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

InvalidOperationException

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

InvalidCastException

指定した列には文字配列が含まれていません。

解説解説
使用例使用例

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();
    }
}
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

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

   

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



DataTableReader.GetChars メソッドのページの著作権

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

©2026 GRAS Group, Inc.RSS