-1

嘿伙计们,我需要一些关于我的代码的帮助,我真的不知道它关闭连接的部分在哪里......我想要的是继续添加记录,直到我从 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

非常感谢您的帮助

4

1 回答 1

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

这不会关闭所有打开的连接吗?

于 2012-11-21T06:15:50.570 回答