C# 中有没有办法在使用 SQLDataReader.GetBytes 时覆盖 8000 字节限制?

问题描述 投票:0回答:1

我有一个数据库,其中的文件数据存储在图像字段中。我需要能够提取该图像字段中的数据并在磁盘上创建文档。我使用 C# 运行查询、提取数据并在磁盘上创建图像。在本例中,我在数据库中存储了一个 tif 文件。我正在使用 SQLDataReader 从我的 sql 查询中获取数据。我遇到的问题是 sqldatareader.getbytes 仅返回前 8000 个字节。我有数千张不同尺寸的图像。有没有办法超越这个 8000 字节的限制?

这是我的代码。

byte[] byteArray = new byte[57470];

using (sqlCmd = new SqlCommand(strQuery, sqlConn))
{
    sqlDataRead = sqlCmd.ExecuteReader();

    while (sqlDataRead.Read())
    {
        long lngNumBytes = sqlDataRead.GetBytes(sqlDataRead.GetOrdinal("attachment_data"), byteArray,57470);
    }
}
c# sql-server sqldatareader
1个回答
0
投票

将字节读入 byte[] 数组并保留返回的字节数。 当字节数超出缓冲区大小时继续读写。

int bufferSize = 57470;
byte[] byteArray = new byte[bufferSize];

    using (SqlCommand sqlCmd = new SqlCommand(strQuery, sqlConn))
    {
        using (SqlDataReader sqlDataRead = sqlCmd.ExecuteReader())
        {
            while (sqlDataRead.Read())
            {
                // Read the bytes into the byteArray
                long bytesRead = sqlDataRead.GetBytes(sqlDataRead.GetOrdinal("attachment_data"), 0, byteArray, 0, bufferSize);
    
                // Process the bytesRead data (e.g., write to a file or stream)
                // ...
    
                // Print a success message
                Console.WriteLine($"Successfully read {bytesRead} bytes from the SqlDataReader.");
            }
        }

}
© www.soinside.com 2019 - 2024. All rights reserved.