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

时间:2015-03-15 15:09:40

标签: mysql vb.net connection

伙计们我正在尝试开发一个应用程序(使用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

结束模块

(执行控制台时没有显示任何内容) 谢谢!

1 个答案:

答案 0 :(得分:0)

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

其次,您需要通过this link

安装Connector/Net
  

注意:请使用版本6.6.7 ,因为他们已删除Visual   6.7.7及以上的Studio集成

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

从“项目”菜单中选择“添加引用”,然后选择“浏览”并浏览到安装连接器的安装文件夹, 选择“MySQL.Data.dll”。

Add Reference window


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