与此命令关联的DataReader已打开。必须首先关闭它

时间:2015-02-03 13:10:05

标签: sql sql-server vb.net

我尝试将一些值插入表中但发生此错误与此命令关联的DataReader已打开。必须首先关闭它 这是代码

Public Function AddDB(ByVal codeP As String, ByVal exigence As String, ByVal nomE As String, ByVal Dt As String)
    OpenCn()
    Dim paramID As New SqlParameter("@id", SqlDbType.Int)
    paramID.Value = 10
    Dim paramText As New SqlParameter("@text", SqlDbType.VarChar, 50)
    paramText.Value = "Ceci est un test"
    Dim paramDate As New SqlParameter("@date", SqlDbType.DateTime)
    paramDate.Value = DateTime.Now
    Dim paramFloat As New SqlParameter("@float", SqlDbType.Float)
    paramFloat.Value = 12.199999999999999

    Dim Sql As String = String.Format("INSERT INTO DossierBleu(CodeP, Exigences, NomE, Date) VALUES (@codeP, @exigence, @nomE, @date)",
        paramText.ParameterName,
            paramText.ParameterName,
            paramText.ParameterName,
            paramText.ParameterName,
            paramDate.ParameterName
          )


    Dim Cmd = New SqlCommand(Sql.ToString(), Cn)
    Cmd.Parameters.Add(New SqlParameter("@codeP", codeP))
    Cmd.Parameters.Add(New SqlParameter("@exigence", exigence))
    Cmd.Parameters.Add(New SqlParameter("@nomE", nomE))
    Cmd.Parameters.Add(New SqlParameter("@date", Dt))
    'Cmd.Parameters.Add(paramDate)
    Return Cmd.ExecuteNonQuery()
    CloseCn()
End Function

谢谢

1 个答案:

答案 0 :(得分:0)

如果您至少使用SQL Server 2005,则添加

MultipleActiveResultSets=True

连接字符串。