0

我正在尝试使用 mysqldump 从 c# 导出数据库。

当我运行它时,我收到此消息:选择数据库时出现未知数据库'mysqldump'。我找不到解决方案。

public static void mysqlBackup()
{
    try
    {
        //string time = DateTime.Now.ToString("dd-MM-yyyy");

        Log.Info("Starting MySQL dump");


        Process MySqlDump = new Process();
        MySqlDump.StartInfo.FileName = @"mysqldump.exe";
        MySqlDump.StartInfo.UseShellExecute = false;
        MySqlDump.StartInfo.Arguments = 
           "mysqldump -uroot -p******** b3 >"+ 
           " C:/Users/Administrator/Documents/temp/backups/backup.sql";
        MySqlDump.StartInfo.RedirectStandardInput = false; 
        MySqlDump.StartInfo.RedirectStandardOutput = false;

        MySqlDump.Start();

        MySqlDump.WaitForExit();
        MySqlDump.Close();

        Log.Info("Successfull created");
    }

    catch (IOException ex)
    {
        Log.Error("Unable to write the database file" + ex.ToString());
    }
}

我试图从参数中删除 mysqldump 有点同样的问题。

4

4 回答 4

4

重定向运算符>不是mysqldump. 当您在命令行上执行它时,它是由命令行本身解释的,而不是由 mysqldump 解释的。您在这里有两个选择:

  1. 使用--result-file其他人提到的选项
  2. RedirectStandardOutput捕获过程的标准输出,并通过将属性设置为StartInfo来对输出做你喜欢的事情true。在此之后,您可以从StandardOutput流程的流中读取。
于 2012-08-03T20:32:29.140 回答
1

我认为您需要指定要转储的数据库的名称作为第一个参数。多亏了 nathan,它--databases在最后才进行。

于 2012-08-03T18:53:00.543 回答
0

Mysql 文档指出有 3 种方法可以使用 mysqldump 命令:

shell> mysqldump [options] db_name [tbl_name ...]
shell> mysqldump [options] --databases db_name ...
shell> mysqldump [options] --all-databases

确保命令通过您的命令行正常工作。如果确实如此,则直接在您的代码中执行该命令。如果可行,那么开始提取您的参数并在代码中用您自己的参数替换它们。

基本上,您希望尽可能地基本并从那里开始工作。

如果文件在命令行上工作,试试这个:

using (Process p = new Process())
{        
        p.StartInfo.FileName = @"mysqldump.exe -u root -p *** --database b3 -r test.sql"; <~~~ note the change here
        p.Start();
        p.WaitForExit();
}

除非您更改该代码,否则该文件将转储到您的项目文件夹 bin/debug 或 bin/release 文件夹。

这是您编辑的方法:

公共静态无效mysqlBackup(){尝试{

        //string time = DateTime.Now.ToString("dd-MM-yyyy");

        Log.Info("Starting MySQL dump");


    using(Process MySqlDump = new Process()
    {
        MySqlDump.StartInfo.FileName = @"mysqldump.exe";
        MySqlDump.StartInfo.UseShellExecute = false;
        MySqlDump.StartInfo.Arguments = "-uroot -p******** b3 --result-file=C:/Users/Administrator/Documents/temp/backups/backup.sql";
        MySqlDump.StartInfo.RedirectStandardInput = false; 
        MySqlDump.StartInfo.RedirectStandardOutput = false; //You can redirect this as mention in other answers 

        MySqlDump.Start();

        MySqlDump.WaitForExit();
        MySqlDump.Close();
    }
   Log.Info("Successfully created");
    }

    catch (IOException ex)
    {
        Log.Error("Unable to write the database file" + ex.ToString());
    }
}
于 2012-08-03T18:57:32.253 回答
0
MySqlDump.StartInfo.Arguments = "-u root -p *** database_name --result-file [path]\backup.sql";

您也不需要在命令中再次指定 mysqldump (并不是说它应该有很大的不同)。

于 2012-08-03T20:40:16.347 回答