连接本地数据库的区别?

时间:2016-10-10 09:21:10

标签: mysql vb.net local

从机器上的MySQL数据库连接到MySQL数据库服务器时,连接字符串之外的代码是否有任何差异?

我想对机器上本地安装的MySQL数据库使用它。它可以工作还是我需要应用更改?

Public Sub Foo()
    Try
        Dim result As String
        Dim conn As MySqlConnection
        conn = New MySqlConnection()

        Dim DatabaseName As String = ""
        Dim server As String = "db4free.net" 
        Dim port As String = "3306"
        Dim userName As String = ""
        Dim password As String = ""
        conn.ConnectionString = String.Format("server={0}; Port={1}; user id={2}; password={3}; database={4}; pooling=false", server, port, userName, password, DatabaseName)
        'conn.ConnectionString = "server=localhost; user id=root; password=xxxx; database=main"
        Try
            conn.Open()
            Console.WriteLine("OPEN CONN")
            Console.WriteLine()

        Catch myerror As MySqlException
            Console.WriteLine("Connection to the Database Failed")
        End Try
        Dim myAdapter As New MySqlDataAdapter
        Dim sqlquery = "SELECT * FROM `Customer`"
        Dim myCommand As New MySqlCommand()
        myCommand.Connection = conn
        myCommand.CommandText = sqlquery
        myAdapter.SelectCommand = myCommand
        Dim myData As MySqlDataReader
        myData = myCommand.ExecuteReader()
        Console.WriteLine("ID" & vbTab & "Name" & vbTab & "City" & vbTab & "Age")
        Console.WriteLine()
        While myData.Read()
            Console.WriteLine(myData("id").ToString & vbTab & myData("Name") & vbTab & myData("City") & vbTab & myData(3).ToString)
        End While        

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


End Sub

0 个答案:

没有答案