jerry.sommerville@crown.c
New member
I have a function that opens a DB, counts the number of records in a table, then closes the database. The first iteration it works perfectly, the second and subsequent passes it fails but without any exceptions being thrown. Here is the offending (test) code. Can anyone tell me if there is some close or garbage collection that needs to take place before the subsequent calls? The DB can be any Sql Server DB. I count a table in Master DB only to check to see if the server is alive.
VB.NET:
Imports system.data
Imports system.data.sqlclient
Public Class Form1
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim x As Integer
Dim msgstr As String
For i = 1 To 2
RadioButton1.BackColor = Me.BackColor
If Not DatabaseAlive("Smaug") Then RadioButton1.BackColor = Color.Red
msgstr = CType(i, String)
MsgBox(msgstr, MsgBoxStyle.OkOnly)
For x = 1 To 10000000
' pause for some time
Next x
Next i
End Sub
Private Function DatabaseAlive(ByVal strName As String)
Dim strConn = "Server=" + strName + ";Database=Master;Trusted_Connection=True;"
Dim sqlConn As SqlConnection = New SqlConnection(strConn)
sqlConn.Open()
' Define command
Dim sqlstr As String = "select count(*) from spt_monitor"
Dim cmdScalar As SqlCommand = New SqlCommand(sqlstr, sqlConn)
cmdScalar.CommandType = CommandType.Text
Dim intCount As Integer = CInt(cmdScalar.ExecuteScalar)
' Close the connection
sqlConn.Close()
Return intCount
End Function
End Class
Last edited by a moderator: