update mysql by connection / net

ปรับปรุง : 2550-11-22 (รวบรวม)
คำแนะนำ
1. Download mysql-connector-net-5.1.3.zip 2.7 MB and setup
2. Toolbox, Right Click, Choose Items ...
3. Check in .NET Framework Components
- MySqlCommand
- MySqlConnection
- MySqlDataAdapter 4. Reference : http://www.vbmysql.com/articles/vbnet-mysql-tutorials/ 5. ถ้าทดสอบก็อย่าลบ root และ admin นะครับ .. อาจร้ายแรงถึงขั้นเข้า mysql ไม่ได้
Imports MySql.Data.MySqlClient
Public Class Form5
    Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        read_tb()
    End Sub
    Sub read_tb()
        Dim conn = New MySqlConnection
        conn.ConnectionString = "server=127.0.0.1;" _
          & "user id=admin;password=p;database=mysql"
        Dim cmd As New MySqlCommand
        Dim adt As New MySqlDataAdapter
        Dim mydata As New DataTable
        Dim sql As String = "select user,password from user"
        conn.Open()
        Try
            cmd.Connection = conn
            cmd.CommandText = sql
            adt.SelectCommand = cmd
            adt.Fill(mydata)
            DataGridView1.DataSource = mydata
        Catch myerror As MySqlException
            MsgBox("error: " & myerror.Message)
        End Try
        conn.close()
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim conn As New MySqlConnection
        conn.ConnectionString = "server=127.0.0.1;" _
            & "user id=admin;password=p;database=mysql"
        Dim cmd As New MySqlCommand
        cmd.Connection = conn
        cmd.CommandText = "insert into user(user, password)values('abcs', 'def')"
        Try
            conn.Open()
            cmd.ExecuteNonQuery()
        Catch myerror As MySqlException
            MsgBox("error: " & myerror.Message)
        End Try
        conn.Close()
        read_tb()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim uindex As Integer = Val(DataGridView1.CurrentRow.Index.ToString)
        Dim uid As String = DataGridView1.Rows(uindex).Cells(0).Value
        Dim conn As New MySqlConnection
        conn.ConnectionString = "server=127.0.0.1;" _
            & "user id=admin;password=p;database=mysql"
        Dim cmd As New MySqlCommand
        cmd.Connection = conn
        cmd.CommandText = "delete from user where user ='" & uid & "'"
        Try
            conn.Open()
            cmd.ExecuteNonQuery()
        Catch myerror As MySqlException
            MsgBox("error: " & myerror.Message)
        End Try
        conn.Close()
        read_tb()
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim conn As New MySqlConnection
        conn.ConnectionString = "server=127.0.0.1;" _
            & "user id=admin;password=p;database=mysql"
        Dim cmd As New MySqlCommand
        cmd.Connection = conn
        cmd.CommandText = "select user,password from user"
        Try
            conn.Open()
            Dim s As String = ""
            Dim r As MySqlDataReader = cmd.ExecuteReader()
            While r.Read()
                s &= r(0) & r(1) & Chr(10)
            End While
            MsgBox(s)
        Catch myerror As MySqlException
            MsgBox("error: " & myerror.Message)
        End Try
        conn.Close()
    End Sub
End Class

แนะนำเว็บใหม่ : ผลการจัดอันดับ
รักลำปาง : thcity.com : korattown.com : topsiam.com : มหาวิทยาลัยโยนก
ศูนย์สอบ : รวมบทความ : ไอทีในชีวิตประจำวัน : ดาวน์โหลด : yourname@thaiall.com
ติดต่อ ทีมงาน ชาวลำปาง มีฝันเพื่อการศึกษา Tel.08-1992-7223