连接没有关闭。连接的当前状态是打开的

时间:2011-12-19 04:42:39

标签: vb.net ado.net

Try
    If functionmode = "ADD" Then
        SQLStr = "INSERT INTO boatmast VALUES ('" & gBoatType & "','" & TxtBoatCode.Text & "','" & TxtBoatName.Text & "','" & Format(txtBP.Text, "##0.#0") & "','" & Format(txtBPM.Text, "##0.#0") & "','" & Format(txtLDA.Text, "##0.#0") & "','" & , 'bpk', '" & Today & "', '" & updtime & "', 'bpk', '" & Today & "', '" & updtime & "')"
    End If
    conn.Open()
    SQLCmd.Connection = conn
    SQLCmd.CommandText = SQLStr
    SQLCmd.ExecuteNonQuery()
    conn.Close()


Catch ex As OdbcException
    MsgBox(ex.ToString)
Finally
    conn.Close()
End Try

1 个答案:

答案 0 :(得分:4)

问题是你两次调用conn.Close()。保留你的finally块上的Close连接。

Try

    If functionmode = "ADD" Then
                                        //Supposed this is you columnNAmes                                                     //Set parameters                 
        SQLStr = "INSERT INTO boatmast (gBoatType,BoatCode,BoatName,BP, BPM,LDA,bpk,Today,updtime,bpk2,Today2,updtime2) VALUES (@gBoatType,@BoatCode,@BoatName,@BP, @BPM,@LDA,@bpk,@Today,@updtime,@bpk2,@Today2,@updtime2)"

      SQLCmd.Parameters.AddWithValue("@gBoatType",gBoatType)
      //DO OTHER STUFF TIL @updtime2            

    conn.Open()
    SQLCmd.Connection = conn
    SQLCmd.CommandText = SQLStr
    SQLCmd.ExecuteNonQuery() 

  End If
Catch ex As OdbcException
    MsgBox(ex.ToString)
Finally
     If conn.State = ConnectionState.Open Then conn.Close()
End Try

此致