保存以将基因从sqlfilestream降低或弄清楚如何在一个字节阵列中存储大数据



您好,我一直在网上遵循大量的教程,以进行我正在从事的新项目。我正在从FileStream中获取数据,并且在此行中获得了不可记忆的例外:

byte[] buffer = new byte[(int)sfs.Length];

我正在做的是立即使用字节数组,然后想将其保存到光盘中。如果没有一种简单的方法可以避免系统以外的记忆例外,是否有一种方法可以从sqlfilestream避免创建新的字节数组来写入光盘?

        string cs = @”Data Source=<your server>;Initial Catalog=MyFsDb;Integrated Security=TRUE”;
        using (SqlConnection con = new SqlConnection(cs))
        {
            con.Open();
            SqlTransaction txn = con.BeginTransaction();
            string sql = “SELECT fData.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT(), fName FROM MyFsTable”;
            SqlCommand cmd = new SqlCommand(sql, con, txn);
            SqlDataReader rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                string filePath = rdr[0].ToString();
                byte[] objContext = (byte[])rdr[1];
                string fName = rdr[2].ToString();
                SqlFileStream sfs = new SqlFileStream(filePath, objContext, System.IO.FileAccess.Read);
                **byte[] buffer = new byte[(int)sfs.Length];**
                sfs.Read(buffer, 0, buffer.Length);
                sfs.Close();

                string filename = @”C:Temp” + fName;
                System.IO.FileStream fs = new System.IO.FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.Write);
                fs.Write(buffer, 0, buffer.Length);
                fs.Flush();
                fs.Close();
            }
            rdr.Close();
            txn.Commit();
            con.Close();
        }
    }

这是一种可用于从一个 Stream到另一个 Stream读取字节的方法,而无需考虑它们每个类型的Stream

Public Sub CopyStream(source As Stream, destination As Stream, Optional blockSize As Integer = 1024)
    Dim buffer(blockSize - 1) As Byte
    'Read the first block.'
    Dim bytesRead = source.Read(buffer, 0, blockSize)
    Do Until bytesRead = 0
        'Write the current block.'
        destination.Write(buffer, 0, bytesRead)
        'Read the next block.'
        bytesRead = source.Read(buffer, 0, blockSize)
    Loop
End Sub

相关内容

  • 没有找到相关文章

最新更新