将我的 vb.net 应用程序连接到 MySQL 数据库

connecting my vb.net app to MySQL database

伙计们,我正在尝试开发一个应用程序(使用 vb.net 和 sql 数据库),使用 PhpMyAdmin 管理我的数据库,所以问题是,在执行我的应用程序时,它什么也没显示: / ,我不知道问题是否是密码错误,但即便如此,它应该会向我显示一条错误消息(我使用了 catch nd try ...)

> 代码 !

进口System.Data.SqlClient

模块模块 1

Sub Main()

    Dim Connexion As New SqlConnection("Data Source=localhost;Initial Catalog=stockage;User Id=pma;Password=pmapass;")

    Try

        Connexion.Open()

        Dim Requete As String = "UPDATE client SET nom_client ='client unknown' WHERE nom_client is null"
        Dim Commande As New SqlCommand(Requete, Connexion)

        Try
            Console.WriteLine("there was " & Commande.ExecuteNonQuery() & " lignes changed")
        Catch ex As Exception
            Console.WriteLine(ex.Message)
        End Try
        Commande.Dispose()
        Connexion.Close()

    Catch ex As Exception
        Console.WriteLine(ex.Message)
    End Try

    Console.ReadLine()
End Sub

模块结束

(执行控制台时什么都不显示) 谢谢!

首先,您没有使用 phpMyAdmin 管理 SQL 数据库。 phpMyAdmin 改为管理 mySQL 个数据库。

其次你需要通过this link

安装Connector/Net

Note : Please go for version 6.6.7 as they have removed Visual Studio integration from 6.7.7 and above

安装连接器后,在 VB.NET 中启动一个新项目(我使用的是 2008),然后您需要添加对连接器的引用。

从项目菜单中选择 "Add Reference",然后选择 select "Browse" 并浏览到安装连接器的安装文件夹, 选择 "MySQL.Data.dll" 。


Imports MySql.Data.MySqlClient 'Very Important

Module Module1

    Sub Main()
        Dim Connexion As New MySqlConnection
        Dim db_name As String = "stockage"
        Dim db_host As String = "localhost"
        Dim db_username As String = "pma"
        Dim db_password As String = "pmapass"

        If Not Connexion Is Nothing Then Connexion.Close() 'to close already open connections
        Connexion.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", db_host, db_username, db_password, db_name)

        Try
            Connexion.Open()
            Console.WriteLine("Connected to server " + db_host)

            Dim Commande As MySqlCommand
            Dim Requete As String = "UPDATE client SET nom_client ='client unknown' WHERE nom_client is null"

            Commande = New MySqlCommand(Requete, Connexion)
            Console.WriteLine("there was " & Commande.ExecuteNonQuery() & " lines changed")
            Connexion.Close()
            Console.WriteLine("Disconnected from server " + db_host)

        Catch ex As MySqlException
            MessageBox.Show("Cannot connect to database: " & ex.Message)

        Finally
            Connexion.Dispose()

        End Try
    End Sub
End Module