VB.NET Windows服务不能与MySql一起使用

VB.NET Windows服务不能与MySql一起使用,mysql,vb.net,windows-services,Mysql,Vb.net,Windows Services,我正在创建一个windows服务,它有一个计时器,每隔几秒钟就会显示一次,然后查询一个数据库。该服务的计时器工作正常,但只要我添加任何MySql细节,它就会停止工作 只需添加下面的几行就可以停止工作 Dim ConnectionString As String = "Data Source=localhost;User ID=root;Password=******;database=******;" Dim sqlConn As MySqlConnection sqlC

我正在创建一个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
reference

任何帮助都将不胜感激,谢谢

编辑: 按要求添加整个方法

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上安装了它,但忘了在我的服务器上安装它

你能把整个方法贴出来吗?它可能与创建MySqlConnection之前或之后发生的事情有关。我还将使用sqlConn作为新的MySqlConnection(connectionString)编写您的块。。。结束使用只是为了确保对象每次都被释放。我目前正在测试它,安装它并将其作为windows服务运行,该服务不会引发异常(我不认为),因此我没有特定的异常。您知道从windows服务获取异常的方法吗?或者更好的测试itAlso的方法是,尝试使用以下连接字符串:“Persist Security Info=False;database=****;server=localhost;user id=root;pwd=****”。我从未使用过MySql连接的数据源。测试这一点的最简单方法是提取您希望服务运行到其自身项目中的所有代码。然后,您可以在Windows服务中引用此代码,也可以在Windows窗体或控制台应用程序或其他任何应用程序中引用此代码,使其易于进入和调试。多亏了这些想法,我尝试了修改后的
连接字符串
,但没有任何区别。看来我得试着在它自己的项目中运行它,看看出了什么问题