【问题标题】:Unable to resolve open data reader error (needs closing)无法解决打开数据读取器错误(需要关闭)
【发布时间】:2015-01-02 15:39:58
【问题描述】:

错误:已经有一个打开的 DataReader 与此命令关联,必须先关闭

这个想法是让 getMaxID() 返回字段中的最大值 + 1- 将其设置为新的 ReportID(现在)。但是我不断收到上述错误消息。我试图将 getMaxID() 传递给变量,然后将变量分配给@ReportID,但仍然出现错误。我也尝试过使用 conn.close(),但没有运气。任何帮助或建议将不胜感激。

我在这里查看了其他答案,但仍然无法摆脱错误。

 Private Sub addReport()

            Dim Str As String = _
           <String> INSERT INTO
                        Report(
                            ReportID, 
                            ScoutID, 
                            FixtureID, 
                            PlayerID, 
                            ReportDate,
                            Comments) 
                    VALUES(
                            @ReportID,
                            '2',
                            '3',
                            '6',
                            '10/15/2014',
                            @comments) 
           </String>

            Try
                Using conn As New SqlClient.SqlConnection(DBConnection)
                    conn.Open()
                    Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
                        cmdQuery.Parameters.Add("@ReportID", SqlDbType.Int).Value = getMaxID("ReportID", "Report")       
                        cmdQuery.Parameters.Add("@Comments", SqlDbType.VarChar).Value = txtComments.Text
                        cmdQuery.ExecuteNonQuery()
                    End Using
                End Using

                MsgBox("Report Added")

            Catch ex As Exception
                MsgBox("Add Report Exception: " & ex.Message & vbNewLine & Str)
            End Try

        End Sub

            Public Function getMaxID(ByVal fieldName As String, ByVal tableName As String) As Integer

                Dim newID As Integer
                Dim Str = "SELECT MAX(" & fieldName & ") FROM " & tableName & ""

                Try
                    Using conn As New SqlClient.SqlConnection(DBConnection)
                        conn.Open()
                        Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
                            cmdQuery.ExecuteReader()

                            If IsDBNull(cmdQuery.ExecuteScalar()) = True Then
                                newID = 1
                            Else
                                newID = cmdQuery.ExecuteScalar()
                                newID = newID + 1
                            End If

                        End Using
                    End Using

                Catch ex As Exception
                    MsgBox("Generate Max ID Exception: " & ex.Message & vbNewLine & Str)
                End Try

                Return newID

            End Function

【问题讨论】:

  • 在 getMaxID 中,您在 ExecuteReader 之后执行 ExecuteScalar。 ExecuteReader 没有意义,关闭阅读器或移除 ExecuteReader。
  • 除了上面的代码之外,如果它不为空,您的代码将 ExecuteScalar 两次。将调用结果存储在局部变量中并对其进行测试,而不是调用两次。
  • 成功了,谢谢你的帮助!

标签: sql database vb.net error-handling sqldatareader


【解决方案1】:

您在 ExecuteReader 之后执行 ExecuteScalar。 ExecuteReader 返回一个需要关闭的阅读器。我建议你在 sql 中使用 IsNull 函数来简化你的函数。

        Public Function getMaxID(ByVal fieldName As String, ByVal tableName As String) As Integer

            Dim newID As Integer
            Dim Str = "SELECT IsNull(MAX(" & fieldName & "), 0)+1 FROM " & tableName & ""

            Try
                Using conn As New SqlClient.SqlConnection(DBConnection)
                    conn.Open()
                    Using cmdQuery As New SqlClient.SqlCommand(Str, conn)
                        newID = cmdQuery.ExecuteScalar()
                    End Using
                End Using

            Catch ex As Exception
                MsgBox("Generate Max ID Exception: " & ex.Message & vbNewLine & Str)
            End Try

            Return newID

        End Function

我强烈建议您不要为 sql 连接字符串,并且如果可能,请使用 sql server 中已经存在的自动增量属性。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 2013-07-23
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多