无法在其他电脑上工作
您好,我有以下代码,它在我的机器上运行良好,但是当我在其他机器上进行安装时,应用程序崩溃并给出错误。
我想要实现的功能是,我将几个表的模式和数据备份到输出文件夹中,如果用户选中 Schema_checkbox,则备份文件应该删除模式之前的行,只保留数据位。所以最终它将包含数据但不包含模式。这一点在我的机器上运行良好,但在安装了我的 exe 的支持台上运行不佳。
我的代码:
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);
}
}
错误消息:
27/09/2011 14:46:34 开始支持表数据
27/09/2011 14:46:54 异常是:System.OutOfMemoryException:
引发了“System.OutOfMemoryException”类型的异常。
在 System.String.GetStringForStringBuilder(字符串值,Int32 startIndex、Int32 长度、Int32 容量)
在 System.Text.StringBuilder.GetNewString(String currentString, Int32 requiredLength) at System.Text.StringBuilder.Append(String value)
在 ErikEJ.SqlCeScripting.Generator.GenerateTableContent(字符串表名, 布尔值 saveImageFiles)
在 ErikEJ.SqlCeScripting.Generator.GenerateTableData(字符串表名, 布尔值 saveImageFiles)
在 ErikEJ.SqlCeScripting.Generator.GenerateTableScript(String tableName, 字符串输出文件夹,布尔值 isBackupReq)
在 ErikEJ.SqlCeScripting.Generator.GenerateSchemaGraph(字符串 连接字符串,List`1 表,字符串输出文件夹路径,布尔值 isDataBackupReq)
在 SqlDatabaseDataExport.MainForm.BackupScriptLocation()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您尝试打开的文件非常大,则可能会达到系统内存不足的限制。我建议您打开流来读取文件,然后和其他流一起使用缓冲区写入另一个文件(使用 StringBuilder)。这样,您的
newText
将不会达到非常高的内存级别,因为您将使用 StringBuilder 控制大小。另外,当连接大量字符串时使用 StringBuilder 而不是 String 总是更好,因为 String 使用更多内存。在我的建议结束时,您只需要删除原始文件并使用第一个文件的名称重命名输出文件。就是这样。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
newText
won'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.