zoukankan      html  css  js  c++  java
  • C#程序调用cmd执行命令-MySql备份还原

    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-winx64in";
        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);
    }

  • 相关阅读:
    使用puppeteer爬取网页数据实践小结
    React服务器端渲染框架next.js项目实战及部署上下文context问题解决办法
    在 React 组件中监听 android 手机物理返回/回退/back键事件
    vue页面切换效果(slide效果切换)
    记录HttpWebRequest辅助类
    C#异常Retry通用类
    Quartz.net2.2初体验
    【jQuery源码】整体架构
    CSRF攻击原理及防御
    SpringBoot----跨域配置
  • 原文地址:https://www.cnblogs.com/tianma3798/p/5079939.html
Copyright © 2011-2022 走看看