***MENGGUNAKAN DATABASE MYSQL dengan VB.NET 2008***
membuat program data mahasiswa menggunakan VB.Net 2008 dengan mysql
langsung saja kita bahasa untuk pertama x nya buat lha databases menggunakan
mysql lalu desain from seperti data di bawah ini;
* lalu install kan konektor ODBC untuk VB.Net
*lalu buat lha from atau desain from seperti dibawah ini
* tambah kan referense kedalam projek dengen projek [ add refernse
* pilih browse dan cara file msqldata.dll dengan alamat "C:\programs files \mysql\mysql conector Net.1.0,7\bin\\net 1.1\" dan clik tombol OK.
* kemudian listing program di bawah ini;
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=akademikk"
Try
db.ConnectionString = sql
db.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub bersih()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
rubahbutton(True, False, False, False, True)
TextBox1.Enabled = True
End Sub
Sub RUBAHBUTTON(ByVal Btn1 As Boolean, ByVal Btn2 As Boolean, ByVal Btn3 As Boolean, ByVal Btn4 As Boolean, ByVal Btn5 As Boolean)
Button1.Enabled = Btn1
Button2.Enabled = Btn2
Button3.Enabled = Btn3
Button4.Enabled = Btn4
Button5.Enabled = Btn5
End Sub
Sub buattabel()
lv.Columns.Add("npm", 80, HorizontalAlignment.Center)
lv.Columns.Add("nama", 180, HorizontalAlignment.Left)
lv.Columns.Add("jenjang", 80, HorizontalAlignment.Left)
lv.Columns.Add("jurusan", 100, HorizontalAlignment.Left)
lv.GridLines = True
lv.FullRowSelect = True
lv.View = View.Details
End Sub
Sub ISITABEL()
lv.Items.Clear()
sql = "select * from mahasiswa"
cmd = New MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
While rs.Read
Dim ist As New ListViewItem
ist.Text = rs("npm")
ist.SubItems.Add(rs("nama"))
ist.SubItems.Add(rs("jenjang"))
ist.SubItems.Add(rs("jurusan"))
lv.Items.Add(ist)
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
rs.Close()
End Sub
Sub prosesdb(ByVal log As Integer)
Dim pesan As String = ""
Select Case log
Case 0
sql = "insert into mahasiswa(npm, nama, jenjang, jurusan)" & _
"values ('" & TextBox1.Text & _
"','" & TextBox2.Text & _
"','" & TextBox3.Text & _
"','" & TextBox4.Text & "')"
pesan = "data telah disimpan"
Case 1
sql = "update mahasiswa set nama='" & TextBox2.Text & "', " & _
"jenjang='" & TextBox3.Text & "'," & _
"jurusan='" & TextBox4.Text & "' " & _
"where npm='" & TextBox1.Text & "'"
pesan = "telah di update"
Case 2
sql = "delete from mahasiswa where npm='" & TextBox1.Text & "'"
pesan = "data telah dihapus"
End Select
Try
cmd = New MySqlCommand(sql, db)
cmd.ExecuteNonQuery()
MsgBox(pesan, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "komfirmasi")
Call bersih()
Call ISITABEL()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub showdb()
rs.Read()
TextBox1.Text = rs("npm")
TextBox2.Text = rs("nama")
TextBox3.Text = rs("jenjang")
TextBox4.Text = rs("jurusan")
RUBAHBUTTON(False, True, True, True, False)
Button3.Text = "edit"
TextBox1.Enabled = False
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call opendb()
Call bersih()
Call ISITABEL()
Call buattabel()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call bersih()
TextBox1.Focus()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Call bersih()
TextBox1.Focus()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
End
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x As String
x = MsgBox("anda yakin ingin menghapus", MsgBoxStyle.Information + MsgBoxStyle.YesNo, "hapus")
If x = vbYes Then
Call prosesdb(2)
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Button3.Text = "simpan" Then prosesdb(0) Else prosesdb(1)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim tombol As Integer = Asc(e.KeyChar)
If tombol = 13 Then
Dim x As String
If TextBox1.Text = "" Then
MsgBox("isi npm terlebih dahulu")
Else
sql = "select* from mahasiswa where npm='" & TextBox1.Text & "'"
cmd = New MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
Call showdb()
Catch ex As Exception
x = TextBox1.Text
bersih()
TextBox1.Text = x
RUBAHBUTTON(False, False, True, True, False)
Button3.Text = "simpan"
End Try
TextBox2.Focus()
rs.Close()
End If
End If
End Sub
End Class
hasil akhir nya
sekian dulu pembahasan saya tentang mengenai database mysql dan VB.Net 2008
semoga bermamfaat buat teman teman yang lagi membutuh kan nya
sekian dan terima kasih,
BY; HERI SIANIPAR
membuat program data mahasiswa menggunakan VB.Net 2008 dengan mysql
langsung saja kita bahasa untuk pertama x nya buat lha databases menggunakan
mysql lalu desain from seperti data di bawah ini;
* lalu install kan konektor ODBC untuk VB.Net
*lalu buat lha from atau desain from seperti dibawah ini
* tambah kan referense kedalam projek dengen projek [ add refernse
* pilih browse dan cara file msqldata.dll dengan alamat "C:\programs files \mysql\mysql conector Net.1.0,7\bin\\net 1.1\" dan clik tombol OK.
* kemudian listing program di bawah ini;
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=akademikk"
Try
db.ConnectionString = sql
db.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub bersih()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
rubahbutton(True, False, False, False, True)
TextBox1.Enabled = True
End Sub
Sub RUBAHBUTTON(ByVal Btn1 As Boolean, ByVal Btn2 As Boolean, ByVal Btn3 As Boolean, ByVal Btn4 As Boolean, ByVal Btn5 As Boolean)
Button1.Enabled = Btn1
Button2.Enabled = Btn2
Button3.Enabled = Btn3
Button4.Enabled = Btn4
Button5.Enabled = Btn5
End Sub
Sub buattabel()
lv.Columns.Add("npm", 80, HorizontalAlignment.Center)
lv.Columns.Add("nama", 180, HorizontalAlignment.Left)
lv.Columns.Add("jenjang", 80, HorizontalAlignment.Left)
lv.Columns.Add("jurusan", 100, HorizontalAlignment.Left)
lv.GridLines = True
lv.FullRowSelect = True
lv.View = View.Details
End Sub
Sub ISITABEL()
lv.Items.Clear()
sql = "select * from mahasiswa"
cmd = New MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
While rs.Read
Dim ist As New ListViewItem
ist.Text = rs("npm")
ist.SubItems.Add(rs("nama"))
ist.SubItems.Add(rs("jenjang"))
ist.SubItems.Add(rs("jurusan"))
lv.Items.Add(ist)
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
rs.Close()
End Sub
Sub prosesdb(ByVal log As Integer)
Dim pesan As String = ""
Select Case log
Case 0
sql = "insert into mahasiswa(npm, nama, jenjang, jurusan)" & _
"values ('" & TextBox1.Text & _
"','" & TextBox2.Text & _
"','" & TextBox3.Text & _
"','" & TextBox4.Text & "')"
pesan = "data telah disimpan"
Case 1
sql = "update mahasiswa set nama='" & TextBox2.Text & "', " & _
"jenjang='" & TextBox3.Text & "'," & _
"jurusan='" & TextBox4.Text & "' " & _
"where npm='" & TextBox1.Text & "'"
pesan = "telah di update"
Case 2
sql = "delete from mahasiswa where npm='" & TextBox1.Text & "'"
pesan = "data telah dihapus"
End Select
Try
cmd = New MySqlCommand(sql, db)
cmd.ExecuteNonQuery()
MsgBox(pesan, MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "komfirmasi")
Call bersih()
Call ISITABEL()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Sub showdb()
rs.Read()
TextBox1.Text = rs("npm")
TextBox2.Text = rs("nama")
TextBox3.Text = rs("jenjang")
TextBox4.Text = rs("jurusan")
RUBAHBUTTON(False, True, True, True, False)
Button3.Text = "edit"
TextBox1.Enabled = False
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call opendb()
Call bersih()
Call ISITABEL()
Call buattabel()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call bersih()
TextBox1.Focus()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Call bersih()
TextBox1.Focus()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
End
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x As String
x = MsgBox("anda yakin ingin menghapus", MsgBoxStyle.Information + MsgBoxStyle.YesNo, "hapus")
If x = vbYes Then
Call prosesdb(2)
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Button3.Text = "simpan" Then prosesdb(0) Else prosesdb(1)
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim tombol As Integer = Asc(e.KeyChar)
If tombol = 13 Then
Dim x As String
If TextBox1.Text = "" Then
MsgBox("isi npm terlebih dahulu")
Else
sql = "select* from mahasiswa where npm='" & TextBox1.Text & "'"
cmd = New MySqlCommand(sql, db)
rs = cmd.ExecuteReader
Try
Call showdb()
Catch ex As Exception
x = TextBox1.Text
bersih()
TextBox1.Text = x
RUBAHBUTTON(False, False, True, True, False)
Button3.Text = "simpan"
End Try
TextBox2.Focus()
rs.Close()
End If
End If
End Sub
End Class
hasil akhir nya
sekian dulu pembahasan saya tentang mengenai database mysql dan VB.Net 2008
semoga bermamfaat buat teman teman yang lagi membutuh kan nya
sekian dan terima kasih,
BY; HERI SIANIPAR
Tidak ada komentar:
Posting Komentar