本文介绍了在 C#/SQL 2005 中读取 BLOB 数据的内存有效方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 C# 3.5 读取 SQL 2005 图像字段最有效的内存方法是什么?
What's the most memory effective way to read an SQL 2005 image field using C# 3.5?
现在我有一个 (byte[])cm.ExecuteScalar("...")
.
如果我不能将所有字段内容读入内存,那就太好了.
If I could not read all field content into memory, that would be nice.
推荐答案
See this excellent article here or this blog post for a long explanation how to do it.
基本上,您需要使用 SqlDataReader 并在创建它时为其指定 SequentialAccess
- 然后您可以以最适合您的任何大小的块从数据库中读取(或写入)BLOB.
Basically, you need to use a SqlDataReader and specify SequentialAccess
to it when you create it - then you can read (or write) the BLOB from the database in chunks of whatever size is best for you.
基本上是这样的:
SqlDataReader myReader = getEmp.ExecuteReader(CommandBehavior.SequentialAccess);
while (myReader.Read())
{
int startIndex = 0;
// Read the bytes into outbyte[] and retain the number of bytes returned.
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
// Continue reading and writing while there are bytes beyond the size of the buffer.
while (retval == bufferSize)
{
// write the buffer to the output, e.g. a file
....
// Reposition the start index to the end of the last buffer and fill the buffer.
startIndex += bufferSize;
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize);
}
// write the last buffer to the output, e.g. a file
....
}
// Close the reader and the connection.
myReader.Close();
马克
这篇关于在 C#/SQL 2005 中读取 BLOB 数据的内存有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!