VB.NET MySQL 连接对所有形式开放
在第一种形式中,我将在要打开数据库连接的第二个形式中加载表格。
我编写了一个类,它使我能够打开和关闭连接
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class DBConn
Dim conn As New MySqlConnection
Dim connString As String
Dim DataSchnitstelel As MySqlDataAdapter
Public Function verbindungString(ByVal Server As String, ByVal UID As String, ByVal PWD As String, _
ByVal Datenbank As String)
connString = "server=" & Server & ";uid=" & UID & ";pwd=" & PWD & ";database=" & Datenbank & ";"
connString = CStr(connString)
End Function
Public Sub Open()
conn.ConnectionString = connString
Try
conn.Open()
MsgBox(connString)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Function Close()
If conn.State = ConnectionState.Open Then
Try
conn.Close()
MessageBox.Show("Closed!")
Catch ex As Exception
MessageBox.Show("Something Wrong" & ex.Message)
End Try
Else
MessageBox.Show("Verbindung bereitsgeschlossen")
End If
End Function
Public Function UpdateStatus()
Dim Klank As Boolean
If conn.State = 1 Then
Klank = True
End If
If conn.State = 0 Then
Klank = False
End If
Return Klank
End Function
Public Function SQLSelect()
End Function
End Class
In the Second Form i Connect to the DB。 连接成功打开,但未以第一种形式打开:(
出了什么问题?
In the first form I will load Tabels in the secound I want to open the DB Connection.
I wrote a Class which enables me to open and close the connection
Imports System
Imports System.Data
Imports MySql.Data.MySqlClient
Public Class DBConn
Dim conn As New MySqlConnection
Dim connString As String
Dim DataSchnitstelel As MySqlDataAdapter
Public Function verbindungString(ByVal Server As String, ByVal UID As String, ByVal PWD As String, _
ByVal Datenbank As String)
connString = "server=" & Server & ";uid=" & UID & ";pwd=" & PWD & ";database=" & Datenbank & ";"
connString = CStr(connString)
End Function
Public Sub Open()
conn.ConnectionString = connString
Try
conn.Open()
MsgBox(connString)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Function Close()
If conn.State = ConnectionState.Open Then
Try
conn.Close()
MessageBox.Show("Closed!")
Catch ex As Exception
MessageBox.Show("Something Wrong" & ex.Message)
End Try
Else
MessageBox.Show("Verbindung bereitsgeschlossen")
End If
End Function
Public Function UpdateStatus()
Dim Klank As Boolean
If conn.State = 1 Then
Klank = True
End If
If conn.State = 0 Then
Klank = False
End If
Return Klank
End Function
Public Function SQLSelect()
End Function
End Class
In the Second Form i Connect to the DB.
The connection opens successfully but it's not open in the first form :(
Whats Wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想问题是你在某个地方使用了两种形式,
相反,你可以使用单例模式。在 DBConn 类中添加以下代码:
进行这些更改后,您需要将每个表单中的一行“更正”为以下内容:
这应该确保两个表单都要求实例,并且仅在第一次创建实例时。所有进一步的请求都会收到相同的实例。
还有一件事:如果您有“WithEvents”或任何 EventHandling 连接到该 Singleton,那么请确保在关闭/处置 form1 时将 DBConn 变量设置为空。但根据你的代码,这并不重要。
I guess the problem is that you use in both forms somewhere
Instead that, you can make use of the Singleton Pattern. Add the following code inside your DBConn class:
After those changes, you need to 'correct' one line in each form to the following:
This should assure that both forms are asking for an instance, and only the very first time an instance is created. All further askings receive the same instance.
One more thing: If you have 'WithEvents' or any EventHandling connected to that Singleton, then make sure to set your DBConn variable to nothing when you close/dispose form1. But according to your code this does not matter.
如果您将每个表单中的连接类声明为
Dim someName as new DBConn
,那么您的问题就很明显:它们不知道彼此的存在。如果除了表单之外还有一个模块,其中您将 DBConn 实例声明为 Friend,那么所有表单都会知道它If you declare the connection class in each of your forms as
Dim someName as new DBConn
, your problem is obvious: they know not of each others existence. If you have a module besides your forms, in which you declare the DBConn instance as Friend, it would be known to all forms