2011-10-25 43 views
1

我試圖恢復使用c#通過下面的方法將數據庫文件(backup.sql)不工作...恢復功能使用mysql和C#

   string hostaddress = "localhost"; 
       try 
       { 
        string pathforbackupfile = @"C:\folder\Access\backupdb\backup.sql"; 
        StreamReader file = new StreamReader(pathforbackupfile); 
        string input = file.ReadToEnd(); 
        //file.Close(); 

        ProcessStartInfo psi = new ProcessStartInfo(); 
        psi.FileName = @"C:\wamp\bin\mysql\mysql5.5.8\bin\mysql.exe"; 
        psi.RedirectStandardInput = true; 
        psi.RedirectStandardOutput = true; 
        psi.CreateNoWindow = true; 

        psi.Arguments = string.Format(@"-u {0} -h {1} {2}", "root", hostaddress, "access"); 
        psi.UseShellExecute = false; 
        Process p = Process.Start(psi); 
        //p.StandardInput.WriteLine(input); 
        p.StandardInput.Write(input); 
        p.StandardInput.Close(); 
        p.WaitForExit(); 
        p.Close(); 

,但我沒能恢復數據庫..

什麼是錯的,上面的代碼..

編輯:

這是我T的一個他的表結構:

-- 
-- Table structure for table `banks` 
-- 

DROP TABLE IF EXISTS `banks`; 
/*!40101 SET @saved_cs_client  = @@character_set_client */; 
/*!40101 SET character_set_client = utf8 */; 
CREATE TABLE `banks` (
    `bank_Id` int(11) NOT NULL AUTO_INCREMENT, 
    `bank_Tag` varchar(32) NOT NULL DEFAULT '', 
    `bank_Name` varchar(32) NOT NULL DEFAULT '', 
    `bank_Address1` varchar(32) NOT NULL, 
    `bank_Address2` varchar(32) NOT NULL, 
    `bank_Address3` varchar(32) NOT NULL, 
    `bank_Town` varchar(32) NOT NULL, 
    `bank_County` varchar(32) NOT NULL, 
    `bank_Postcode` varchar(32) NOT NULL, 
    `bank_Country` varchar(32) NOT NULL DEFAULT 'UK', 
    `bank_Contact` varchar(32) NOT NULL DEFAULT '', 
    `bank_Phone` varchar(32) NOT NULL DEFAULT '', 
    `bank_Email` varchar(32) NOT NULL DEFAULT '', 
    `bank_SortCode` varchar(16) NOT NULL DEFAULT '00-00-00', 
    `bank_Active` varchar(8) NOT NULL DEFAULT 'yes', 
    PRIMARY KEY (`bank_Id`) 
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; 
/*!40101 SET character_set_client = @saved_cs_client */; 

-- 
-- Dumping data for table `banks` 
-- 

LOCK TABLES `banks` WRITE; 
/*!40000 ALTER TABLE `banks` DISABLE KEYS */; 
INSERT INTO `banks` VALUES (1,'ne','asd','asdf','df','df','df','df','sdf','df','df','df','sdf','sdf','y'); 
/*!40000 ALTER TABLE `banks` ENABLE KEYS */; 
UNLOCK TABLES; 

回答

0

這是我很久以前寫的一個函數的一部分。
希望這有助於:

private MySqlConnection NewConnection(string host, int port, string database, string user, string password) 
    { 
     if (port <= 0) port = 3306; 
     string cstr = ""; 
     if (database == "") 
      cstr = String.Format("SERVER={0};PORT={1};UID={2};PWD={3}", 
       host, port, user, password); 
     else 
      cstr = String.Format("SERVER={0};PORT={1};DATABASE={2};UID={3};PWD={4}", 
       host, port, database, user, password); 
     MySqlConnection conn = new MySqlConnection(cstr); 
     try { conn.Open(); } 
     catch (Exception ex) 
     { 
      conn = null; 
     } 
     return conn; 
    } 

    public int ExecuteScript(MySqlConnection conn, string sql) 
    { 
     err = ""; 
     int totrows = -1; 
     if (conn == null) return totrows; 
     if (conn.State == System.Data.ConnectionState.Closed) conn.Open(); 
     MySqlCommand cmd = new MySqlCommand(sql, conn); 
     try 
     { 
      totrows = cmd.ExecuteNonQuery(); 
      cmd = null; 
      return totrows; 
     } 
     catch (Exception ex) 
     { 
      err = ex.Message; 
      return 0; 
     } 
    } 

public bool Restore(string dbHost, int dbPort, string dbUser, string dbPassword, string file) 
{ 
    int toterr = 0; 
    int totsql = 0; 
    string msql = ""; 
    StreamReader rd = null; 
    FileStream fs = null;  

    MySqlConnection conn = NewConnection(dbHost, dbPort, "", dbUser, dbPassword); 
    if (conn == null) return false; 
    int cur = 0; 
    int rows = 0; 

    fs = new FileStream(file, FileMode.Open); 
    rd = new StreamReader(fs); 
    while (rd.Peek() > 0) 
    { 
     string t = rd.ReadLine().Trim(); 
     if ((t == "") || (t.StartsWith("--"))) continue; 
     msql += t; 
     if (msql == ";") msql = ""; 
     if (msql.EndsWith(";")) 
     { 
      string s = msql.Remove(msql.Length - 1, 1).Trim(); 
      msql = ""; 
      try 
      { 
       rows = ExecuteScript(conn, s); 
      } 
      catch 
      { 
       toterr++; 
       break; 
      } 
      finally 
      { 
       cur++; 
      } 
     } 
    } 
    rd.Close(); 
    rd = null; 
    fs.Close(); 
    fs = null; 

    conn.Close(); 
    conn = null; 
    return true; 
} 
+0

這裏有兩個函數「rows = ExecuteScript(conn,s);」和「NewConnection(dbHost,dbPort,」「,dbUser,dbPassword);」我有這兩個功能的錯誤..... – rockyashkumar

+0

@rockyashkumar:在這裏他們,看看 – Marco

+0

@Macro它不工作,我沒有在恢復前刪除數據庫,可能是這是原因,但我不知道,我不知道....你會提出任何想法 – rockyashkumar

0

@Marco給了什麼是更好的方式來處理它,因爲你會不會根據對執行代碼的外部應用程序文件。

但是,如果您想要在mysql客戶端中執行帶有SQL語句的文本文件,那麼您需要將它作爲參數傳遞給source命令。我想你需要將路徑中的反斜槓更改爲正斜槓

因此,而不是p.StandardInput.Write(input);您可以在代碼中使用p.StandardInput.Write("source "+pathforbackupfile.Replace('\\', '/'));