1
我正在創建一個帶有定時器的Windows服務,每隔幾秒鐘就會打勾一次,然後查詢一個數據庫。該服務的計時器工作正常,但只要我添加任何MySql的詳細信息,它停止工作。VB.NET Windows服務不與MySql一起工作
只需添加下面的幾行工作
Dim ConnectionString As String = "Data Source=localhost;User ID=root;Password=******;database=******;"
Dim sqlConn As MySqlConnection
sqlConn = New MySqlConnection(ConnectionString)
沒有這些線路的計時器保持在滴答作響停止它(我寫的EventLog
,看看發生了什麼)
我想,這一直是一個參考問題,但我添加了MySql.Data
參考。
任何幫助將不勝感激,謝謝。
編輯: 整個方法添加的要求
Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class Service1
Protected Overrides Sub OnStart(ByVal args() As String)
serviceTimer.Enabled = True
serviceTimer.Start()
EventLog.WriteEntry("MyService Started and Timer started")
End Sub
Protected Overrides Sub OnStop()
serviceTimer.Stop()
EventLog.WriteEntry("Out OnStop", EventLogEntryType.Information)
End Sub
Private Sub serviceTimer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles serviceTimer.Elapsed
Dim MyLog As New EventLog()
If Not EventLog.SourceExists("MyService") Then
EventLog.CreateEventSource("MyService", "Myservice Log")
End If
MyLog.Source = "MyService"
EventLog.WriteEntry("MyService Log", "This is log on " & CStr(TimeOfDay), EventLogEntryType.Information)
Dim ConnectionString As String = "Data Source=localhost;User ID=root;Password=******;database=******;"
Using sqlConn As New MySqlConnection(ConnectionString)
Dim sqlComm As MySqlCommand
Dim outputParam As Integer
sqlConn.Open()
sqlComm = New MySqlCommand("uspTest", sqlConn)
sqlComm.CommandType = CommandType.StoredProcedure
sqlComm.Parameters.Add(New MySqlParameter("Test", "100"))
outputParam = CInt(sqlComm.ExecuteScalar)
sqlConn.Close()
End Using
End Sub
End Class
你有例外嗎? – aleroot
您可以發佈整個方法嗎?它可能與創建MySqlConnection之前或之後發生的事情有關。我也會寫你的塊作爲使用sqlConn作爲新的MySqlConnection(connectionString)...結束使用只是爲了確保對象處理每個滴答。 – dash
我目前正在通過安裝並運行它作爲Windows服務來測試它,服務不會拋出異常(我不認爲),所以我沒有特定的異常。你知道從Windows服務中獲取例外的方法嗎?或者更好的測試方法 – jdtaylor