试图在一个连接中运行2个查询

问题描述 投票:0回答:2

我正在尝试运行一小段代码,首先检查该值是否存在,如果不存在,则更新数据库。我的问题是我可以运行一个查询,也可以运行另一个查询,因为不能运行两个查询,因为这样它就表明数据库是打开的,无法访问。我知道我可以写数据库。它没有读取保护,位于我无法访问的位置,等等。如果我只是尝试更新而不检查,该例程就会运行。该检查肯定没有记录。我尝试了使用完全不同的变量进行连接的单独例程。我很沮丧。我尝试关闭,使用,处置,但是我需要比我聪明得多的人来告知我我做错了什么...

For Each strfile As String In flist
    Dim pth As String = Path.GetDirectoryName(strfile)
    Dim objReader As New System.IO.StreamReader(strfile)
    Dim dVal As String = String.Empty
    TextLine = objReader.ReadLine()
    SplitLine = Split(TextLine, ",")
    dVal = SplitLine(1)

    Using Dcon As New OleDbConnection
        Dcon.ConnectionString = dbProvider
        Dcon.Open()

        Dim q1 As String = "SELECT Shares.[_Date] FROM Shares WHERE (((Shares.[_Date])=" & dVal & "))"
        Dim comd As OleDbCommand = New OleDbCommand(q1, Dcon)
        comd.ExecuteReader()

        Dim q2 As String = "INSERT INTO Shares (Code, _Date, _Open, _High, _Low, _Close, _Volume) " &
                                                                  "SELECT F1, F2, F3, F4, F5, F6, F7 FROM [Text;HDR=NO;DATABASE=" & pth & "].[" & Path.GetFileName(strfile) & "];"

        Dim cmd As OleDbCommand = New OleDbCommand(q2, Dcon)

        cmd.ExecuteNonQuery()
        Dcon.Close()
    End Using
Next

我尝试了单独的例程,尝试打开,关闭然后重新打开。我尝试了一种连接,我尝试了两种。有人知道我在做什么错吗,为什么它总是告诉我数据库是专门打开的,为什么它不让我写呢?

先感谢您的帮助。

sql database vb.net
2个回答
0
投票

实际的错误消息会告诉您,连接上已经有打开的阅读器。显而易见的解决方案是关闭您打开的数据读取器。

您的代码存在一些严重问题。您调用ExecuteReader,这将打开阻止第二条命令的数据读取器,但实际上您从未对该数据读取器执行任何操作。您没有将其分配给变量,因此您无法从中获取数据,也无法将其关闭。您可以这样做:

Dim reader = comd.ExecuteReader()

'Use reader here.

reader.Close()

正确的方法是使用Using块:

Using reader = comd.ExecuteReader()
    'Use reader here.
End Using

就是说,如果您要做的只是确定数据是否存在并且不实际使用该数据,那么您根本不需要数据读取器。只需使用返回Boolean的查询并调用ExecuteScalar,例如

Using connection As New OleDbConnection("connection string here")
    connection.Open()

    Dim query As New OleDbCommand("SELECT COUNT(*) > 0 FROM MyTable", connection)

    If CBool(query.ExecuteScalar()) Then
        'The table does contain data.
    End If
End Using

根据数据库,您也许可以使用更有效的查询。


0
投票

当我尝试执行多个sql语句时,我使用此代码:

Dim ConnectionString As String = ("Connection_String")
        'LoginsVal is a Table,
        Dim SqlStr1 As String = ("Select Count(Accounts.DtCrtd) FROM Accounts WHERE Accounts.DtCrtd > " & Now.Date & ";")
        Dim SqlStr2 As String = ("INSERT INTO LoginsVal(LoginNm,DtMdfd) VALUES ('Test1'," & Now.Date & ");")
        'DtCtrtd is Date DataType, Accounts is the Table Name
        Dim ThisCmd1, ThisCmd2 As New OleDbCommand
        Using ThisConn As New OleDbConnection With {.ConnectionString = ConnectionString}
            Try
                If ThisConn.State <> ConnectionState.Open Then
                    ThisConn.Open()
                    Debug.WriteLine("Database : Open")
                End If
            Catch ex As OleDbException
                Debug.WriteLine(ex.Message)
            End Try
            With ThisCmd1
                .Connection = ThisConn
                .CommandType = CommandType.Text
                .CommandText = SqlStr1
            End With
            'Debug.WriteLine(ThisCmd1.ExecuteScalar) 'use to test only without if statement below.
            If IsDbNull(ThisCmd1.ExecuteScalar) Then
                Debug.WriteLine("No records found")
            Else
                'Your Next SqlStatement
                With ThisCmd2
                    .Connection = ThisConn
                    .CommandType = CommandType.Text
                    .CommandText = SqlStr2
                End With
                Debug.WriteLine("Num of Rows affects is : " & ThisCmd2.ExecuteNonQuery)
            End If
            ThisConn.Close()
        End Using
© www.soinside.com 2019 - 2024. All rights reserved.