|
-
Mar 23rd, 2016, 01:41 PM
#1
Thread Starter
Lively Member
Working with SQL Connections
Hi
This might sound like a really simple question, but I really hope someone out there can help me with something I can't seem to get my head around.
I'm in the process of writing a Windows Service. When the service starts, it logs that fact in a database table and then goes on to generate an email advising that the service has been started. So far, so good.
Once that step has been completed, the Service makes a series of calls to a configuration table in the back-end database which contains various settings that can be used to control the behaviour of the service. My issue at the moment is that I've been testing the service quite happily in my development environment, but when I roll it out to what will eventually become the production server, I'm getting a number of errors, one of which "ExecuteReader requires an open and available Connection. The connection's current state is connecting." suggests that the service is unable to retrieve one (or more) of the configuration settings because the connection hasn't opened quickly enough.
The code that I'm trying to use is this:
Code:
Dim sReturn As String = ""
'Open the connection if necessary
If cGlbDB.cmd.Connection.State = ConnectionState.Closed Then cGlbDB.cmd.Connection.Open()
'Execute the query and return the values
cGlbDB.reader = cGlbDB.cmd.ExecuteReader
While cGlbDB.reader.Read
If Len(sReturn) > 0 Then sReturn &= ";"
sReturn &= LTrim(RTrim(cGlbDB.reader("ConfigValue")))
End While
Return sReturn
For brevity, I haven't included the Try..Catch block which starts before the comment and finishes after the Return statement. My problem is this: If the code that's trying to Open the Connection isn't able to do so instantly, this means that the Connection is (presumably) sitting in a state of "Connecting...". Is there a graceful way of coding this so that the application waits until the Connection is fully open before proceeding with the ExecuteReader etc.? The only way I can think of to do it is with a Do..Loop Until clause, with nothing in the actual Do section, but I wondered if there was a nicer way of doing it.
TIA
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|