博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中MySQL数据库的备份 还原 初始化
阅读量:7211 次
发布时间:2019-06-29

本文共 2731 字,大约阅读时间需要 9 分钟。

直接在cmd执行如下代码:
mysqldump  -h localhost -uroot -p123 --default-character-set=utf8 --opt --disable-keys --lock-all-tables -R --hex-blob  TEST >D:\PM\bin\Debug\BackUp\TEST_20161212.sqlTEST为数据库名称
// 执行创建数据库操作this.GetExecute(G_Con, "create database if not exists NEWDB");this.sqlAddress = " -h " + IP + " -u" + User + " -p" + Password + " NEWDB ";// 数据库的备份private void btn_Dump_Click(object sender, EventArgs e){    using (SaveFileDialog sfd = new SaveFileDialog())    {        sfd.Filter = "数据库文件|*.sql";        sfd.FilterIndex = 0;        sfd.RestoreDirectory = true;        sfd.FileName = "BackUp" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".sql";        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)        {            string filePath = sfd.FileName;            string cmd = "mysqldump " + sqlAddress + " > \"" + filePath + "\"";            string result = RunCmd(cmd);            if (result.Trim() == "")            {                MessageBox.Show("数据库备份成功!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            else            {                MessageBox.Show(result, "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);            }                  }    }}//数据库的还原// 还原数据库private void btn_Import_Click(object sender, EventArgs e){    if (this.tb_Path.Text.Trim() == "")    {        MessageBox.Show("请选择要恢复的文件!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);        return;     }    //this.GetExecute(G_Con, "create database if not exists clothes");    string filePath = this.tb_Path.Text.Trim();    string cmd = "mysql " + sqlAddress + " < \"" + filePath + "\"";    string result = RunCmd(cmd);    if (result.Trim() == "")    {        MessageBox.Show("数据库恢复成功!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);    }    else    {        MessageBox.Show(result, "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);    }}// 命令行操作private string RunCmd(string command){    //例Process    Process p = new Process();    p.StartInfo.FileName = "cmd.exe";           //确定程序名    p.StartInfo.Arguments = "/c " + command;    //确定程式命令行    p.StartInfo.UseShellExecute = false;        //Shell的使用    p.StartInfo.RedirectStandardInput = true;   //重定向输入    p.StartInfo.RedirectStandardOutput = true; //重定向输出    p.StartInfo.RedirectStandardError = true;   //重定向输出错误    p.StartInfo.CreateNoWindow = true;          //设置置不显示示窗口    p.Start();   //00    p.StandardInput.WriteLine(command);       //也可以用这种方式输入入要行的命令    p.StandardInput.WriteLine("exit");        //要得加上Exit要不然下一行程式    //p.WaitForExit();    //p.Close();    //return p.StandardOutput.ReadToEnd();        //输出出流取得命令行结果果    return p.StandardError.ReadToEnd();}

 

转载于:https://www.cnblogs.com/step-city/p/6078553.html

你可能感兴趣的文章
Ubuntu下,用cron实现简单的定时器
查看>>
MySql常用命令总结
查看>>
文本相似度计算之余弦定理
查看>>
SpringMVC访问静态资源
查看>>
yii2视图(布局)中各种函数总结报告及使用场景
查看>>
scala override的学习总结
查看>>
[转]rhel 忘记root密码修改方法
查看>>
java面试专题(一):int和integer的深层次区别
查看>>
JS操作VML
查看>>
Spring-Cloud-eureka(Dalston)的配置
查看>>
忽然间对 生命的游戏 着了魔
查看>>
ES6箭头函数中的this绑定问题
查看>>
hadoop mysql
查看>>
Javascript面向对象编程
查看>>
Findbugs 使用备忘录
查看>>
btrace学习四--bBTrace example
查看>>
java生产者消费者问题(线程同步与线程等待的应用)
查看>>
openstack-mitaka安装文档-Compute服务安装
查看>>
前端基础
查看>>
js加载时间线 (重点掌握)
查看>>