SQL データベースから結果を取得するコードで奇妙な問題が発生しています。以下にコードを示しますが、その基本は次のとおりです。
接続が開かれ、すぐにクエリが実行されます。8/10 回は問題なく動作しますが、変更されていない同じデータベースに対するまったく同じクエリであっても、不可解なことに、実行されたクエリが結果を返さないことがあります。接続が開かれてから閉じられるまでの間に threading.sleep 関数を挿入すると、この問題は完全に解消されることに気付きましたが、明らかな理由からそれは理想的ではありません。
接続のステータスを照会して準備ができているかどうかを確認しようとしましたが、DB から結果が得られない場合でも、常に開いていると報告されます。
誰にもアイデアはありますか?
'Runs the passed query and returns each row as an object within an ArrayList
Function Get_Results(ByVal query As String, ByVal ConnectionStringName As String) As ArrayList
Dim sqlComm As SqlCommand = Get_Connection(query, ConnectionStringName)
'Open that connection
sqlComm.Connection.Open()
'This part doesn't work because the connection will always report that it's open.
While sqlComm.Connection.State <> Data.ConnectionState.Open
Threading.Thread.Sleep(100)
End While
'pause and wait for the connection to fully open (maybe?)
'Threading.Thread.Sleep(100)
'Execute the query and store all of the results into the SqlDataReader.
Dim sqlRead As SqlDataReader = sqlComm.ExecuteReader()
Dim result As ArrayList = New ArrayList
'Read each row one by one.
While sqlRead.Read()
'Create an object of the size needed.
Dim row(sqlRead.FieldCount - 1) As Object
'Fill the row object with the values.
sqlRead.GetValues(row)
'Add the result object to the array.
result.Add(row)
End While
'Close all open connections to the database.
sqlRead.Close()
sqlComm.Connection.Close()
Return result
End Function