DataTableReader.GetChars(Int32, Int64, Char[], Int32, Int32) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會回傳指定欄位的值,作為字元陣列。
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);
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
零基列序數。
- dataIndex
- Int64
欄位內的索引,用來開始讀取操作。
- buffer
- Char[]
用來讀取角色串流的緩衝區。
- bufferIndex
- Int32
緩衝區內的索引,開始放置資料。
- length
- Int32
複製到緩衝區的最大長度。
傳回
實際閱讀的字元數。
例外狀況
通過的指數超出0到 FieldCount -1的範圍。
嘗試從已刪除的資料列中取得資料。
嘗試讀取或存取封閉 DataTableReader.
指定的欄位不包含字元陣列。
範例
以下範例示範此 GetChars 方法。 此 TestGetChars 方法預期會傳遞 DataTableReader 兩個欄位的資料:第一欄為檔案名稱,第二欄為字元陣列。 此外,還 TestGetChars 能指定緩衝區大小,因為它在讀取字元陣列 DataTableReader資料時會使用。
TestGetChars 建立一個檔案,對應 中 DataTableReader每一列的資料,使用第一欄 DataTableReader 提供的資料作為檔名。
此程序展示了讀取資料作為字元陣列的方法的使用GetCharsDataTable。 任何其他類型的資料都會使 GetChars 方法拋出一個 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 已用於取得欄位的字元,回傳的數字可能小於該欄位的真實長度。
若到達欄位末端,實際讀取的字元數可能少於請求長度。 如果你通過一個為空的緩衝區(Nothing 在 Visual Basic),GetChars 會回傳整個欄位的長度(以字元計),而不是根據緩衝區偏移參數的剩餘大小。
不進行任何轉換;因此,要檢索的資料必須已經是字元陣列或可強制到字元陣列。