我正在创建一个带有计时器的 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
最佳答案
事实证明上面的代码是正确的,我忘记在我的服务器上安装 MySQL/NET 连接器,我已经在我的 PC 上安装了它但忘记在我的服务器上安装它!
关于mysql - VB.NET Windows 服务不能与 MySql 一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8378331/