我在這段C#代碼中遇到了OutOfMemoryException問題。OutOfMemoryException和MemoryStream()
private void SaveZip(IDataReader reader, string header, string fileName, Ionic.Zip.ZipFile zip)
{
using (var stream = new MemoryStream())
{
using (var writer = new StreamWriter(stream, Encoding.ASCII))
{
writer.WriteLine(header);
while (reader.Read())
{
writer.WriteLine(reader.GetString(0)); //exception occurs here
}
writer.Close();
}
zip.AddEntry(fileName, stream.ToArray());
}
}
該代碼嘗試將DataReader對象寫入文件,該文件在過程結束時被壓縮。問題是Reader包含一大組數據,大約150MB,當我嘗試在內存中寫入數據流時,它會產生異常。 我該如何解決這個問題?
感謝您的幫助!
此代碼是否適用於較小的數據集? – 2013-02-12 17:47:23