Click here to view and discuss this page in DocCommentXchange. In the future, you will be sent there automatically.

SQL Anywhere 12.0.0 (中文) » SQL Anywhere 服务器 - 编程 » .NET 应用程序编程 » SQL Anywhere .NET 数据提供程序 » 访问和操作数据

 

处理 BLOB

读取长字符串值或二进制数据时,可以使用一些方法分段读取数据。对于二进制数据,可使用 GetBytes 方法;而对于字符串数据,则可使用 GetChars 方法。否则,BLOB 数据的处理方法与从数据库读取的任何其它数据的处理方法相同。

有关详细信息,请参见GetBytes 方法GetChars 方法

 ♦  使用 GetChars 方法发出返回字符串的语句
  1. 声明并初始化一个 Connection 对象。

  2. 打开该连接。

  3. 添加一个 Command 对象以定义并执行一条 SQL 语句。

    SACommand cmd = new SACommand(
        "SELECT int_col, blob_col FROM test", conn );
  4. 调用 ExecuteReader 方法以返回 DataReader 对象。

    SADataReader reader = cmd.ExecuteReader();

    下面的代码读取结果集中的两列。第一列是一个整数 (GetInt32( 0 )),而第二列的类型为 LONG VARCHAR。GetChars 用于从 LONG VARCHAR 列一次读取 100 个字符。



    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. 关闭 DataReader 和 Connection 对象。

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