什么关闭这个连接?

时间:2012-11-21 05:46:30

标签: mysql vb.net

嘿家伙我需要一些关于我的代码的帮助我真的不知道它关闭连接的部分在哪里....我想要的是继续添加记录,直到我从msgbox点击否

继承我的代码:

    Dim connstr As String = "server=localhost; uid= root; password=root; database=lmsdbase;"
Dim conn As MySqlConnection = New MySqlConnection
Dim ds As New DataSet
Dim da As MySqlDataAdapter


    conn.ConnectionString = connstr



    Try
        If conn.State = ConnectionState.Closed Then
            conn.Open()
        Else
            conn.Close()
            MsgBox("Database Connection Error")
        End If
    Catch ex As Exception
        MsgBox(Err.Description)

        txtBookISBN.Focus()

    End Try



    Dim SqlStatement As String = "INSERT INTO tblIssue(ISBN, Title, BorrowerID, Name, Occupation, BorrowerType, School, DateIssued, DateToBeReturned) VALUES('" & txtBookISBN.Text & "','" & txtBTitle.Text & "','" & txtMemID.Text & "','" & txtMemName.Text & "','" & txtOccupation.Text & "','" & cboBtype.Text & "','" & txtSchool.Text & "','" & DateTimePicker1.Text & "','" & DateTimePicker2.Text & "')"
    Save(SqlStatement)

   Public Sub Save(ByRef SqlStatement As String)
    Dim cmd As MySqlCommand = New MySqlCommand

    With cmd

        .CommandText = SqlStatement
        .CommandType = CommandType.Text
        .Connection = conn
        .ExecuteNonQuery()

    End With

    'conn.Close()
    MsgBox("Success!")


    If MsgBox("Do You Want to Add New Record?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then



        txtMemID.Text = ""
        txtMemName.Text = ""
        txtBookISBN.Text = ""
        txtBTitle.Text = ""
        txtOccupation.Text = ""
        cboBtype.Text = ""
        txtSchool.Text = ""

        txtMemID.Focus()

    Else


        Me.Close()


    End If

    conn.Dispose()

End Sub

非常感谢您的帮助

1 个答案:

答案 0 :(得分:1)

If conn.State = ConnectionState.Closed Then
            conn.Open()
        Else
            conn.Close()
            MsgBox("Database Connection Error")

这不会关闭所有Open连接吗?