无法解决打开数据读取器错误(需要关闭)

Unable to resolve open data reader error (needs closing)

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

想法是让 getMaxID() return 字段中的最高值 + 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

您正在 ExecuteReader 之后执行 ExecuteScalar。之前需要关闭的ExecuteReader returns a reader。我建议您使用 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 服务器中已经存在的自动递增属性。