2013-08-30 80 views
0

我正在開發的停車場數據庫系統的一部分涉及從車牌讀取攝像機將數據加載到數據庫表中。使用.net連接器中的批量加載器對象(用於試驗單行和多行文件),我設法做到了這一點,但是有一個常規的,但間歇性的錯誤彈出,我希望這裏有人可以幫助我解決出了什麼問題並提供解決方案MySQL負載數據錯誤

我正在使用一對filesystemwatcher對象來監視攝像機(入口和出口)輸出其CSV數據的位置。在「文件創建」事件上,觸發另一個子例程,然後將文件的內容加載到數據庫中。代碼爲這一子程序列出如下:

Public Sub sqlloaddata_in(ByVal sqlfilepath As String) 

     loaddatainsqlconn = New MySqlConnection 
     loaddatainsqlconn.ConnectionString = "Server=localhost;user id=root;password=W1nd0ws;database=hystest" 

     Try 
      'instanciate a mysqlbulkloader, feed in the parameters 
      Dim sqlbulkin As New MySqlBulkLoader(loaddatainsqlconn) 
      Dim insertedin As Long 
      sqlbulkin.TableName = "hystest.tblin_dupebuffer" 
      sqlbulkin.FieldTerminator = "," 
      sqlbulkin.LineTerminator = "\n" 
      sqlbulkin.FileName = sqlfilepath 
      'sqlbulkin.ConflictOption = MySqlBulkLoaderConflictOption.Ignore 
      sqlbulkin.NumberOfLinesToSkip = 1 
      'load the data into the database. return the number of rows inserted into the variable 
      insertedin = sqlbulkin.Load 
      Debug.Print(insertedin & " rows inserted.") 

      ' refreshdatagrid_in() 
      'close and get rid of the connection 
      loaddatainsqlconn.Close() 
      loaddatainsqlconn.Dispose() 
      'transferdata_in() 
     Catch ex As MySqlException 
      'if bad stuff happens, do this 
      'MessageBox.Show("Database Error: " & ex.Message & vbCrLf & ex.InnerException.Message) 
      'Debug.Print(ex.Message & ex.InnerException.Message & ex.InnerException.InnerException.Message) 
      Debug.Print(ex.ToString) 
      'Debug.Print("Retrying insert") 
      'Try 
      ' sqlloaddata_in(sqlfilepath) 
      'Catch ex2 As Exception 

      'End Try 
     End Try 
     'dedupe_in() 
    End Sub 

該子程序將運行每一個新文件被創建的時間(相機輸出每隔10秒文件),但是在運行時,它產生以下錯誤(來自調試窗口取) :

1 rows inserted. 
A first chance exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll 
MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered during command execution. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Fatal error encountered attempting to read the resultset. ---> MySql.Data.MySqlClient.MySqlException (0x80004005): Error during LOAD DATA LOCAL INFILE ---> System.IO.IOException: The process cannot access the file 'C:\HYS Database\Raw\IN\INLIST_HYS,HN03KTG,1970-01-01,03-36-01-582.CSV' because it is being used by another process. 
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath) 
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access) 
    at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename) 
    at MySql.Data.MySqlClient.NativeDriver.SendFileToServer(String filename) 
    at MySql.Data.MySqlClient.NativeDriver.GetResult(Int32& affectedRow, Int64& insertedId) 
    at MySql.Data.MySqlClient.Driver.GetResult(Int32 statementId, Int32& affectedRows, Int64& insertedId) 
    at MySql.Data.MySqlClient.Driver.NextResult(Int32 statementId, Boolean force) 
    at MySql.Data.MySqlClient.MySqlDataReader.NextResult() 
    at MySql.Data.MySqlClient.MySqlDataReader.NextResult() 
    at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) 
    at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior) 
    at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader() 
    at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery() 
    at MySql.Data.MySqlClient.MySqlBulkLoader.Load() 
    at SmartPark_Data_Loader.mysql.sqlloaddata_in(String sqlfilepath) in C:\Users\<name>\Dropbox\Visual Studio 2010\Projects\SmartPark Data Loader\SmartPark Data Loader\mysql.vb:line 201 
1 rows inserted. 
1 rows inserted. 
1 rows inserted. 
1 rows inserted. 
1 rows inserted. 
1 rows inserted. 
在上面的例子

,一個文件被成功地插入,然後在第二,誤差彈出。這需要大約一分鐘的時間才能出現,並且在那段時間內,還創建了其他幾個文件,創建待處理事件的積壓,然後正確插入。

這種情況經常發生,我不確定爲什麼。調試窗口的最終佈局會在錯誤和文件事件的結果之間交替出現。

我該怎麼辦?

+0

錯誤消息很清楚地說明了問題所在:*「進程無法訪問文件'C:\ HYS Database \ Raw \ IN \ INLIST_HYS,HN03KTG,1970-01-01,03-36-01-582 .CSV',因爲它正在被另一個進程使用。「*該消息的哪部分你不明白? –

回答

0

我認爲史蒂芬Doggart先生注意到了相應的錯誤信息:

C:\HYS Database\Raw\IN\INLIST_HYS,HN03KTG,1970-01-01,03-36-01-582.CSV 

該文件似乎當MySQL客戶端代碼去閱讀它無法訪問。

假設這是隻有你知道的正確的文件名,那麼你遇到了文件系統觀察者的一個相當常見的問題。當目錄發生變化時,他們開始他們的活動。

所以,有時間的時刻之間的窗口時,將節目寫入文件

  1. 創建文件,並
  2. 寫完它,並將其關閉。

如果您的程序試圖在該時間窗口中打開該文件,則可能導致此錯誤。

以下是解決此問題的一種方法。在將文件名傳遞給批量加載程序之前,讓程序在一個循環中嘗試打開文件進行讀取(然後立即關閉它)。如果您的打開嘗試失敗(引發異常),則需要睡眠十幾毫秒,然後重試。不要讓這個循環無限:十次嘗試後放棄。你不想讓你的程序掛起來這樣做。