ExecuteScalar hangs (1 Viewer)

Milton

New member
Local time
Today, 07:37
Joined
Mar 5, 2008
Messages
1
I have a method that calls ExecuteScalar for a Count(*) sql statement. It works fine for several iterations then all of a sudden it goes off and never returns. When it hangs it takes over 100% of my CPU. I get no error messages back so I have no clue what I am doing wrong. I have isolated my method from the rest of the code and open up and close the database connection within the method. Following is my isolated code. The odd thing is that when we upgraded our environment from ESRI Arc 9.1 to 9.2, visual studio 2005 and .NET Framework 2.x this error showed up. Works perfectly in ESRI 9.1, VS 2003 and .NET Framework 1.x. Any suggestions would be greatly appreciated.

Friend Function testScaler() As Integer
Dim sqlStr As String = "SELECT COUNT(*) FROM maps " & _
"WHERE [proj_id] = ? " & _
"AND [data_name] = ?" ' $NON-NLS$

Dim connectionString As String = _
"Provider=" & PaExtension.paProject.dbProvider & ";" & _
"Data Source=" & PaExtension.paProject.dbDataSource & ";"

Dim con As OleDbConnection = New OleDbConnection
con.ConnectionString = connectionString
con.Open()
Dim cmd As OleDbCommand = New OleDbCommand
cmd.Connection = con
cmd.CommandType = CommandType.Text
cmd.CommandText = sqlStr

addParameter(cmd, "[proj_id]", Me.projId) ' $NON-NLS$
addParameter(cmd, "[data_name]", Me.dataName) ' $NON-NLS$

Dim count As Integer
Dim bob As Object = Nothing
bob = cmd.ExecuteScalar() 'This is where after 4-5 times working it finally
' runs away and never returns.

If IsNothing(bob) Then
count = 0
Else
count = CInt(bob)
End If


cmd.Dispose()
cmd = Nothing
con.Dispose()
con = Nothing

Return count

End Function
 

MarkK

bit cruncher
Local time
Today, 06:37
Joined
Mar 17, 2004
Messages
8,179
A few things I would change:
- Open your connection just before executing the command.
- Close your connection! Do this just after executing the command.
- Don't explicitly .Dispose your variables. Leave this to the GC.
 

dan-cat

Registered User.
Local time
Today, 14:37
Joined
Jun 2, 2002
Messages
3,433
Yes as Lagbolt says, not closing your connection could easily cause this.

A nice easy way to remember this is like this:

Code:
Using myConn as new sqlconnection("myconnectionstring")

Using myComm as new sqlcommand("myCommandString",myConn)

myConn.Open()

...

End Using

End Using

This way all your command and connection objects get closed up for you.
 

Users who are viewing this thread

Top Bottom