打开连接错误 [英] Open connection error

查看:106
本文介绍了打开连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:
我收到错误消息,您尝试打开已经打开的连接,尽管我总是检查连接是否关闭,然后才打开连接.
错误出在以下代码中的mycon.open() 中.

我想添加一些我的程序具有不同线程的东西,我不知道如果其中一个线程打开了连接,其他线程是否可以使用数据库.

我需要知道的是,如果每个函数都有自己的连接,那么两个函数可以同时使用数据库吗?

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

推荐答案

可以.尽管这取决于数据库允许的连接数.
另外,在这种情况下,您需要注意数据的一致性(使用并发和锁实现).
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.


根据本文,Jet引擎理论上可以支持255个连接,但是建议这样做您最多使用20个并发连接.


用于Microsoft Visual Studio 6.0的Microsoft数据引擎(MSDE):用于构建桌面和共享解决方案的Jet的替代方法 [ ^ ]

阅读以下部分-如何选择数据库引擎
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


这篇关于打开连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆