DataTableReader.GetChars(Int32, Int64, Char[], Int32, Int32) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Returnerar värdet för den angivna kolumnen som en teckenmatris.
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
Parametrar
- ordinal
- Int32
Den nollbaserade kolumnordningen.
- dataIndex
- Int64
Indexet i fältet som läsåtgärden ska startas från.
- buffer
- Char[]
Bufferten där du kan läsa strömmen av tecken.
- bufferIndex
- Int32
Indexet i bufferten där data ska börja placeras.
- length
- Int32
Den maximala längden som ska kopieras till bufferten.
Returer
Det faktiska antalet tecken som lästs.
Undantag
Indexet som skickades låg utanför intervallet 0 till FieldCount - 1.
Ett försök gjordes att hämta data från en borttagen rad.
Ett försök gjordes att läsa eller komma åt en kolumn i en stängd DataTableReader.
Den angivna kolumnen innehåller inte någon teckenmatris.
Exempel
I följande exempel visas GetChars metoden. Metoden TestGetChars förväntar sig att skickas en DataTableReader fylld med två datakolumner: ett filnamn i den första kolumnen och en matris med tecken i den andra. Dessutom TestGetChars kan du ange buffertstorleken som ska användas när den läser data från teckenmatrisen DataTableReaderi .
TestGetChars skapar en fil som motsvarar varje rad med data i DataTableReader, med hjälp av angivna data i den första kolumnen i DataTableReader som filnamn.
Den här proceduren visar användningen av metoden GetChars som läser data som lagrats i DataTable som en teckenmatris. Andra typer av data gör GetChars att metoden genererar en 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
Kommentarer
GetChars returnerar antalet tillgängliga tecken i fältet. För det mesta är det den exakta längden på fältet. Talet som returneras kan dock vara mindre än fältets sanna längd om GetChars det redan har använts för att hämta tecken från fältet.
Det faktiska antalet lästa tecken kan vara mindre än den begärda längden om slutet av fältet nås. Om du skickar en buffert som är null (Nothing i Visual Basic) returnerar GetChars längden på hela fältet i tecken, inte den återstående storleken baserat på parametern buffertförskjutning.
Inga konverteringar utförs. Därför måste de data som ska hämtas redan vara en teckenmatris eller tvingad till en teckenmatris.