即使在我清除了 stringbuilder 对象后,我也出现了内存不足的异常。
我想将数据表转换为 CSV,当我尝试将其转换为字符串时出现异常。
代码如下:
int lineCount = 0;
const int capacity = 10000000;
StringBuilder csvBuilder = new StringBuilder(capacity);
if (dt != null && dt.Rows.Count > 0)
{
using (var sw = new StreamWriter(FileName, false))
{
foreach (DataRow dr in dt.Rows)
{
csvBuilder.AppendLine(String.Join(fileSeperator, dr.ItemArray));
lineCount++;
if (lineCount >= 100000)
{
sw.Write(csvBuilder.ToString());
csvBuilder.clear();
lineCount = 0;
}
}
sw.Write(csvBuilder.ToString());
csvBuilder.clear();
lineCount = 0;
}
}