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

标签 sql database vb.net error-handling sqldatareader

错误:已经有与此命令关联的打开的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服务器中已经存在的自动增量属性。

关于sql - 无法解决打开的数据读取器错误(需要关闭),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27744588/

相关文章:

mysql - 如何在 SQL SERVER 中逐字反转字符串

具有自定义 'table-like' 输出的 mysql 过程

mysql - sql中更新语句中的算术

mysql - SQL根据最大值满足条件

java - delete sql语句违反完整性约束

java - 错误 DOMException : Failed to execute 'open' on 'XMLHttpRequest' : Invalid URL

sql - 跨多个用户的本地数据库的连接字符串

c# - 如何为用作 DataGrid 的 ItemsSource 的项目的集合属性中的每个项目生成并绑定(bind)一列

c# - 在事务中执行 SQL 更新批处理

mysql - Laravel 热切加载关系与添加的子句?