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ではなくコマンドライン自体によって解釈されます。ここでは2つの選択肢があります。

  1. --result-file他の人が述べているようにオプションを使用してください
  2. RedirectStandardOutputプロセスの標準をキャプチャし、のプロパティをに設定して、出力で好きなことを行いStartInfoますtrueStandardOutputこの後、プロセスのストリームから読み取ることができます。
于 2012-08-03T20:32:29.140 に答える
1

最初の引数として、ダンプしたいデータベースの名前を指定する必要があると思います。ネイサンのおかげ--databasesで、最後に続きます。

于 2012-08-03T18:53:00.543 に答える
0

Mysql のドキュメントには、mysqldump コマンドを使用する方法が 3 つあります。

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 フォルダーにダンプされます。

編集した方法は次のとおりです。

public static void 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 に答える