我有一个数据库,其中的文件数据存储在图像字段中。我需要能够提取该图像字段中的数据并在磁盘上创建文档。我使用 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);
}
}
将字节读入 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.");
}
}
}