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.

To issue a statement 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.

    SACommand cmd = new SACommand(
        "SELECT int_col, blob_col FROM test", conn );
  4. Call the ExecuteReader method to return the DataReader object.

    SADataReader reader = cmd.ExecuteReader();

    The following code reads the two columns from the result set. The first column is an integer (GetInt32( 0 )), while the second column is a LONG VARCHAR. GetChars is used to read 100 characters at a time from the LONG VARCHAR column.

    int length = 100;
    char[] buf = new char[ length ];
    int intValue;
    long dataIndex = 0;
    long charsRead = 0;
    long blobLength = 0;
    while( reader.Read() ) {
        intValue = reader.GetInt32( 0 );
        while ( ( charsRead = reader.GetChars(
            1, dataIndex, buf, 0, length ) ) == ( long )
                length ) {
        dataIndex += length;
    }
    blobLength = dataIndex + charsRead;
    }
  5. Close the DataReader and Connection objects.

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