Artikel -24
Contoh Program Database MYSQL dengan VB.NET 2008
Imports
MySql.Data.MySqlClient
Public Class Form1
Public db As New
MySql.Data.MySqlClient.MySqlConnection
Public sql As String
Public cmd As MySqlCommand
Public rs As MySqlDataReader
Sub
opendb()
sql = "server=localhost;uid=root;pwd;database=taanding"
Try
db.ConnectionString = sql
db.Open()
Catch
ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub
bersih()
Tim.Text = ""
kandang.Text = ""
lawan.Text = ""
score.Text = ""
nilai.Text = ""
Tim.Enabled = True
End Sub
Sub
buattabel()
LV.Columns.Add("tim",
80, HorizontalAlignment.Center)
LV.Columns.Add("kandang",
80, HorizontalAlignment.Left)
LV.Columns.Add("lawan",
80, HorizontalAlignment.Left)
LV.Columns.Add("score",
80, HorizontalAlignment.Left)
LV.Columns.Add("nilai",
80, HorizontalAlignment.Left)
LV.GridLines = True
LV.FullRowSelect = True
LV.View = View.Details
End Sub
Sub
isitabel()
LV.Items.Clear()
sql = "select
* from pemaiin"
cmd = New
MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
While
rs.Read
Dim
lst As New
ListViewItem
lst.Text = rs("tim")
lst.SubItems.Add(rs("kandang"))
lst.SubItems.Add(rs("lawan"))
lst.SubItems.Add(rs("score"))
lst.SubItems.Add(rs("nilai"))
LV.Items.Add(lst)
End
While
Catch
ex As Exception
MsgBox(ex.Message)
End Try
rs.Close()
End Sub
Sub
showDB()
rs.Read()
Tim.Text = rs("tim")
kandang.Text = rs("kandang")
lawan.Text = rs("lawan")
score.Text = rs("score")
nilai.Text = rs("nilai")
Btnsimpan.Text = "edit"
Tim.Enabled = False
End Sub
Private Sub Btnbaru_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnbaru.Click
Call
bersih()
Tim.Focus()
End Sub
Private Sub Btnkeluar_Click(ByVal
sender As System.Object, ByVal e As System.EventArgs)
Handles Btnkeluar.Click
End
End Sub
Private Sub Btnhapus_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnhapus.Click
LV.Items.Clear()
End Sub
Private Sub Tim_KeyPress(ByVal
sender As Object,
ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles
Tim.KeyPress
Dim
tombol As Integer
= Asc(e.KeyChar)
If
tombol = 13 Then
Dim
x As String
If
Tim.Text = "" Then
MsgBox("isi
tim terlebih dahulu")
Else
sql = "
select * from pemaiin where tim='" & Tim.Text & "'"
cmd = New
MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
Call
showDB()
Catch
ex As Exception
x = Tim.Text
bersih()
Tim.Text = x
Btnsimpan.Text = "simpan"
End
Try
kandang.Focus()
rs.Close()
End
If
End If
End Sub
Private Sub Btnsimpan_Click(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles Btnsimpan.Click
Call
opendb()
Call
bersih()
Call
buattabel()
Call
isitabel()
End Sub
End Class
Tidak ada komentar:
Posting Komentar