• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C#程序调用cmd执行命令-MySql备份还原

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.简单实例

//备份还原mysql
public static void TestOne()
{
    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.CreateNoWindow = true;
    //指定MySql程序的bin 目录
    p.StartInfo.WorkingDirectory = @"E:\mysql-5.6.26-winx64\bin";
    p.Start();

    /*************
    * 备份命令
    **************/
    //简单格式
    //string strSql = "mysqldump --quick --host=localhost -u root -p123 test > d:\\test_20151227110010.sql";
    /*命令指定格式*/
    //String command = "mysqldump --quick --host=localhost --default-character-set=gb2312 --lock-tables --verbose  --force --port=端口号 --user=用户名 --password=密码 数据库名 -r 备份到的地址";
    //string strSql = "mysqldump --quick --host=localhost  --default-character-set=gb2312 --lock-tables --verbose  --force --port=3306 --user=root --password=123 test -r d:\\one.sql";

    /*************
    * 还原命令
    **************/
    //简单格式
    //string strSql = "mysql  --host=localhost -u root -p123 test < d:\\test_20151227110010.sql";
    /**还原命令格式**/
    //string s = "mysql --host=localhost --default-character-set=gbk --port=端口号 --user=用户名 --password=密码 数据库名<还原文件所在路径";
    string strSql = "mysql  --host=localhost --port=3306 --user=root --password=123 test < d:\\test_20151227110010.sql";

    p.StandardInput.WriteLine(strSql + " &exit");
    p.StandardInput.AutoFlush = true;

    /****执行命令,没有返回可验证的结果*****/
    //显示方式1
    //StreamReader reader = p.StandardOutput;
    //string line = reader.ReadLine();
    //while (!reader.EndOfStream)
    //{
    //    Console.WriteLine(line);
    //    line = reader.ReadLine();
    //}

    //显示方式2
    string result = p.StandardOutput.ReadToEnd();
    Console.WriteLine(result);

    //返回警告结果 --Warning: Using a password on the command line interface can be insecure.
    string result2 = p.StandardError.ReadToEnd();
    Console.WriteLine(result2);

    //显示方式3
    ShowValue(p);

    p.WaitForExit();
    p.Close();
}

private static async void ShowValue(Process p)
{
    string result = await p.StandardOutput.ReadToEndAsync();
    Console.WriteLine(result);
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#找到目录和其子目录的某个文件发布时间:2022-07-13
下一篇:
存储过程中调用C#自定义封装的DLL发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap