错误:已经有与此命令关联的打开的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

最佳答案

您正在执行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服务器中已经存在的自动增量属性。

10-07 19:13
查看更多