Share via


DataTableReader.GetChars(Int32, Int64, Char[], Int32, Int32) Metode

Definisi

Mengembalikan nilai kolom yang ditentukan sebagai array karakter.

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

Parameter

ordinal
Int32

Ordinal kolom berbasis nol.

dataIndex
Int64

Indeks dalam bidang untuk memulai operasi baca.

buffer
Char[]

Buffer untuk membaca aliran karakter.

bufferIndex
Int32

Indeks dalam buffer untuk mulai menempatkan data.

length
Int32

Panjang maksimum untuk disalin ke dalam buffer.

Mengembalikan

Jumlah karakter aktual yang dibaca.

Pengecualian

Indeks yang diteruskan berada di luar kisaran 0 hingga FieldCount - 1.

Upaya dilakukan untuk mengambil data dari baris yang dihapus.

Upaya dilakukan untuk membaca atau mengakses kolom dalam keadaan tertutup DataTableReader.

Kolom yang ditentukan tidak berisi array karakter.

Contoh

Contoh berikut menunjukkan GetChars metode . Metode TestGetChars ini mengharapkan untuk dilewatkan yang DataTableReader diisi dengan dua kolom data: nama file di kolom pertama, dan array karakter di kolom kedua. Selain itu, TestGetChars memungkinkan Anda menentukan ukuran buffer yang akan digunakan saat membaca data dari array karakter di DataTableReader. TestGetChars membuat file yang sesuai dengan setiap baris data di DataTableReader, menggunakan data yang disediakan di kolom DataTableReader pertama sebagai nama file.

Prosedur ini menunjukkan penggunaan GetChars metode membaca data yang disimpan dalam DataTable sebagai array karakter. Jenis data lainnya menyebabkan GetChars metode melempar 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

Keterangan

GetChars mengembalikan jumlah karakter yang tersedia di bidang . Sebagian besar waktu ini adalah panjang bidang yang tepat. Namun, angka yang dikembalikan mungkin kurang dari panjang benar bidang jika GetChars telah digunakan untuk mendapatkan karakter dari bidang .

Jumlah karakter aktual yang dibaca bisa kurang dari panjang yang diminta, jika akhir bidang tercapai. Jika Anda melewati buffer yang null (Nothing di Visual Basic), GetChars mengembalikan panjang seluruh bidang dalam karakter, bukan ukuran yang tersisa berdasarkan parameter offset buffer.

Tidak ada konversi yang dilakukan; oleh karena itu data yang akan diambil harus sudah menjadi array karakter atau dapat dikoerksi ke array karakter.

Berlaku untuk