2016-10-17 4 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 닷넷 커넥터/Net (MySql.Data.DLL) MySqlBackup.NET이 작동하려면이 DLL에 대한 참조를 프로젝트에 추가해야합니다. MySql.Data.DLL은 Oracle Corporation에서 개발되었으며 GPL 라이센스 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html)로 라이센스가 부여됩니다. –