Export (0) Print
Expand All

DataTableReader.GetChars Method

Returns the value of the specified column as a character array.

Namespace: System.Data
Assembly: System.Data (in system.data.dll)

public override long GetChars (
	int ordinal,
	long dataIndex,
	char[] buffer,
	int bufferIndex,
	int length
)
public long GetChars (
	int ordinal, 
	long dataIndex, 
	char[] buffer, 
	int bufferIndex, 
	int length
)
public override function GetChars (
	ordinal : int, 
	dataIndex : long, 
	buffer : char[], 
	bufferIndex : int, 
	length : int
) : long
Not applicable.

Parameters

ordinal

The zero-based column ordinal.

dataIndex

The index within the field from which to start the read operation.

buffer

The buffer into which to read the stream of chars.

bufferIndex

The index within the buffer at which to start placing the data.

length

The maximum length to copy into the buffer.

Return Value

The actual number of characters read.

Exception typeCondition

ArgumentOutOfRangeException

The index passed was outside the range of 0 to FieldCount - 1.

DeletedRowInaccessibleException

An attempt was made to retrieve data from a deleted row.

InvalidOperationException

An attempt was made to read or access a column in a closed DataTableReader.

InvalidCastException

The specified column does not contain a character array.

GetChars returns the number of available characters in the field. Most of the time this is the exact length of the field. However, the number returned may be less than the true length of the field if GetChars has already been used to obtain characters from the field.

The actual number of characters read can be less than the requested length, if the end of the field is reached. If you pass a buffer that is null (Nothing in Visual Basic), GetChars returns the length of the entire field in characters, not the remaining size based on the buffer offset parameter.

No conversions are performed; therefore the data to be retrieved must already be a character array or coercible to a character array.

The following example demonstrates the GetChars method. The TestGetChars method expects to be passed a DataTableReader filled with two columns of data: a file name in the first column, and an array of characters in the second. In addition, TestGetChars lets you specify the buffer size to be used as it reads the data from the character array in the DataTableReader. TestGetChars creates a file corresponding to each row of data in the DataTableReader, using the supplied data in the first column of the DataTableReader as the file name.

This procedure demonstrates the use of the GetChars method reading data that was stored in the DataTable as a character array. Any other type of data causes the GetChars method to throw an 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();
    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0

Community Additions

ADD
Show:
© 2014 Microsoft