Click here to Skip to main content
15,918,211 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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?

VB
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
Posted
Updated 26-Jun-11 2:58am
v3

Yes it can. Though it depends on the number of connections the DB allows.
Also, you need to watch out for data consistency (achieved using concurrency and locks) in such scenarios.
 
Share this answer
 
According this article, the Jet engine can theoretically support 255 connections, But it is recommended that you use a maximum of 20 concurrent connections.


Microsoft Data Engine (MSDE) for Microsoft Visual Studio 6.0: An Alternative to Jet for Building Desktop and Shared Solutions[^]

Have a read of the section - How to Choose a Database Engine
 
Share this answer
 
v2
Comments
muathvb1 27-Jun-11 1:48am    
please if it is possible that you tell me what is wrong in my code above, that cause the error in the line mycon.open()
Simon_Whale 27-Jun-11 3:57am    
Whats the error message?
muathvb1 27-Jun-11 10:34am    
You attempted to open a database that is already opened by the user admin on machine mypc
Simon_Whale 27-Jun-11 10:39am    
REad this article

http://www.fryan0911.com/2008/01/solving-access-error-you-attempted-to.html

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900