简体   繁体   English

MySQL备份和还原C#

[英]mySQL backup and restore c#

The code have no errors. 该代码没有错误。 but when the I back up,the size of the file is 0kb or 1kb only. 但是当我备份时,文件的大小仅为0kb或1kb。 And when I restore nothing happens. 当我恢复时,什么也没发生。 All data is still deleted. 所有数据仍被删除。

Back up Code: 备份代码:

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);

Restore Code: 恢复代码:

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);

Why not you should try a more easiest way of taking backup and restore option available MySqlBackup.NET - MySQL Backup Solution for C# 为什么不尝试采用更简单的方法来获取可用的备份和还原选项MySqlBackup.NET-C#的MySQL备份解决方案

Where the code for Backup is 备份的代码在哪里

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();
        }
    }
}

And Restore 还原

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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM