2016-10-17 20 views
0

コードにエラーはありません。しかし、私がバックアップすると、ファイルのサイズは0kbまたは1kbのみです。私は何も復元しません。すべてのデータは削除されます。mySQLのバックアップと復元c#

string path; 
path = "D:\\MySqlBackup"+ ".sql"; 
StreamWriter file = new StreamWriter(path); 


ProcessStartInfo psi = new ProcessStartInfo(); 
psi.FileName ="C:\\xampp\\mysql\\bin\\mysqldump.exe"; 
psi.RedirectStandardInput = false; 
psi.RedirectStandardOutput = true; 
psi.Arguments = "--user=root --password=database --database=hotelreservationandbillingsystem < D:\\MySqlBackup.sql"; 
psi.UseShellExecute = false; 

Process process = Process.Start(psi); 
string output; 
output = process.StandardOutput.ReadToEnd(); 
file.WriteLine(output); 
process.WaitForExit(); 
file.Close(); 
process.Close(); 
MessageBox.Show("Back Up Successfully!","Saved",MessageBoxButtons.OKCancel,MessageBoxIcon.Information); 

コード復元:バックコードアップ

string path; 
path = "D:\\MySqlBackup.sql"; 
StreamReader file = new StreamReader(path); 
string input = file.ReadToEnd(); 
file.Close(); 

ProcessStartInfo psi = new ProcessStartInfo(); 
psi.FileName = "C:\\xampp\\mysql\\bin\\mysqldump.exe"; 
psi.RedirectStandardInput = true; 
psi.RedirectStandardOutput = false; 
psi.Arguments = "--user=root --password=database --database=hotelreservationandbillingsystem < D:\\MySqlBackup.sql"; 
psi.UseShellExecute = false; 


Process process = Process.Start(psi); 
process.StandardInput.WriteLine(input); 
process.StandardInput.Close(); 
process.WaitForExit(); 
process.Close(); 
MessageBox.Show("Restored Successfully!", "Restored", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); 

答えて

1

がなぜあなたはバックアップを取ってより多くの最も簡単な方法を試してみて、利用できるオプションを復元する必要があります MySqlBackup.NET - MySQL Backup Solution for C#

どこにBackuのコードP

string constring = "server=localhost;user=root;pwd=qwerty;database=test;"; 
string file = "C:\\backup.sql"; 
using (MySqlConnection conn = new MySqlConnection(constring)) 
{ 
    using (MySqlCommand cmd = new MySqlCommand()) 
    { 
     using (MySqlBackup mb = new MySqlBackup(cmd)) 
     { 
      cmd.Connection = conn; 
      conn.Open(); 
      mb.ExportToFile(file); 
      conn.Close(); 
     } 
    } 
} 

そして、私は参照を追加する必要があります

string constring = "server=localhost;user=root;pwd=qwerty;database=test;"; 
string file = "C:\\backup.sql"; 
using (MySqlConnection conn = new MySqlConnection(constring)) 
{ 
    using (MySqlCommand cmd = new MySqlCommand()) 
    { 
     using (MySqlBackup mb = new MySqlBackup(cmd)) 
     { 
      cmd.Connection = conn; 
      conn.Open(); 
      mb.ImportFromFile(file); 
      conn.Close(); 
     } 
    } 
} 
+0

を復元しているのですか? MySqlBackupのために? – Frank

+0

はい、前提条件が必要です MySqlBackup.NETは、以下のコンポーネントを使用して動作します。 MySQLのドットネットConnector/Net(MySql.Data.DLL) MySqlBackup.NETを動作させるには、このDLLの参照をプロジェクトに追加する必要があります。 MySql.Data.DLLは、Oracle Corporationによって開発され、GPLライセンス(http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)でライセンスされています。 –

関連する問題