我无法从C#.net windows应用程序恢复现有数据库

时间:2012-03-25 17:19:25

标签: c# mysql

  

BasePriority ='process.BasePriority'引发了类型异常   'System.InvalidOperationException' ...

这是从我的.net应用程序恢复数据库时遇到的异常..这里我提供代码....

try
{
    //Read file from C:\
    string path;
    path = filetext.Text;

    StreamReader file = new StreamReader(path);
    string input = file.ReadToEnd();
    file.Close();

    ProcessStartInfo psi = new ProcessStartInfo();
    psi.FileName = @"C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin\mysqlimport.exe";
    psi.RedirectStandardInput = true;
    psi.RedirectStandardOutput = false;
    psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}",
    userid, paswd, server, comboBox1.Text);
    psi.UseShellExecute = false;

    Console.WriteLine(psi);

    Process process = Process.Start(psi);
    process.StandardInput.WriteLine(input);
    process.StandardInput.Close();

    process.WaitForExit();
    process.Close();

    MessageBox.Show("database is restored");
}
catch (IOException ex)
{
   MessageBox.Show("Error , unable to Restore!");
}

process.start(psi).之后。它显示了异常......

Plz提供任何建议......

2 个答案:

答案 0 :(得分:0)

当您尝试写入StandardInput时,看起来该进程尚未启动。 您可以尝试拨打Thread.Sleep()WaitForInputIdle()吗?

答案 1 :(得分:0)

尝试此功能它工作正常。只需使用mysql.exe更改mysqlimport.exe。

代码

public static string RestoreDataBase(string ExeLocation,
    string Fname,
    string DBName,
    string Ip,
    string Uid,
    string Pass)
{
    string sql = "";
    try
    {
        //Read file from C:\
        string path;
        path = Fname;

        StreamReader file = new StreamReader(path);
        string input = file.ReadToEnd();
        file.Close();

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = ExeLocation;//@"C:\Program Files (x86)\MySQL\MySQL Server 5.0\bin\mysqlimport.exe";
        psi.RedirectStandardInput = true;
        psi.RedirectStandardOutput = false;
        psi.Arguments = string.Format(@"-u{0} -p{1} -h{2} {3}",
        Uid, Pass, Ip, DBName);
        psi.UseShellExecute = false;

        Console.WriteLine(psi);

        Process process = Process.Start(psi);
        process.StandardInput.WriteLine(input);
        process.StandardInput.Close();

        process.WaitForExit();
        process.Close();

        sql = "OK";
    }
    catch (Exception ex)
    {
        sql = ex.Message;
    }
    return sql;
}

调用功能

string sql = functionclass.RestoreDataBase(@"C:\Program Files\MySQL\MySQL Server 5.1\bin\mysql.exe", @"C:\iTelBilling-5-4-2013.sql", "iTelBilling", "localhost", "root", "admin");