Hi i have the following code which works fine in my machine but when i give my install set in other machines application is been crashed and giving me error .
And the functionality i am trying to achive is I am backing schema and data for few tables into an output folder and if the user checks the Schema_checkbox then the backup file should delete the lines till schema and just leave the data bit . So finally it will contain data but not schema . This bit is working fine on my machine but not in support desk who installed my exe.
My code :
if (Schema_checkbox.Checked)
{
DisplayMainWindow("Schema not required selected");
Logger.Log("Schema not required selected " + DateTime.Now);
FileHelper.CopyFileContent(outputFileName);
DisplayMainWindow(" Only table data is backup excluding schema");
Logger.Log(" Only table data is backup excluding schema" + DateTime.Now);
}
public void CopyFileContent(string filePath)
{
try
{
StringBuilder sb = new StringBuilder();
string newText = null;
using (StreamReader tsr = new StreamReader(filePath))
{
do
{
string textLine = tsr.ReadLine() + "\r\n";
{
if (textLine.StartsWith("INSERT INTO"))
{
newText += textLine + Environment.NewLine;
}
}
}
while (tsr.Peek() != -1);
tsr.Close();
}
File.WriteAllText(filePath, newText);
}
catch (Exception ex)
{
Logger.Log("Exception" + ex);
MessageBox.Show("Error Occured" + ex);
}
}
Error Message :
27/09/2011 14:46:34 Started backing Table data
27/09/2011 14:46:54 Exception is : System.OutOfMemoryException:
Exception of type ‘System.OutOfMemoryException’ was thrown.
at System.String.GetStringForStringBuilder(String value, Int32
startIndex, Int32 length, Int32 capacity)
at System.Text.StringBuilder.GetNewString(String currentString, Int32
requiredLength) at System.Text.StringBuilder.Append(String value)
at ErikEJ.SqlCeScripting.Generator.GenerateTableContent(String tableName,
Boolean saveImageFiles)
at ErikEJ.SqlCeScripting.Generator.GenerateTableData(String tableName,
Boolean saveImageFiles)
at ErikEJ.SqlCeScripting.Generator.GenerateTableScript(String tableName,
String outputFolder, Boolean isBackupReq)
at ErikEJ.SqlCeScripting.Generator.GenerateSchemaGraph(String
connectionString, List`1 tables, String outputFolderPath, Boolean
isDataBackupReq)
at SqlDatabaseDataExport.MainForm.BackupScriptLocation()
If the file you try to open is very big, you can reach a limit where your system will run low in memory. I suggest you that you open the stream to read the file and then and other stream to write into the other file with a buffer (use StringBuilder). This way, your
newTextwon’t reach a very high level of memory because you will control the size with the StringBuilder. Also, it’s always better when concatenate a lot of string to use a StringBuilder instead of String because the String use more memory. At the end of my suggestion, you just need to delete the original file and rename the output file with the name of the first file. That’s it.