无法在Windows服务执行SQL命令 [英] Unable to execute SQL command in windows service
问题描述
我正在创建一个Windows服务,将发送电子邮件给客户时,他们一个月有其提交到期的范围内。我用vb.net在视觉工作室2008
I'm working on creating a windows service that will send emails to a customer when they are within a month of having their submission expire. I'm using vb.net in Visual Studios 2008
由于它是一个窗口服务,这是非常难以调试。试图缩小我的错误我创建,如果它得到一定行向我发送一封电子邮件,一个sendDebugEmail的方法。该邮件永远不会让过去博士= cmd.ExecuteReader()
Because it's a windows service it's very difficult to debug. Trying to narrow down my error I created a "sendDebugEmail" method that sends me an email if it gets to a certain line. The emails never make it past "dr = cmd.ExecuteReader()"
我不知道我在做什么错。我的SQL语句,应该能正常运行。我在我的SQL Server数据库中进行了测试。
I'm wondering what I am doing wrong. My SQL statement should work fine. I've tested it in my SQL server database.
我已经创建了我刚才在SQL Server以及一个dummy_database。我增加了一个INSERT SQL语句的虚拟表我在那里只是为了看看我是否能真正访问数据库。所有的表中需要的是行号和时间被发送。当我运行我的Windows服务,数据库更新就好了。
I've created a dummy_database that I just made in sql server as well. I added an INSERT sql statement for the dummy table i have in there just to see if i could actually access a database. All the table takes in is the line number and time it was sent. When I run my windows service that database updates just fine.
任何帮助将是AP preciated。谢谢
Any help would be appreciated. Thanks
Dim conn As New SqlConnection(connString2)
sendDebugEmail("134")
SQL = "Select email FROM _Customer WHERE custID in (SELECT custID FROM _OnlineCustomer WHERE ExpirationDate <= '6-20-12' AND ExpirationDate >= '6-10-12')"
Dim cmd As New SqlCommand(SQL, conn)
sSubject = "hello"
sBody = "This is test data"
Dim dr As SqlDataReader
sendDebugEmail("143")
Try
dr = cmd.ExecuteReader() // This is were it stops
sendDebugEmail("147")
While dr.Read
sendDebugEmail("152")
Try
LogInfo("Service woke up")
Dim i As Integer = 0
' Prepare e-mail fields
sFrom = "test@gmail.com"
sTo = "test1@gmail.com"
sCc = "test2@gmail.com"
Dim oMailMsg As MailMessage = New MailMessage
oMailMsg.From = sFrom
oMailMsg.To = sTo
oMailMsg.Cc = sCc
' Call a stored procedure to process the current item
' The success message
oMailMsg.Subject = sSubject + "(Success)"
oMailMsg.Body = sBody + "Email has been sent successfully."
' Send the message
If Not (oMailMsg.To = String.Empty) Then
SmtpMail.Send(oMailMsg)
End If
Catch obug As Exception
LogEvent(obug.Message)
Finally
End Try
End While
Catch ex As Exception
Finally
dr.Close()
cmd.Dispose()
conn.Close()
conn.Dispose()
End Try
End Sub
/////////////////////////////////////////////// //////////////////////////////////
问题解决了:的设置我的连接,但我从来没有打开它
。
我需要conn.open()
/////////////////////////////////////////////////////////////////////////////////
Problem Solved: I set up my connection but I never opened it.
I needed conn.open()
这帮助最深的是添加此code到我的最后一个catch语句的事情:
The thing that helped me most was adding this code into my last catch statement:
sendDebugEmail(ex.Message & vbcrlf & ex.stackTrace)
这给我的堆栈跟踪的电子邮件,并使它很容易调试
It send me an email of the stackTrace and made it very easy to debug
推荐答案
问题解决了:的设置我的连接,但我从来没有打开它
。
我需要conn.open()
Problem Solved: I set up my connection but I never opened it.
I needed conn.open()
这帮助最深的是添加此code到我的最后一个catch语句的事情:
The thing that helped me most was adding this code into my last catch statement:
sendDebugEmail(ex.Message & vbcrlf & ex.stackTrace)
这给我的堆栈跟踪的电子邮件,并使它很容易调试
It send me an email of the stackTrace and made it very easy to debug
这篇关于无法在Windows服务执行SQL命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!