Mysql 访问;“全部”;将Class1.vb中声明的变量转换为vb.net中多种形式的解决方案

Mysql 访问;“全部”;将Class1.vb中声明的变量转换为vb.net中多种形式的解决方案,mysql,vb.net,Mysql,Vb.net,在下载了一系列连接器和一些插件之后,我终于成功地将xampp连接到VB.Net。尽管我发现每次我都会在每个需要sqlinsert和select语句的表单中放置一个连接字符串,这非常耗时/麻烦,所以我想到了创建一个Class1.vb并将所有连接字符串放在那里到xampp。因此,每次创建新表单时,我所要做的就是将导入到每个表单中,这样我就可以作为新的MySqlConnection访问Dim conn,正如我提到的Class1.vb Public Class Class1 Dim conn As Ne

在下载了一系列连接器和一些插件之后,我终于成功地将xampp连接到VB.Net。尽管我发现每次我都会在每个需要
sqlinsert和select语句的表单中放置一个连接字符串,这非常耗时/麻烦,所以我想到了创建一个Class1.vb并将所有连接字符串放在那里到xampp。因此,每次创建新表单时,我所要做的就是将
导入到每个表单中,这样我就可以作为新的MySqlConnection访问
Dim conn
,正如我提到的
Class1.vb

Public Class Class1
Dim conn As New MySqlConnection
Dim command As MySqlCommand
Dim reader As MySqlDataReader

Public Sub mysqlConnect()

If Not conn Is Nothing Then conn.Close()
    conn.ConnectionString = String.Format('connectionString for Dbname,server,uid,pword)
    Try
        conn.Open()

        MessageBox.Show("Connected!")
    Catch x As Exception
        MsgBox(x.Message)
    End Try
    conn.Close()
end sub
end class
尽管如此,我还是以某种方式连接到了
MYSQL
大多数变量,包括带有
dbtable
名称的变量,都无法访问。 让我向您展示我的
Class1.vb中的内容

Public Class Class1
Dim conn As New MySqlConnection
Dim command As MySqlCommand
Dim reader As MySqlDataReader

Public Sub mysqlConnect()

If Not conn Is Nothing Then conn.Close()
    conn.ConnectionString = String.Format('connectionString for Dbname,server,uid,pword)
    Try
        conn.Open()

        MessageBox.Show("Connected!")
    Catch x As Exception
        MsgBox(x.Message)
    End Try
    conn.Close()
end sub
end class
内置Form1.vb

Public Class Form1
Dim newCon as New Class1   


Private Sub Form2_Load
newCon.mysqlConnect()
'Note: mysqlConnect() is a function declared in Class1.vb


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Try
        conn.Open()
    Catch x As Exception
    End Try
    Dim command As New MySqlCommand(String.Format("INSERT STATEMENT"))
    command.ExecuteNonQuery()
    conn.Close()
    End Sub
但是我在Button1_Click上指出的变量不起作用。这就好像它无法读取
公共子连接()的某些函数一样
不过,正如我前面提到的,Msgbox(“Connected”)确实可以工作,这意味着我完全与我的
DB
连接

朋友们,有什么想法吗

非常感谢您的评论。提前感谢。

两种常见的解决方案是: 1) 将
Class1
更改为具有“工厂方法”,该方法将为您提供开放连接。 2) 将您的连接包装到类中

例1:

Public Class Class1
Dim conn As New MySqlConnection
Dim command As MySqlCommand
Dim reader As MySqlDataReader


Public Shared Function mysqlConnect() AS MySqlConnection

    If Not conn Is Nothing Then conn.Close()
    conn.ConnectionString = String.Format('connectionString for Dbname,server,uid,pword')
    Try
        conn.Open()

        ''MessageBox.Show("Connected!")
    Catch x As Exception
        MsgBox(x.Message)
    End Try
    ''conn.Close() ''This must be done by your calling function now
    ''btw, if you forget, it may cause connection leaks, which are evil
    Return conn
End function
End class
例2:

Public Class Class1
Public conn As MySqlConnection
Dim command As MySqlCommand
Dim reader As MySqlDataReader

''open the connection in the constructor
Sub New()
   conn = New MySqlConnection
   conn.ConnectionString = String.Format('connectionString for Dbname,server,uid,pword')
    Try
        conn.Open()

        ''MessageBox.Show("Connected!")
    Catch x As Exception
        MsgBox(x.Message)
    End Try
End Sub

''close the connection in the destructor
Protected Overrides Sub Finalize()
    conn.Close() ''automatically runs when this class is garbage collected
    conn.Dispose()
End Sub
End class

我能看看1班的连接吗?或者connect的inseat应该是mysqlConnect()conn在显示“Connected!”后立即关闭。此外,conn是Class1的一个变量,但您似乎正在使用另一个名为conn inside Button1的变量。噢!很抱歉,我在newCon中错误地键入了mysqlConnect to connect(),它必须是
newCon。mysqlConnect()
我会编辑它。谢谢,先生@Claudiusma可能是“Public Sub Connect()的某些函数”的一部分。我正试图在
按钮1中打开它,先生,正如您所说,我在
连接后立即关闭了它我试图对conn.close()进行注释,但仍然无效。