DataTableReader.GetChars(Int32, Int64, Char[], Int32, Int32) Metódus

Definíció

A megadott oszlop értékét adja vissza karaktertömbként.

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

Paraméterek

ordinal
Int32

A nulla alapú oszloprend.

dataIndex
Int64

Azon mező indexe, amelyből az olvasási műveletet el szeretné indítani.

buffer
Char[]

A puffer, amelybe beolvassa a karakterfolyamot.

bufferIndex
Int32

A puffer azon indexe, amelynél el szeretné kezdeni az adatok elhelyezését.

length
Int32

A pufferbe másolandó maximális hossz.

Válaszok

Az olvasási karakterek tényleges száma.

Kivételek

Az átadott index a 0 FieldCount és 1 közötti tartományon kívül volt.

Egy törölt sor adatainak lekérésére tett kísérlet történt.

Egy bezárt DataTableReaderoszlop olvasására vagy elérésére tett kísérletet.

A megadott oszlop nem tartalmaz karaktertömböt.

Példák

Az alábbi példa a metódust GetChars mutatja be. A TestGetChars metódus várhatóan két adatoszlopot fog átadni DataTableReader : az első oszlopban egy fájlnevet, a másodikban pedig egy karaktertömböt. TestGetChars Emellett megadhatja a használni kívánt pufferméretet, amikor beolvassa az adatokat a karaktertömbből a DataTableReader. TestGetCharslétrehoz egy, a fájlnév első oszlopában DataTableReader megadott adatokat használó, az DataTableReaderadatsornak megfelelő fájlt.

Ez az eljárás bemutatja a GetChars karaktertömbben DataTable tárolt adatokat beolvasó metódus használatát. Bármilyen más típusú adat miatt a GetChars metódus egy 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

Megjegyzések

GetChars A mezőben elérhető karakterek számát adja vissza. Legtöbbször ez a mező pontos hossza. A visszaadott szám azonban kisebb lehet, mint a mező valódi hossza, ha GetChars már használták karakterek beolvasására a mezőből.

A beolvasott karakterek tényleges száma kisebb lehet, mint a kért hossz, ha a mező vége el van érve. Ha null értékű puffert ad át (Nothing Visual Basic), GetChars a teljes mező hosszát adja vissza karakterekben, nem pedig a puffereltolási paraméter alapján fennmaradó méretet.

Nem történik átalakítás; ezért a lekérendő adatoknak már karaktertömbnek vagy karaktertömbhöz való kényszerítésnek kell lenniük.

A következőre érvényes: