Imports MySql.Data.MySqlClient
Public Class Form11
Dim mysqlcon As New MySqlConnection("SERVER=*******;USERID=*******;PASSWORD=*******;DATABASE=*******;")
Dim mysqlcmd As New MySqlCommand
Dim da As New MySqlDataAdapter
Dim dt As New DataTable
Dim i As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.RowTemplate.Height = 30
showdata()
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
text1.Text = DataGridView1.Item(1, e.RowIndex).Value
text2.Text = DataGridView1.Item(2, e.RowIndex).Value
text3.Text = DataGridView1.Item(3, e.RowIndex).Value
text4.Text = DataGridView1.Item(4, e.RowIndex).Value
text5.Text = DataGridView1.Item(5, e.RowIndex).Value
text6.Text = DataGridView1.Item(6, e.RowIndex).Value
text7.Text = DataGridView1.Item(7, e.RowIndex).Value
text8.Text = DataGridView1.Item(8, e.RowIndex).Value
text9.Text = DataGridView1.Item(9, e.RowIndex).Value
End Sub
Private Sub showdata()
Dim cmd1 As New MySqlCommand
Dim ada As MySqlDataAdapter
Dim table As DataTable
Try
mysqlcon.Open()
cmd1.Connection = mysqlcon
ada = New MySqlDataAdapter("SELECT*FROM data", mysqlcon)
table = New DataTable
ada.Fill(table)
DataGridView1.DataSource = table
mysqlcon.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
mysqlcon.Open()
With mysqlcmd
.Connection = mysqlcon
.CommandText = "INSERT INTO data(money,food,water,food2,other,onlymoney,timeadd,moneynote,othernote)VALUES('" & text1.Text & "','" & text2.Text & "','" & text3.Text & "','" & text4.Text & "','" & text5.Text & "','" & text6.Text & "','" & text7.Text & "','" & text8.Text & "','" & text9.Text & "')"
i = .ExecuteNonQuery
End With
If i > 0 Then
MsgBox("บันทึกข้อมูล เรียบร้อยแล้ว !", MsgBoxStyle.Information)
End If
mysqlcon.Close()
showdata()
End Sub
End Class