C# MySQL資料庫的備份 還原 初始化

來源:互聯網
上載者:User

標籤:

// 執行建立資料庫操作

this.GetExecute(G_Con, "create database if not exists NEWDB");

this.sqlAddress = " -h " + IP + " -u" + User + " -p" + Password + " NEWDB ";

// 資料庫的備份

private void btn_Dump_Click(object sender, EventArgs e)

{   

  using (SaveFileDialog sfd = new SaveFileDialog())

    {  

       sfd.Filter = "資料庫檔案|*.sql";   

      sfd.FilterIndex = 0;    

     sfd.RestoreDirectory = true;  

       sfd.FileName = "BackUp" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".sql";

        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)     

    {         

    string filePath = sfd.FileName;     

        string cmd = "mysqldump " + sqlAddress + " > \"" + filePath + "\"";    

         string result = RunCmd(cmd);    

         if (result.Trim() == "")     

        {             

    MessageBox.Show("Database Backup成功!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);   

          }     

        else     

        {    

             MessageBox.Show(result, "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);   

          }            

       }    

}

}

//資料庫的還原 // 還原資料庫

private void btn_Import_Click(object sender, EventArgs e)

{  

   if (this.tb_Path.Text.Trim() == "")   

  {    

     MessageBox.Show("請選擇要恢複的檔案!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);

        return;   

  }    

//this.GetExecute(G_Con, "create database if not exists clothes");   

  string filePath = this.tb_Path.Text.Trim();

    string cmd = "mysql " + sqlAddress + " < \"" + filePath + "\"";     string result = RunCmd(cmd);  

   if (result.Trim() == "")   

  {    

     MessageBox.Show("資料庫恢複成功!", "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

    else   

  {  

       MessageBox.Show(result, "CMS", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }

}

// 命令列操作

private string RunCmd(string command)

{    

//例

Process     Process p = new Process();

    p.StartInfo.FileName = "cmd.exe";      //確定程式名

    p.StartInfo.Arguments = "/c " + command;    //確定程式命令列

    p.StartInfo.UseShellExecute = false;        //Shell的使用  

   p.StartInfo.RedirectStandardInput = true;   //重新導向輸入   

  p.StartInfo.RedirectStandardOutput = true; //重新導向輸出

    p.StartInfo.RedirectStandardError = true;   //重新導向輸出錯誤    

p.StartInfo.CreateNoWindow = true;          //設定置不顯示示視窗

    p.Start();   //00

    p.StandardInput.WriteLine(command);       //也可以用這種方式輸入入要行的命令

    p.StandardInput.WriteLine("exit");        //要得加上Exit要不然下一行程式  

   //p.WaitForExit();  

   //p.Close();

    //return p.StandardOutput.ReadToEnd();       

//輸出出流取得命令列結果果    

return p.StandardError.ReadToEnd();

}

C# MySQL資料庫的備份 還原 初始化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.