Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

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 Overrides Function GetChars ( _
	ordinal As Integer, _
	dataIndex As Long, _
	buffer As Char(), _
	bufferIndex As Integer, _
	length As Integer _
) As Long


Type: System.Int32

The zero-based column ordinal.

Type: System.Int64

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

Type: System.Char()

The buffer into which to read the stream of chars.

Type: System.Int32

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

Type: System.Int32

The maximum length to copy into the buffer.

Return Value

Type: System.Int64
The actual number of characters read.


IDataRecord.GetChars(Int32, Int64, Char(), Int32, Int32)

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


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


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


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.

Imports System
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)

                  ' 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. 
                     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.")
   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

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.