Guest User

Untitled

a guest
Jan 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. Imports MySql.Data.MySqlClient
  2. Public Class Form1
  3. Dim con As MySqlConnection = New MySqlConnection(“Data Source=192.168.1.104;Database=test;User ID=root;Password=mypassword;”)
  4. Dim sql As MySqlCommand = New MySqlCommand(“SELECT * FROM Employee”, con)
  5. Dim ds As DataSet = New DataSet()
  6. Dim DataAdapter1 As MySqlDataAdapter = New MySqlDataAdapter()
  7. Dim Comb As MySqlCommandBuilder
  8.  
  9. ‘Please add datagridview to your form and name it datagrid1
  10. ‘This will display the data to datagrid view
  11. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  12. con.Open()
  13. DataAdapter1.SelectCommand = sql
  14. DataAdapter1.Fill(ds, “Employee”)
  15. DataGridView1.DataSource = ds
  16. DataGridView1.DataMember = “Employee”
  17. con.Close()
  18. End Sub
  19.  
  20. ‘Update database. Change any data from datagrid then click this button it will save any changes from the grid
  21. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  22. Comb = New MySqlCommandBuilder(DataAdapter1)
  23. Dim i As Integer = DataAdapter1.Update(ds.Tables(“Employee”))
  24. MessageBox.Show(“modify the number “ & i.ToString & ” rows”)
  25. End Sub
  26.  
  27. ‘Sample insert record to MySQL database
  28. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  29. Dim MyCommand As New MySqlCommand
  30. con.Open()
  31. MyCommand.Connection = con
  32. MyCommand.CommandText = “INSERT INTO Employee(IDNo,FirstName,LastName,email) VALUES( ‘” & txtID.Text & “‘,’” & txtfirstname.Text & “‘,’” & txtlastname.Text & “‘,’” & txtemail.Text & “‘)”
  33. MyCommand.ExecuteNonQuery()
  34. con.Close()
  35. MsgBox(“Record successfully added!”)
  36.  
  37. End Sub
  38.  
  39. End Class
Add Comment
Please, Sign In to add comment