2011-09-27 64 views
-2

嗨,我有下面的代碼,它可以在我的機器上正常工作,但是當我給我的安裝設置在其他機器上應用程序已崩潰,並給我錯誤。
而我試圖實現的功能是我支持少量表的模式和數據到輸出文件夾,並且如果用戶檢查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十四時46分34秒開始備份表數據
27/09/2011 14時46分54秒的異常是:System.OutOfMemoryException:
引發類型'System.OutOfMemoryException'的異常。
在System.String.GetStringForStringBuilder(字符串值,的Int32 的startIndex,的Int32長度,的Int32容量)
在System.Text.StringBuilder.GetNewString(字符串currentString,的Int32 requiredLength)在System.Text.StringBuilder.Append(字符串值)
在ErikEJ.SqlCeScripting.Generator.GenerateTableContent(字符串表名, 布爾saveImageFiles)
在ErikEJ.SqlCeScripting.Generator.GenerateTableData(字符串表名, 布爾saveImageFiles)
在ErikEJ.SqlCeScripting.Generator.GenerateTableScript(字符串tableName, String outputFolder,Boolean isBackupReq)
在ErikEJ.SqlCeScripting.Generator.GenerateSchemaGraph(字符串 的connectionString,List`1表,字符串outputFolderPath,布爾 isDataBackupReq)
在SqlDatabaseDataExport.MainForm.BackupScriptLocation()

+0

「System.OutOfMemoryException」 - 您的應用程序使用的內存太多。減少內存使用量或將程序安裝在不太「過時」的計算機上。 – qJake

+4

您包含的代碼中不會發生異常。 'CopyFileContent'不出現在堆棧跟蹤的任何地方。 –

+1

在哪裏使用sb? – hungryMind

回答

1

如果文件嘗試打開是非常大的,你可以達到你的系統內存不足的極限。我建議你打開流來讀取文件,然後將其他流寫入帶有緩衝區的其他文件(使用StringBuilder)。這樣,您的newText將不會達到非常高的內存水平,因爲您將通過StringBuilder控制大小。另外,當連接大量字符串以使用StringBuilder而不是String時總是更好,因爲String使用更多內存。在我的建議結束時,您只需要刪除原始文件並使用第一個文件的名稱重命名輸出文件。而已。

+1

好的建議,但一開始他可以使用堆棧跟蹤來查找異常實際被拋出的位置。 –

+0

@Daok是否可以發送一些代碼刪除原始文件並重命名輸出文件 – 62071072SP

+1

System.IO.File.Move(@「C:\ From.txt」,@「C:\ TO.txt」); //重命名 –

相關問題