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


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

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

辞書ショートカット

すべての辞書の索引

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

   

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



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

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

©2025 GRAS Group, Inc.RSS