使用SqlDataReader获取二进制数据

43

我有一个名为Blob(Id(int),Data(Image))的表格。我需要使用SqlDataReader来获取图像数据。请注意,我不想将数据写入浏览器,而只是需要将二进制数据作为byte[]用于一些内部操作。我所能想到的唯一方法是使用SqlDataReader获取id,然后再次使用SqlCommand.ExecuteScalar()来获取给定id的byte[]。是否可以仅使用SqlDataReader(SqlCommand.ExecuteReader)获取该图像数据作为byte[]?我有遗漏什么吗?

7个回答

72
您可以通过以下方式获取:(byte[])reader["Data"]
另外,请注意image数据类型已被弃用,将在未来的SQL Server版本中移除;请改用varbinary(max)

3
在.NET 4.0的SqlDataReader中,我甚至看不到一个名为Items的集合。 - Zack
1
Items集合已经被弃用了吗? - AJ_83

20

是的,你可以使用SqlDataReader.GetBytes方法。在第一次调用中,您可能想要将缓冲区传递为null,以查找有多少数据,然后再使用适当大小的缓冲区进行第二次调用。

您也可以尝试使用索引器并将结果强制转换为字节数组 - 我不确定。值得一试 :)


1
int ndx = rdr.GetOrdinal("<ColumnName>"); if(!rdr.IsDBNull(ndx)) { long size = rdr.GetBytes(ndx, 0, null, 0, 0); //获取数据长度 byte[] values = new byte[size];int bufferSize = 1024; long bytesRead = 0; int curPos = 0; while (bytesRead < size) { bytesRead += rdr.GetBytes(ndx, curPos, values, curPos, bufferSize); curPos += bufferSize; }} - Ashish Gupta
2
@ydobonmai:当你已经有合适大小的缓冲区时,为什么要费心去使用不同的缓冲区大小呢?而且你应该使用bytesRead 而不是 curPos;因为你目前总是假设它读取了一个完整的缓冲区。 - Jon Skeet
1
SqlDataReader.GetBytes()听起来非常高效,因为您可以重复使用相同的数组(如果它具有适当的大小)。因此,您不需要为每个列创建一个新数组,就像(byte[])reader["Data"]会做的那样。 - mmmmmmmm

16
在.NET Framework 4.5中,您可以使用GetStream方法将二进制数据作为流来访问。

7

来自MSDN。不确定为什么以前找不到。

    SqlConnection pubsConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=pubs;");
    SqlCommand logoCMD = new SqlCommand("SELECT pub_id, logo FROM pub_info", pubsConn);

    FileStream fs;                          // Writes the BLOB to a file (*.bmp).
    BinaryWriter bw;                        // Streams the BLOB to the FileStream object.

    int bufferSize = 100;                   // Size of the BLOB buffer.
    byte[] outbyte = new byte[bufferSize];  // The BLOB byte[] buffer to be filled by GetBytes.
    long retval;                            // The bytes returned from GetBytes.
    long startIndex = 0;                    // The starting position in the BLOB output.

    string pub_id = "";                     // The publisher id to use in the file name.

    // Open the connection and read data into the DataReader.
    pubsConn.Open();
    SqlDataReader myReader = logoCMD.ExecuteReader(CommandBehavior.SequentialAccess);

    while (myReader.Read())
    {
      // Get the publisher id, which must occur before getting the logo.
      pub_id = myReader.GetString(0);  

      // Create a file to hold the output.
      fs = new FileStream("logo" + pub_id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
      bw = new BinaryWriter(fs);

      // Reset the starting byte for the new BLOB.
      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)
      {
        bw.Write(outbyte);
        bw.Flush();

        // 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 remaining buffer.
      if(retval > 0) // if file size can divide to buffer size
          bw.Write(outbyte, 0, (int)retval); //original MSDN source had retval-1, a bug
      bw.Flush();

      // Close the output file.
      bw.Close();
      fs.Close();
    }

    // Close the reader and the connection.
    myReader.Close();
    pubsConn.Close();

5
请注意:如果您在执行ExecuteReader调用时没有设置CommandBehavior.SequentialAccess(例如从数据层的其他部分获取读取器),它将对您的内存造成严重影响(为每个调用GetBytes()创建一个大型二进制数组)。仅应使用此解决方案,如此处所示,搭配CommandBehavior.SequentialAccess一起使用。 - Tao
4
有人能就如何决定缓冲区大小提供评论吗?示例使用了100,但为什么不是10,也不是10,000呢?有哪些因素需要考虑? - Dewald Swanepoel

4

使用此函数进行安全且灵活的字节读取:

    /// <summary>
    /// Reads all available bytes from reader
    /// </summary>
    /// <param name="reader"></param>
    /// <param name="ordinal"></param>
    /// <returns></returns>
    private byte[] GetBytes(SqliteDataReader reader, int ordinal)
    {
        byte[] result = null;

        if (!reader.IsDBNull(ordinal))
        {
            long size = reader.GetBytes(ordinal, 0, null, 0, 0); //get the length of data 
            result = new byte[size];
            int bufferSize = 1024;
            long bytesRead = 0;
            int curPos = 0;
            while (bytesRead < size)
            {
                bytesRead += reader.GetBytes(ordinal, curPos, result, curPos, bufferSize);
                curPos += bufferSize;
            }
        }

        return result;
    }

既然您已经有了正确的大小,最好执行 int bufferSize = size > int.MaxValue ? int.MaxValue : (int)size; - marsze
实际上不需要循环。只需使用正确的大小参数一次调用reader.GetBytes:reader.GetBytes(ordinal, 0, result, 0, result.Length)。 - ZunTzu

1
不需要使用读取器。只需使用数据集从数据库中获取值(使用存储过程或任何其他方法),然后将其强制转换为字节(以下代码),并将其存储在字节数组中。您的工作完成了。
byte[] productImage;
productImage = (byte[])ds.Tables[0].Rows[0]["Image"];

0

这是一个旧问题,我曾经使用Anton Bakulev的答案一段时间,直到我遇到了一个情况,我的数据实际上比int curPos可以处理的2GB还要大。当我尝试将bufferIndex参数更改为0时,超出缓冲区大小的任何内容都会返回损坏的数据。(此外,那个微小的缓冲区大小使得加载超过2MB的任何内容都成为了一个真正的痛苦)。

不,您可能不应该在数据库中的单个列中拥有超过2GB的数据。尽量避免这种情况。但以防万一,这里是一个更健壮、更简化的代码版本,作为SqlDataReader扩展方法:

public static byte[] ParseStrictByteArray(this SqlDataReader reader, string columnName)
{
    int colIdx = reader.GetOrdinal(columnName);
    long size = reader.GetBytes(colIdx, 0, null, 0, 0);
    byte[] imageValue = new byte[size];
    // essentially, we are loading all this data in memory, either way... Might as well do it in one swoop if we can
    int bufferSize = (int)Math.Min(int.MaxValue, size); 
    //int.MaxValue = 2,147,483,647 = roughly 2 GB of data, so if the data > 2GB we have to read in chunks
            
    if(size > bufferSize){

        long bytesRead = 0;
        int position = 0;
        //we need to copy the data over, which means we DON'T want a full copy of all the data in memory. 
        //We need to reduce the buffer size (but not too much, as multiple calls to the reader also affect performance a lot)
        bufferSize = 104857600; //this is roughly 100MB
        byte[] buffer = new byte[bufferSize];
        while (bytesRead < size)
        {
            if (size - bytesRead < bufferSize)
                bufferSize = Convert.ToInt32(size - bytesRead);

            bytesRead += reader.GetBytes(colIdx, position, buffer, 0, bufferSize);
            //shift the buffer into the final array
            Array.Copy(buffer, 0, imageValue, position, bufferSize);
            position += bufferSize;
        }
    }
    else 
    {
        //single read into the image buffer
        reader.GetBytes(colIdx, 0, imageValue, 0, bufferSize);
    }

    return imageValue;
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接