tTapi aku sekarang sedang belajar lagi buat Mysql sebagai databasenya , aku temukan program sederhana di : http://pccontrol.wordpress.com/category/dasar/dasar-2/visual-basic-net/
ini dia yang aku pelajari pertama menggunakan DB Mysql :
menggunakan Mysql karena dulu pas TA aku juga pakai Database Mysql tapi pemrogramannya menggunakan PHP karena lebih ringan daripada SQL server. dan tidak menggunakan memori banyak untuk menginstallnya.
check it out :
sebelumya :
Contoh Program
- Buat tabel tabelContoh di database MYSQL
- Buatlah project baru VB.Net
- Tambahkan referensi mysql.data.dll (klik menu add reference arahkan ke mysql.data ) lokasi mysql.data.dll ada di C:\Program Files\MySQL\MySQL Connector Net 6.3.5\Assemblies\v2.0
- buat modul (dgn mengklik menu Project->Add Module) isi dgn code sbb:
Module Module1
Public conn As MySqlConnection
Public da As MySqlDataAdapter
Public ds As New DataSet
Public cmd As MySqlCommand
Public dr As MySqlDataReader
Public str As String
Sub koneksi()
str = "server=" & "localhost" & ";" & "user id=" & "root" & ";" & "password=" & "" & ";" & "database=test"
conn = New MySqlConnection(str)
If conn.State = ConnectionState.Closed Then
conn.Open()
End If
End Sub
End Module
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call koneksi()
da = New MySqlDataAdapter("select*from tabelContoh", conn)
ds = New DataSet
da.Fill(ds)
dgv.datasource = ds.Tables(0)
dgv.readonly = True
End Sub
Private Sub simpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles simpan.Click
Call koneksi()
Dim simpan As String = "insert into tabelContoh values('" & TextBox1.Text & "','" & TextBox2.Text & "')"
cmd = New MySqlCommand(simpan, conn)
cmd.ExecuteNonQuery()
End Sub
Private Sub edit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles edit.Click
Dim edit As String = "update tabelContoh set no='" & TextBox1.Text & "',nama='" & TextBox2.Text & "' where no='" & TextBox1.Text & "'"
cmd = New MySqlCommand(edit, conn)
cmd.ExecuteNonQuery()
End Sub
Private Sub hapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hapus.Click
Call koneksi()
Dim hapus As String = "delete from tabelContoh where no='" & TextBox1.Text & "'"
cmd = New MySqlCommand(hapus, conn)
cmd.ExecuteNonQuery()
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
Call koneksi()
cmd = New MySqlCommand("select * from tabelContoh where nama='" & TextBox2.Text & "'", conn)
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows Then
Call koneksi()
da = New MySqlDataAdapter("select * from tabelContoh where nama '" & TextBox2.Text & "'", conn)
ds = New DataSet
da.Fill(ds)
dgv.DataSource = ds.Tables(0)
Else
MsgBox("Nama tidak ditemukan")
End If
End Sub
End Class
jangan lupa tambahkan " Imports MySql.Data.MySqlClient " pada script paling atas seperti script module1.vb tanpa tanda petik .
selamat mencoba... :)
0 comments:
Post a Comment