Handling BLOBs

When fetching long string values or binary data, there are methods that you can use to fetch the data in pieces. For binary data, use the GetBytes method, and for string data, use the GetChars method. Otherwise, BLOB data is treated in the same manner as any other data you fetch from the database.

For more information, see “GetBytes method” and “GetChars method”.

StepsIssuing a command that returns a string using the GetChars method

  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()