我在数据库中的一条记录中存了二进制数据,大概有几十兆。现在不想一次把所有的数据都读到内存里,想分成若干个大小为64K的数据块,再依次读出。请问该如何做

解决方案 »

  1.   

    //我有一个读数据库图片文件的例子,请参考
    using System;
    using System.IO;
    using System.Data;
    using System.Data.SqlClient;class BLOBDemo
    {
    [STAThread]
    static void Main(string[] args)
    {
    SqlConnection cn = new SqlConnection("Data Source = (local);Integrated Security = SSPI;Initial Catalog=pubs");
    SqlCommand cmd = new SqlCommand("Select pub_id,logo FROM pub_info", cn); FileStream fs;
    BinaryWriter bw; //缓冲区大小
    const int bufferSize = 100;
    byte [] outByte = new byte[bufferSize];
    //GetBytes返回的字节数量
    long retval;
    //BLOB输出的起始位置
    long startIndex = 0; string pub_id = ""; cn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SequentialAccess); while(dr.Read())
    {
    pub_id = dr.GetString(0); fs = new FileStream("logo" + pub_id + ".bmp", FileMode.OpenOrCreate, FileAccess.Write);
    bw = new BinaryWriter(fs); startIndex = 0;

    do
    {
    retval = dr.GetBytes(1, startIndex, outByte, 0, bufferSize);
    // Console.WriteLine(retval.ToString());
    bw.Write(outByte);
    bw.Flush();
    startIndex += bufferSize;
    }while(retval == bufferSize); bw.Write(outByte, 0, (int)retval - 1);
    bw.Flush(); bw.Close();
    fs.Close();
    } dr.Close();
    cn.Close();
    }
    }
      

  2.   

    通过上面的反过程,将大数据文件放在一个stream中转化成byte然后附给dataset让dataset帮你保存就行了
      

  3.   

    如果上几十兆的东西,就不要再向数据库存了,ado.net好像没有类似于MemoryStream或者FileStream所提供的方法,即你不能分批去写。
      

  4.   

    这么大的东西楼主都想写进数据库。。比较恐怖.这个贴子介绍的较细。MARK
    http://community.csdn.net/Expert/topic/4756/4756451.xml?temp=.7671015
      

  5.   

    才几十兆算什么。数据库不都是号称一个字段最多能放2G,整个库可以放上TB的数据么。但是2G的数据我总不可能一次都放到byte数据里吧。 
    那个 dr.GetBytes 不知道是不是一次把所有数据全读到内存里,然后再一段段返回给用户。如果是这样,就没有什么意义了