I need to append lots of string in a loop and this will create a very long string.
1GB should be long enough to store the string, therefore I do this:
var sb = new StringBuilder(1024 * 1024 * 1024);
but receives this error
'System.OutOfMemoryException' was thrown
any advice to append long strings?
I'm writing a backup tools for MySQL database. The tools will export data from database and write into a text file. The data contains BLOB and TEXT data type. So, will expect very long string.
This is the code: Code Updated 1
var conn = new MySqlConnection(Program.ConnectionString);
var cmd = new MySqlCommand();
cmd.Connection = conn;
conn.Open();
cmd.CommandText = selectSQL;
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
var sb = new StringBuilder(1024 * 1024 * 1024);
for (int i = 0; i < rdr.FieldCount; i++)
{
if (sb.Length > 0)
sb.AppendFormat(separator);
if (rdr[i].GetType() == typeof(byte[]))
{
sb.AppendFormat(ConvertByteArrayToHexString((byte[])rdr[i]));
}
else
sb.AppendFormat(rdr[i] + "");
}
WriteToFile(sb.ToString());
}
conn.Close();