VB.NET MySQL 连接对所有形式开放

发布于 2024-12-14 12:59:02 字数 1526 浏览 1 评论 0原文

在第一种形式中,我将在要打开数据库连接的第二个形式中加载表格。

我编写了一个类,它使我能够打开和关闭连接

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千纸鹤 2024-12-21 12:59:02

我想问题是你在某个地方使用了两种形式,

Dim bla as New DBConn

相反,你可以使用单例模式。在 DBConn 类中添加以下代码:

'Make Constructor Private to disallow creating an Instance from somewhere else
Private Sub New()
End Sub

'Variable to share the only created instance
Private Shared _Instance As DBConn

'Function to get access to the only instance
Public Shared ReadOnly Property Instance() As DBConn
  Get
    If _Instance Is Nothing Then
      _Instance = New DBConn
    End If
    Return _Instance
  End Get
End Property

进行这些更改后,您需要将每个表单中的一行“更正”为以下内容:

Dim bla as DBConn = DBConn.Instance

这应该确保两个表单都要求实例,并且仅在第一次创建实例时。所有进一步的请求都会收到相同的实例。

还有一件事:如果您有“WithEvents”或任何 EventHandling 连接到该 Singleton,那么请确保在关闭/处置 form1 时将 DBConn 变量设置为空。但根据你的代码,这并不重要。

I guess the problem is that you use in both forms somewhere

Dim bla as New DBConn

Instead that, you can make use of the Singleton Pattern. Add the following code inside your DBConn class:

'Make Constructor Private to disallow creating an Instance from somewhere else
Private Sub New()
End Sub

'Variable to share the only created instance
Private Shared _Instance As DBConn

'Function to get access to the only instance
Public Shared ReadOnly Property Instance() As DBConn
  Get
    If _Instance Is Nothing Then
      _Instance = New DBConn
    End If
    Return _Instance
  End Get
End Property

After those changes, you need to 'correct' one line in each form to the following:

Dim bla as DBConn = DBConn.Instance

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.

赴月观长安 2024-12-21 12:59:02

如果您将每个表单中的连接类声明为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文