Imports MySql.Data.MySqlClient
Public Class FrmMySQL_Connection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TxtHN.Enabled = False
TxtName.Enabled = False
TxtAge.Enabled = False
BtnCancel.Enabled = False
BtnSave.Enabled = False
BtnExit.Enabled = False
BtnAddNew.Enabled = True
TextBox1.Enabled = True
End Sub
Private Sub BtnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCancel.Click
TxtHN.Clear()
TxtName.Clear()
TxtAge.Clear()
TxtHN.Focus()
End Sub
Private Sub BtnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
Try
Dim fdCon As New MySqlConnection("Database=db_social;Data Source=localhost;User Id=root;Password=")
Dim fdCom As New MySqlCommand("", fdCon)
fdCon.Open()
'-- Dim fdRead As OleDb.OleDbDataReader = fdCom.ExecuteReader(mysqlCommandBehavior.CloseConnection)
Dim sHN As String = TxtHN.Text
Dim sName As String = TxtName.Text
Dim sAge As String = TxtAge.Text
fdCom.CommandText = "INSERT INTO general (HN,Name,Age) VALUES ('" & sHN & "','" & sName & "','" & sAge & "')"
fdCom.ExecuteNonQuery()
fdCon.Close()
MsgBox("เพิ่มข้อมูล เรียบร้อยแล้ว ครับ")
BtnAddNew.Enabled = True
BtnSave.Enabled = False
BtnCancel.Enabled = False
BtnExit.Enabled = True
TxtHN.Enabled = False
TxtName.Enabled = False
TxtAge.Enabled = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
End
End Sub
Private Sub BtnAddNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAddNew.Click
TxtHN.Clear()
TxtName.Clear()
TxtAge.Clear()
TxtHN.Enabled = True
TxtName.Enabled = True
TxtAge.Enabled = True
BtnCancel.Enabled = True
BtnSave.Enabled = True
BtnExit.Enabled = True
TxtHN.Focus()
End Sub
Private Sub TxtHNSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtHNSearch.TextChanged
End Sub
Private Sub BtnHNSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHNSearch.Click
End Sub
Private Sub TxtNameSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNameSearch.TextChanged
End Sub
Private Sub BtnNameSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNameSearch.Click
End Sub
End Class
Private Sub TxtHNSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtHNSearch.TextChanged
End Sub
Private Sub BtnHNSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnHNSearch.Click
End Sub
Private Sub TxtNameSearch_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNameSearch.TextChanged
End Sub
Private Sub BtnNameSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNameSearch.Click
End Sub
Sub BindData()
Dim objConn As New MySqlConnection
Dim objCmd As New MySqlCommand
Dim dtAdapter As New MySqlDataAdapter
Dim ds As New DataSet
Dim strConnString,strSQL As String
strConnString = "Server=localhost;User Id=root; Password=root; Database=mydatabase; Pooling=false"
strSQL = "SELECT * FROM customer WHERE (Name like '%"& strKeyWord &"%' OR Email like '%"& strKeyWord &"%') "
objConn.ConnectionString = strConnString
With objCmd
.Connection = objConn
.CommandText = strSQL
.CommandType = CommandType.Text
End With
dtAdapter.SelectCommand = objCmd
dtAdapter.Fill(ds)
'*** BindData to GridView ***'
myGridView.DataSource = ds
myGridView.DataBind()
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
End Sub