Issuing a Command that Returns a String Using the GetChars Method

Use the GetChars method to issue a command that returns a string.

  1. Declare and initialize a Connection object.
  2. Open the connection.
  3. Add a Command object to define and execute a SQL statement:

    For C#:

    AseCommand cmd = new AseCommand( 
       "select au_id, copy from blurbs", conn );

    For Visual Basic .NET:

    Dim cmd As New AseCommand( _
       "select au_id, copy from blurbs", conn)
  4. Call the ExecuteReader method to return the DataReader object:

    For C#:

    AseDataReader reader = cmd.ExecuteReader();

    For Visual Basic .NET:

    Dim reader As AseDataReader = cmd.ExecuteReader()

    The following code reads the two columns from the result set. The first column is a varchar, while the second column is Text. GetChars is used to read 100 characters at a time from the Text column:

    For C#:

    int length = 100;
    char[] buf = new char[ length ];
    String au_id;
    long dataIndex = 0;
    long charsRead = 0;
    long blobLength = 0;
    while( reader.Read() )
    {
       au_id = reader.GetString(0);
       do 
       {
          charsRead = reader.GetChars(
             1, dataIndex, buf, 0, length);
          dataIndex += length;
          // do something with the chars read	
          //.... some code
          //
          // reinitialize char array
          buf = new char[ length ];
       } while ( charsRead == length );
       blobLength = dataIndex + charsRead;
    }

    For Visual Basic .NET:

    Dim length As Integer = 100
    Dim buf(length) As Char
    Dim au_id As String
    Dim dataIndex As Long = 0
    Dim charsRead As Long = 0
    Dim blobLength As Long = 0
    While reader.Read()
       au_id = reader.GetString(0)
       Do
          charsRead = reader.GetChars( _
             1, dataIndex, buf, 0, length)
          dataIndex = dataIndex + length
          ' do something with the data read
          ' 
          '  use code
          '
          ' reinitialize the char array
          ReDim buf(length)
       Loop While (charsRead = length)
       blobLength = dataIndex + charsRead
    End While
  5. Close the DataReader and Connection objects:

    For C#:

    reader.Close();
    conn.Close();

    For Visual Basic .NET:

    reader.Close()
    conn.Close()