通过类对象Visual Basic 2010连接到数据库

时间:2013-08-30 18:39:38

标签: mysql vb.net visual-studio-2010 database-connection

我有2个班级

连接课程

Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class connect
Dim dbCon As MySqlConnection
Dim strQuery As String = ""
Dim SqlCmd As MySqlCommand
Dim DR As MySqlDataReader

Public Function Con2Db() As Boolean

    Try
        'Prepare connection and query
        dbCon = New MySqlConnection("Server=localhost; User Id = root; Pwd = 12345; Database = digitallibrary")
        If dbCon.State = ConnectionState.Closed Then
            dbCon.Open()
            Return True
        Else
            dbCon.Close()
            splash.Label1.Text = "Connection is Close"
            Return False
        End If
    Catch ex As Exception
        MsgBox("FAIL")
        Return False
    End Try

End Function


End Class

并查询Class

Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class query
Dim dbCon As MySqlConnection
Dim strQuery As String = ""
Dim SqlCmd As MySqlCommand
Dim DR As MySqlDataReader
Public Sub insert(ByVal ln As String, ByVal fn As String, ByVal mn As String, ByVal user As String, ByVal email As String, ByVal bdate As String, ByVal jdate As String, ByVal jtime As String, ByVal pwd As String)
    Try
        strQuery = "INSERT INTO user_tbl(user_ln,user_fn,user_mn,username,user_email,user_bdate, user_jdate, user_jtime)VALUES('" + ln + "','" + fn + "','" + mn + "','" + user + "','" + email + "','" + bdate + "','" + jdate + "','" + jtime + "' );" & _
            "INSERT INTO login_tbl(username,password)VALUES('" + user + "','" + pwd + "')"
        SqlCmd = New MySqlCommand(strQuery, dbCon)
        SqlCmd.ExecuteNonQuery()
        dbCon.Close()

    Catch ex As Exception
        MsgBox("Error " & ex.Message)
    End Try
End Sub

End Class

也是注册表格

Public Class registration

Private Sub registration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim con As New connect
    If (con.Con2Db = True) Then
        Label13.Text = "Connected To Database"
    Else
        Label13.Text = "Not Connected To Database"
    End If
End Sub

Private Sub submit_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles submit_btn.Click
    Dim ins As New query
    Dim ln As String = ln_txt.Text
    Dim fn As String = fn_txt.Text
    Dim mn As String = mn_txt.Text
    Dim user As String = user_txt.Text
    Dim pwd As String = pwd_txt.Text
    Dim cpwd As String = cpwd_txt.Text
    Dim email As String = email_txt.Text
    Dim year As String = year_cbx.Text
    Dim month As String = month_cbx.Text
    Dim day As String = day_cbx.Text
    Dim bdate As String = year + "-" + month + "-" + day
    Dim jdate As String = Format(Date.Now, "yyyy-MM-dd")
    Dim jtime As String = Format(Date.Now, "HH:mm:ss")
    ins.insert(ln, fn, mn, user, email, bdate, jdate, jtime, pwd)
End Sub
End Class

一切都很好,它在标签上说连接成功但是当我运行代码时它会出错

错误连接必须有效并且打开

我不明白为什么当函数返回true时连接被认为是关闭的......

如果人们想知道为什么要将它分成每个类,那是因为我正在尝试干净地编码(我希望并且想到)并且我希望在编程中更灵活

1 个答案:

答案 0 :(得分:1)

问题在于你的连接' class与你的' insert'没关系功能,意味着您在每个连接中创建一个新连接。您需要做的是创建一个与您的连接类将管理的数据库的共享连接,以及您的插入'函数将使用现有连接。

将代码分成不同的部分没有任何问题,但您必须知道如何有效地执行此操作。我通常在相同的部分中保持连接管理和查询执行,以避免传递额外的对象。这就是我要做的事情:

Imports MySql.Data
Imports MySql.Data.MySqlClient
Public Class QueryManager

Dim dbCon As MySqlConnection

Public Sub ManageConnection(ByVal CloseConnection As Boolean)
    Try
        'Prepare connection and query'
        dbCon = New MySqlConnection("Server=localhost; User Id = root; Pwd = 12345; Database = digitallibrary")
        If CloseConnection = False Then
            If dbCon.State = ConnectionState.Closed Then _
                dbCon.Open()
        Else
            dbCon.Close()
        End If
    Catch ex As Exception
        MsgBox("FAIL")
    End Try

End Sub

Public Sub Insert(ByVal ln As String, ByVal fn As String, ByVal mn As String, ByVal user As String, ByVal email As String, ByVal bdate As String, ByVal jdate As String, ByVal jtime As String, ByVal pwd As String)
    Try
        ManageConnection(True) 'Open connection'

        Dim strQuery As String = "INSERT INTO user_tbl(user_ln,user_fn,user_mn,username,user_email,user_bdate, user_jdate, user_jtime)" & _
            "VALUES('" + ln + "','" + fn + "','" + mn + "','" + user + "','" + email + "','" + bdate + "','" + jdate + "','" + jtime + "' );" & _
            "INSERT INTO login_tbl(username,password)VALUES('" + user + "','" + pwd + "')"

        Dim SqlCmd As New MySqlCommand(strQuery, dbCon)
        SqlCmd.ExecuteNonQuery()

        ManageConnection(False) 'Close connection'

    Catch ex As Exception
        MsgBox("Error " & ex.Message)
    End Try
End Sub

End Class

您也不再需要注册_注册' sub,因为连接只会在使用时打开。如果您想创建一个共享连接并将其保留在整个应用程序中,您可以调整功能以反映您的需求。