I have the following code:
I got the error you try to open a connection which is already open, although I always check if the connection is closed then I open the connection.
The error was in the
mycon.open()
in the code below.
I want to add somthing my program have different threads, I don't know that if one of the threads open the connection does the other threads can use the database.
What I need to know can two functions use the database simulatinously if each one has it is own connection or not?
Sub get_volume_small(ByVal level As Single, ByRef hieght As List(Of Single), ByRef volume As List(Of Single), ByVal id As Integer)
Dim mycon As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + My.Computer.FileSystem.CurrentDirectory + "\gas.accdb';Persist Security Info=True")
Try
If mycon.State = ConnectionState.Closed Then
mycon.Open()
End If
Dim cmd As New OleDbCommand("select * from small_tank where tank_id = " + id.ToString + " and lenght<=" + level.ToString + " order by lenght DESC", mycon)
Dim adp As New OleDbDataAdapter(cmd)
Dim mytable As New DataTable
adp.Fill(mytable)
Dim cmd2 As New OleDbCommand("select * from small_tank where tank_id = " + id.ToString + " and lenght>=" + level.ToString + " order by lenght", mycon)
Dim adp2 As New OleDbDataAdapter(cmd2)
Dim mytable2 As New DataTable
adp2.Fill(mytable2)
hieght.Add(0)
hieght.Add(0)
volume.Add(0)
volume.Add(0)
If mytable.Rows.Count > 0 Then
hieght.Item(0) = CType(mytable.Rows(0).Item(0), Single)
volume.Item(0) = CType(mytable.Rows(0).Item(1), Single)
Else
hieght.Item(0) = CType(mytable2.Rows(0).Item(0), Single)
volume.Item(0) = CType(mytable2.Rows(0).Item(1), Single)
End If
If mytable2.Rows.Count > 0 Then
hieght.Item(1) = CType(mytable2.Rows(0).Item(0), Single)
volume.Item(1) = CType(mytable2.Rows(0).Item(1), Single)
Else
hieght.Item(1) = CType(mytable.Rows(0).Item(0), Single)
volume.Item(1) = CType(mytable.Rows(0).Item(1), Single)
End If
cmd.Dispose()
cmd2.Dispose()
mycon.Close()
mycon.Dispose()
mytable.Dispose()
mytable2.Dispose()
Catch ex As Exception
MsgBox(ex.Message & "(" & Microsoft.VisualBasic.Right(ex.StackTrace, 5) & ")", , "get_volume_small db_functions")
Finally
If mycon.State = ConnectionState.Open Then
mycon.Close()
mycon.Dispose()
End If
End Try