2012-02-10 216 views
0

我試圖加載大數據文件,例如10 MB文件到數據庫使用加載數據本地INFILE到MySQL使用Windows服務。但是它失敗了,windows服務沒有任何異常就停止了。我嘗試使用2 MB等小尺寸文件,併成功完成。無法使用加載數據infile本地到mysql加載大文件

有沒有什麼辦法可以將大文件加載到mysql數據庫中?而Windows服務停止的原因可能是什麼?

這裏是我的代碼..

public int UpdateDataBase(string query, string filename, Logger swLog) 
    { 
     int status = 0; 

     using (MySqlConnection myconnection = new MySqlConnection(connectionCdrBank)) 
     { 

      try 
      { 
       myconnection.ConnectionTimeout = 1000; 
       myconnection.Open(); 
       myconnection.BeginTransaction(); 

       MySqlCommand mycommand = new MySqlCommand("ClearTempTable", myconnection); 
       mycommand.CommandType = CommandType.StoredProcedure; 
       status = mycommand.ExecuteNonQuery(); 

       MySqlCommand mycommand1 = new MySqlCommand(query, myconnection); 
       mycommand1.CommandTimeout = 1000; 
       status = mycommand1.ExecuteNonQuery(); 

       MySqlCommand mycommand2 = new MySqlCommand("InsertToCdrDetailsTempTable", myconnection); 
       mycommand2.CommandType = CommandType.StoredProcedure; 
       mycommand2.CommandTimeout = 1000; 
       status = Convert.ToInt32(mycommand2.ExecuteScalar()); 

       myconnection.Commit(); 
       myconnection.Close(); 

      } 
      catch (Exception ex) 
      { 
       Console.WriteLine("Error:" + ex.Message); 
       if (myconnection.State == ConnectionState.Open) 
        myconnection.Rollback(); 
       status = (ex.Message.IndexOf("Duplicate entry") != -1) ? -1000 : 0; 
       swLog.WriteErrorToLog(""); 
       swLog.WriteErrorToLog("Error while saving: " + ex.Message); 
       mail.SentMail("Error while saving:", ex.Message); 
       swLog.CloseLogger(); 
      } 
     } 

     return status; 
    } 

哪裏查詢

LOAD DATA LOCAL INFILE 'a.txt' INTO TABLE tbl_cdrload" FIELDS TERMINATED BY ',' LINES  TERMINATED BY '\n'; 
+0

錯誤日誌中是否有相關條目? 10MB不是一個巨大的文件加載。我定期從很多GB文件加載數據。 – nnichols 2012-02-10 17:44:56

+0

什麼都沒有記錄......實際上,當我試圖調試service.It它不會拋出異常,它只是停止Windows服務。 – Jay 2012-02-10 17:55:49

回答

1

我加載高達100個MB的文件到MySQL BLOB字段。這只是我的人爲限制。

因此,10MB文件不應該是一個問題。

您是否正確設置了max_allowed_pa​​cket?

你用什麼來加載文件?你自己的代碼或一些工具?什麼MySQL連接器?

+0

我正在執行它作爲從c#代碼的mysql文本命令。我使用devart連接到mysql.And我們可以在哪裏設置這個max_allowed_pa​​cket? – Jay 2012-02-10 18:10:16

+0

Google是你的朋友... http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html 如果你有MySQL Workbench,它是一個服務器網絡設置。 – Remy 2012-02-10 18:21:49

+0

嘗試將max_allowed_pa​​cket設置爲更大的值。但仍然是同樣的問題...這裏是我的代碼....我已更新我的代碼 – Jay 2012-02-10 18:49:14