DataTableReader.GetChars(Int32, Int64, Char[], Int32, Int32) 메서드

정의

지정된 열의 값에 해당하는 문자 배열을 반환합니다.

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

버퍼로 복사할 최대 길이입니다.

반환

Int64

읽은 실제 문자 수입니다.

예외

전달된 인덱스가 0에서 FieldCount - 1 사이의 범위에 속하지 않는 경우

삭제된 행에서 데이터를 검색하려고 한 경우

닫힌 DataTableReader의 열을 읽거나 액세스하려고 한 경우

지정된 열에 문자 배열이 없는 경우

예제

다음 예제는 GetChars 메서드. TestGetChars 메서드에 전달할 필요는 DataTableReader 두 개 열의 데이터가 채워진: 첫 번째 열, 두 번째에서 문자의 배열에는 파일 이름입니다. 또한 TestGetChars 문자 배열에서 데이터를 읽고 사용할 버퍼 크기를 지정할 수는 DataTableReader합니다. TestGetChars 데이터의 각 행에 해당 파일을 만듭니다는 DataTableReader, 제공 된 데이터를 사용 하 여 첫 번째 열에는 DataTableReader 파일 이름으로.

이 절차의 사용을 보여 줍니다.는 GetChars 에 저장 된 데이터를 읽는 메서드는 DataTable 문자 배열로 합니다. 다른 유형의 데이터를 사용 하면 합니다 GetChars throw 하는 메서드는 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을 전달 하는 경우 (Nothing Visual Basic에서), GetChars 문자 버퍼 오프셋된 매개 변수에 따라 나머지 크기가 아닌 전체 필드의 길이 반환 합니다.

변환이 수행 되지 않습니다. 따라서 데이터를 검색할 수 있어야 문자 배열 또는 문자 배열에 강제 변환 합니다.

적용 대상