数据库连接未结束

时间:2012-02-29 06:16:40

标签: vb.net database-connection

我有以下代码将一些数据从GridView插入到oracle数据库。

Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        Dim con = New OleDbConnection("Data Source=sml2; User ID=sfpl; Password=a; provider=OraOLEDB.Oracle")
        con.Open()
        For Each gvRow As GridViewRow In GridView1.Rows
            Dim trndate As Date = ddlTrnDte.SelectedValue
            Dim ZoneNo As Integer = ddlZone.SelectedValue
            Dim DocNo As TextBox = CType(gvRow.FindControl("txtDocNo"), TextBox)
            Dim VehCod As DropDownList = CType(gvRow.FindControl("ddlVeh"), DropDownList)
            Dim CircleNo As DropDownList = CType(gvRow.FindControl("ddlCircle"), DropDownList)
            Dim Contractor As DropDownList = CType(gvRow.FindControl("ddlContractor"), DropDownList)
            Dim Supplier As DropDownList = CType(gvRow.FindControl("ddlSupplier"), DropDownList)
            Dim NetWt As TextBox = CType(gvRow.FindControl("txtNetWt"), TextBox)
            Dim Rate As TextBox = CType(gvRow.FindControl("txtRate"), TextBox)
            Dim SNF As TextBox = CType(gvRow.FindControl("txtSNF"), TextBox)
            Dim FAT As TextBox = CType(gvRow.FindControl("txtFAT"), TextBox)
            Dim LR As TextBox = CType(gvRow.FindControl("txtLR"), TextBox)
            Dim TS As TextBox = CType(gvRow.FindControl("txtTS"), TextBox)
            'Create Connection


            Dim cmd As OleDbCommand = New OleDbCommand("Insert into MLK_02_01 (TRN_DTE,ZONE_NO,CIRCLE_NO,CNT_NO,DOC_NO,SUP_COD,VEH_NUM,NET_WEIGHT,Q_SNF,Q_FAT,Q_LR,Q_TS,RATE,veh_cod) VALUES (:TRN_DTE,:ZONE_NO,:CIRCLE_NO,:CNT_NO,:DOC_NO,:SUP_COD,:VEH_NUM,:NET_WEIGHT,:Q_SNF,:Q_FAT,:Q_LR,:Q_TS,:RATE,:veh_cod)", con)
            cmd.Parameters.Clear()
            'TrnDate
            cmd.Parameters.Add(":trn_dte", (OleDb.OleDbType.Date))
            cmd.Parameters(":trn_dte").Value = DateTime.Now()
            cmd.Parameters(":trn_dte").Value = ddlTrnDte.SelectedValue
            cmd.Parameters.AddWithValue(":ZONE_no", ZoneNo)
            cmd.Parameters.AddWithValue(":CIRCLE_no", CircleNo.SelectedValue)
            cmd.Parameters.AddWithValue(":CNT_no", Contractor.SelectedValue)
            cmd.Parameters.AddWithValue(":Doc_No", DocNo.Text)
            cmd.Parameters.AddWithValue(":sup_cod", Contractor.SelectedValue)
            cmd.Parameters.AddWithValue(":Veh_num", VehCod.SelectedItem.Text)
            cmd.Parameters.AddWithValue(":NET_WEIGHT", NetWt.Text)
            cmd.Parameters.AddWithValue(":Q_SNF", SNF.Text)
            cmd.Parameters.AddWithValue(":Q_FAT", FAT.Text)
            cmd.Parameters.AddWithValue(":Q_LR", LR.Text)
            cmd.Parameters.AddWithValue(":Q_TS", TS.Text)
            cmd.Parameters.AddWithValue(":rate", Rate.Text)
            cmd.Parameters.AddWithValue(":veh_cod", VehCod.SelectedValue)


            cmd.ExecuteNonQuery()
            Label1.Text = "All Records are Saved Successfully"
            con.Close()
            btnInsert.Enabled = False
        Next

    End Sub

数据被很好地插入数据库,但是当表单关闭时,数据库连接不会结束。我需要帮助来结束数据库会话以消除进一步的通信错误。

1 个答案:

答案 0 :(得分:0)

你提到它是一个页面,所以我假设这是一个ASP页面。

IIS默认启用数据库连接池。这意味着即使代码关闭了连接,IIS也会保持连接,以便使下一个请求的下一个查询执行得更快。

要禁用连接池,请在数据库连接字符串中添加以下内容:

  

池=假

New OleDbConnection("Data Source=sml2; User ID=sfpl; Password=a; provider=OraOLEDB.Oracle; Pooling=false")

此外,执行更改后:

cmd.ExecuteNonQuery()
cmd.Dispose()

con.Close()之后:

con.Close()
con.Dispose()

那应该处理命令和连接对象。