C# 恢复在线数据库
只是另一个问题。
我有一个 C# 项目,具有备份和恢复在线 MySQL 数据库的功能。备份功能运行得很好,但是我似乎无法使恢复功能在线工作。不过,它对于本地数据库来说效果很好。
这是我的恢复功能的代码:
private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
{
//restoreFile is an OpenFileDialog
restoreFile.Title = "Restore Database";
restoreFile.FileName = "";
restoreFile.Filter = "MySQL Dump (*.sql)|*.sql";
DialogResult dr = restoreFile.ShowDialog();
if (dr == DialogResult.OK)
{
string filepath = restoreFile.FileName;
StreamReader file = new StreamReader(filepath);
string input = file.ReadToEnd();
file.Close();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "mysql";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}", "uName", "pass", "localhost", "database");
psi.UseShellExecute = false;
Process process = Process.Start(psi);
process.StandardInput.WriteLine(input);
process.StandardInput.Close();
process.WaitForExit();
process.Close();
MessageBox.Show("Database was successfully restored!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
任何帮助将不胜感激。谢谢你!
just another question.
I have a C# project that has a functionality of backing-up and restoring an online MySQL database. The back-up function is working well enough however, I can't seem to make the restore function work online. It works well for a local database though.
Here's my code for the restore function:
private void restoreToolStripMenuItem_Click(object sender, EventArgs e)
{
//restoreFile is an OpenFileDialog
restoreFile.Title = "Restore Database";
restoreFile.FileName = "";
restoreFile.Filter = "MySQL Dump (*.sql)|*.sql";
DialogResult dr = restoreFile.ShowDialog();
if (dr == DialogResult.OK)
{
string filepath = restoreFile.FileName;
StreamReader file = new StreamReader(filepath);
string input = file.ReadToEnd();
file.Close();
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "mysql";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = false;
psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}", "uName", "pass", "localhost", "database");
psi.UseShellExecute = false;
Process process = Process.Start(psi);
process.StandardInput.WriteLine(input);
process.StandardInput.Close();
process.WaitForExit();
process.Close();
MessageBox.Show("Database was successfully restored!", "Notification", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
Any help would be much appreciated. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
替换 psi.FileName = "mysql";和
psi.FileName = "C:/wamp/bin/mysql/mysql5.5.24/bin/mysql.exe";
Replace psi.FileName = "mysql"; with
psi.FileName = "C:/wamp/bin/mysql/mysql5.5.24/bin/mysql.exe";