2011-12-04 37 views
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 
+0

你有例外吗? – aleroot

+0

您可以发布整个方法吗?它可能与创建MySqlConnection之前或之后发生的事情有关。我也会写你的块作为使用sqlConn作为新的MySqlConnection(connectionString)...结束使用只是为了确保对象处理每个滴答。 – dash

+0

我目前正在通过安装并运行它作为Windows服务来测试它,服务不会抛出异常(我不认为),所以我没有特定的异常。你知道从Windows服务中获取例外的方法吗?或者更好的测试方法 – jdtaylor

回答

0

事实证明,上面的代码是正确的,我忘了我的服务器上安装了MySQL/NET连接,我已经安装了它在我的电脑上但忘记将其安装在我的服务器上!