2020年8月15日 星期六

vb.net 連結 MySQL [Connect MySQL]

1
2
3
Imports MySql.Data
Imports MySql.Data.MySqlClient
Imports MySql.Data.Types

1
    Dim mydb As New MySqlConnection






 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim server As String = "192.168.0.100"
        Dim DatabaseName As String = "DBTesting"
        Dim userName As String = "lhh"
        Dim password As String = "lhh"

        If Not mydb Is Nothing Then mydb.Close()
        mydb.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false", server, userName, password, DatabaseName)
        Try
            mydb.Open()
            Dim cmd As New MySqlCommand("INSERT INTO Products (proName) VALUES ('PC')")
            cmd.Connection = mydb
            cmd.ExecuteNonQuery()

            MsgBox("Insert Success")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        mydb.Close()
    End Sub

沒有留言:

張貼留言