PEREKAM DATA IMAGE/ FOTO KE DALAM MYSQL
PAKAI VB 2010
1. Kita buat dulu Table nya saya pakai XAMPP-MYSQL menggunakan MYSQL-Front
2. Kita bikin modulnya setelah buat Form
3. Kita Connectkan DataBase MYSQL dngan VB 2010 dengan menggunakan
Connector MYSQL versi NET 6.5.4
4. Kita siapkan : PictureBox, OpenFileDialog, DataGridView, Button, TextBox.
Kita buat Codingnya - lalu kita jalankan Selesai
Caranya sama seperti sebelumnya :
Lihat Gambar
PERSIAPAN FORM NYA
BEBERAPA HASIL PERCOBAAN UNTUK DIREKAM DAN DITAMPILKAN
CODINGNYA DIBAGI 2 YAITU DI MODUL DAN FORM :
Imports MySql.Data.MySqlClient
Imports System.Data
Module MODULFOTO
Public conn As New MySqlConnection
Public DTadapter As New MySqlDataAdapter
Public DTset As DataSet
Public DTrow As DataRow
'Public CMD As MySqlCommand
Public DTreader As MySqlDataReader
Public DTtable As New DataTable
Public CMDbuild As MySqlCommandBuilder
Public sql As String
Public cmd, cdm As New MySqlCommand
Public dadapter As New MySqlDataAdapter
Public datardr As MySqlDataReader
Public strSql As String
Dim serv As String = "Server=localhost;"
Dim dbase As String = "Database=jualan;"
Dim uid As String = "uid=root;"
Dim pwd As String = "pwd=;"
Dim ConString = serv & dbase & uid & pwd
Public db As New MySqlConnection(ConString)
Public Sub konek()
Try
tutupDB()
db.Open()
MsgBox("Koneksi DataBase berhasil dilakukan")
Catch ex As MySqlException
MsgBox(ex.Message)
MsgBox("Koneksi tidak berhasil dilakukan")
End Try
End Sub
Public Sub tutupDB()
If db.State = ConnectionState.Open Then
db.Close()
End If
End Sub
End Module
PAKAI VB 2010
1. Kita buat dulu Table nya saya pakai XAMPP-MYSQL menggunakan MYSQL-Front
2. Kita bikin modulnya setelah buat Form
3. Kita Connectkan DataBase MYSQL dngan VB 2010 dengan menggunakan
Connector MYSQL versi NET 6.5.4
4. Kita siapkan : PictureBox, OpenFileDialog, DataGridView, Button, TextBox.
Kita buat Codingnya - lalu kita jalankan Selesai
Caranya sama seperti sebelumnya :
Lihat Gambar
PERSIAPAN FORM NYA
BEBERAPA HASIL PERCOBAAN UNTUK DIREKAM DAN DITAMPILKAN
WANITA CANTIK MILITER DI SELURUH DUNIA
CODINGNYA DIBAGI 2 YAITU DI MODUL DAN FORM :
UNTUK MODUL :
Imports System.Data
Module MODULFOTO
Public conn As New MySqlConnection
Public DTadapter As New MySqlDataAdapter
Public DTset As DataSet
Public DTrow As DataRow
'Public CMD As MySqlCommand
Public DTreader As MySqlDataReader
Public DTtable As New DataTable
Public CMDbuild As MySqlCommandBuilder
Public sql As String
Public cmd, cdm As New MySqlCommand
Public dadapter As New MySqlDataAdapter
Public datardr As MySqlDataReader
Public strSql As String
Dim serv As String = "Server=localhost;"
Dim dbase As String = "Database=jualan;"
Dim uid As String = "uid=root;"
Dim pwd As String = "pwd=;"
Dim ConString = serv & dbase & uid & pwd
Public db As New MySqlConnection(ConString)
Public Sub konek()
Try
tutupDB()
db.Open()
MsgBox("Koneksi DataBase berhasil dilakukan")
Catch ex As MySqlException
MsgBox(ex.Message)
MsgBox("Koneksi tidak berhasil dilakukan")
End Try
End Sub
Public Sub tutupDB()
If db.State = ConnectionState.Open Then
db.Close()
End If
End Sub
End Module
UNTUK FORM NYA :
Imports System.IO
Imports MySql.Data.MySqlClient
Public Class Form1
Private path As String = ""
Private strSql As String = String.Empty
Dim FileName As String
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
konek()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
With ofdphoto
.Title = "Browse Gambar"
.FileName = "Pilih Gambar"
.Filter = "Type(*.jpg, *.jpeg, *.bmp, *.gif, *.png)|*.jpg; *.jpeg; *.bmp; *gif; *.png"
.ShowDialog()
path = .FileName
txtlokasi.Text = path
picPhoto.Image = Image.FromFile(path)
picPhoto.SizeMode = PictureBoxSizeMode.StretchImage
'biar tampilan image pas dengan kotak image yang di sediakan
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
tutupDB()
db.Open()
Catch ex As MySqlException
MsgBox(ex.Message)
MsgBox("Koneksi tidak berhasil dilakukan")
Exit Sub
End Try
sql = "SELECT * from mahasiswa where npm ='" & txtnim.Text.Trim & "'"
DTtable = New DataTable
cdm = New MySqlCommand(sql, db)
DTreader = cdm.ExecuteReader()
If DTreader.HasRows = True Then
DTreader.Read()
MsgBox("Data tersebut pernah di Rekam....!", MsgBoxStyle.Exclamation, "Perhatian")
Exit Sub
End If
' untuk contoh disini lokasi gambarnya saya hardcode
' kita bisa kembangkan dengan memanfaatkan komponen OpenFileDialog
konek()
' Dim FileName As String = "E:\ter\tantri.jpg"
Dim cmd As New MySqlCommand("INSERT INTO mahasiswa (npm, nama, foto) VALUES (@1, @2, @3)", db)
'strSql = "INSERT INTO mahasiswa (npm, nama, foto) VALUES (@1, @2, @3)" ' ini perintah insert biasa
' Using cmd As New MySqlClient.MySqlCommand(strSql, db)
With cmd
.Parameters.Clear()
.Parameters.AddWithValue("@1", txtnim.Text.Trim) ' npm
.Parameters.AddWithValue("@2", txtnama.Text.Trim) ' nama
.Parameters.AddWithValue("@3", IO.File.ReadAllBytes(txtlokasi.Text)) ' foto
.ExecuteNonQuery()
End With
' End Using
TampilData()
MessageBox.Show("Data sudah disimpan", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Try
tutupDB()
db.Open()
Catch ex As MySqlException
MsgBox(ex.Message)
MsgBox("Koneksi tidak berhasil dilakukan")
Exit Sub
End Try
Try
strSql = "SELECT foto FROM mahasiswa WHERE npm = @1"
cmd.CommandText = strSql
cmd.Connection = db
dadapter.SelectCommand = cmd
' datar = cmd.ExecuteReader
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@1", txtnim.Text) ' npm
datardr = cmd.ExecuteReader
If datardr.HasRows Then
'datardr.Read()
If datardr.Read() Then
'txtnim.Text = datardr("npm")
'txtnama.Text = datardr("nama")
Dim ms As New IO.MemoryStream(CType(datardr.Item("foto"), Byte()))
picPhoto.Image = Image.FromStream(ms) ' tampilkan gambarnya
End If
End If
Catch ex As Exception
End Try
End Sub
Private Sub txtnim_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnim.KeyPress
If e.KeyChar = Chr(13) Then
tampilkanfoto()
End If
End Sub
Sub tampilkanfoto()
Try
tutupDB()
db.Open()
Catch ex As MySqlException
MsgBox(ex.Message)
MsgBox("Koneksi tidak berhasil dilakukan")
Exit Sub
End Try
sql = "SELECT * from mahasiswa where npm ='" & txtnim.Text.Trim & "'"
DTtable = New DataTable
cmd = New MySqlCommand(sql, db)
DTreader = cmd.ExecuteReader()
If DTreader.HasRows = True Then
DTreader.Read()
txtnim.Text = DTreader("npm")
txtnama.Text = DTreader("nama")
Dim gambar As New IO.MemoryStream(CType(DTreader.Item("foto"), Byte()))
picPhoto.Image = Image.FromStream(gambar)
DTtable.Clear()
Else
MessageBox.Show("Data yang anda cari tidak dapat kami tampilkan, Masukan NIM Mahasiswa ")
End If
db.Close()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
tampilkanfoto()
End Sub
Private Sub dgv_CellClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellClick
With dgv
txtnim.Text = .Item(0, .CurrentRow.Index).Value
tampilkanfoto()
End With
End Sub
Private Sub TampilData()
Dim da As New MySqlDataAdapter("select * from mahasiswa", db)
Dim dt As New DataTable
da.Fill(dt)
dgv.DataSource = dt
dgv.Refresh()
'Dim query As String = "SELECT * FROM CUSTOMER"
' Dim da As New MySqlDataAdapter(query, db)
' Dim ds As New DataSet()
'If da.Fill(ds) Then
'dgv.DataSource = ds.Tables(0)
'End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TampilData()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
tampilkanfoto()
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
konek()
Dim cmdb As New MySqlCommand(("update MAHASISWA set nama='" & txtnama.Text & "' where npm ='" & txtnim.Text.Trim & "'"), db)
cmdb.ExecuteNonQuery()
TampilData()
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Try
Dim hapus As Integer
hapus = MsgBox("Anda Yakin Ingin Menghapus Data " & txtnim.Text.Trim & " ini?", MsgBoxStyle.OkCancel, "Info Program")
Select Case hapus
Case vbCancel
Exit Sub
Case vbOK
konek()
Dim cmdb As New MySqlCommand(("Delete from MAHASISWA where npm ='" & txtnim.Text.Trim & "'"), db)
cmdb.ExecuteNonQuery()
TampilData()
MessageBox.Show("Data berhasil di Hapus")
End Select
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Pesan Kesalahan")
End Try
End Sub
End Class
Imports MySql.Data.MySqlClient
Public Class Form1
Private path As String = ""
Private strSql As String = String.Empty
Dim FileName As String
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
konek()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
With ofdphoto
.Title = "Browse Gambar"
.FileName = "Pilih Gambar"
.Filter = "Type(*.jpg, *.jpeg, *.bmp, *.gif, *.png)|*.jpg; *.jpeg; *.bmp; *gif; *.png"
.ShowDialog()
path = .FileName
txtlokasi.Text = path
picPhoto.Image = Image.FromFile(path)
picPhoto.SizeMode = PictureBoxSizeMode.StretchImage
'biar tampilan image pas dengan kotak image yang di sediakan
End With
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error")
End Try
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
tutupDB()
db.Open()
Catch ex As MySqlException
MsgBox(ex.Message)
MsgBox("Koneksi tidak berhasil dilakukan")
Exit Sub
End Try
sql = "SELECT * from mahasiswa where npm ='" & txtnim.Text.Trim & "'"
DTtable = New DataTable
cdm = New MySqlCommand(sql, db)
DTreader = cdm.ExecuteReader()
If DTreader.HasRows = True Then
DTreader.Read()
MsgBox("Data tersebut pernah di Rekam....!", MsgBoxStyle.Exclamation, "Perhatian")
Exit Sub
End If
' untuk contoh disini lokasi gambarnya saya hardcode
' kita bisa kembangkan dengan memanfaatkan komponen OpenFileDialog
konek()
' Dim FileName As String = "E:\ter\tantri.jpg"
Dim cmd As New MySqlCommand("INSERT INTO mahasiswa (npm, nama, foto) VALUES (@1, @2, @3)", db)
'strSql = "INSERT INTO mahasiswa (npm, nama, foto) VALUES (@1, @2, @3)" ' ini perintah insert biasa
' Using cmd As New MySqlClient.MySqlCommand(strSql, db)
With cmd
.Parameters.Clear()
.Parameters.AddWithValue("@1", txtnim.Text.Trim) ' npm
.Parameters.AddWithValue("@2", txtnama.Text.Trim) ' nama
.Parameters.AddWithValue("@3", IO.File.ReadAllBytes(txtlokasi.Text)) ' foto
.ExecuteNonQuery()
End With
' End Using
TampilData()
MessageBox.Show("Data sudah disimpan", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Try
tutupDB()
db.Open()
Catch ex As MySqlException
MsgBox(ex.Message)
MsgBox("Koneksi tidak berhasil dilakukan")
Exit Sub
End Try
Try
strSql = "SELECT foto FROM mahasiswa WHERE npm = @1"
cmd.CommandText = strSql
cmd.Connection = db
dadapter.SelectCommand = cmd
' datar = cmd.ExecuteReader
cmd.Parameters.Clear()
cmd.Parameters.AddWithValue("@1", txtnim.Text) ' npm
datardr = cmd.ExecuteReader
If datardr.HasRows Then
'datardr.Read()
If datardr.Read() Then
'txtnim.Text = datardr("npm")
'txtnama.Text = datardr("nama")
Dim ms As New IO.MemoryStream(CType(datardr.Item("foto"), Byte()))
picPhoto.Image = Image.FromStream(ms) ' tampilkan gambarnya
End If
End If
Catch ex As Exception
End Try
End Sub
Private Sub txtnim_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnim.KeyPress
If e.KeyChar = Chr(13) Then
tampilkanfoto()
End If
End Sub
Sub tampilkanfoto()
Try
tutupDB()
db.Open()
Catch ex As MySqlException
MsgBox(ex.Message)
MsgBox("Koneksi tidak berhasil dilakukan")
Exit Sub
End Try
sql = "SELECT * from mahasiswa where npm ='" & txtnim.Text.Trim & "'"
DTtable = New DataTable
cmd = New MySqlCommand(sql, db)
DTreader = cmd.ExecuteReader()
If DTreader.HasRows = True Then
DTreader.Read()
txtnim.Text = DTreader("npm")
txtnama.Text = DTreader("nama")
Dim gambar As New IO.MemoryStream(CType(DTreader.Item("foto"), Byte()))
picPhoto.Image = Image.FromStream(gambar)
DTtable.Clear()
Else
MessageBox.Show("Data yang anda cari tidak dapat kami tampilkan, Masukan NIM Mahasiswa ")
End If
db.Close()
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
tampilkanfoto()
End Sub
Private Sub dgv_CellClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgv.CellClick
With dgv
txtnim.Text = .Item(0, .CurrentRow.Index).Value
tampilkanfoto()
End With
End Sub
Private Sub TampilData()
Dim da As New MySqlDataAdapter("select * from mahasiswa", db)
Dim dt As New DataTable
da.Fill(dt)
dgv.DataSource = dt
dgv.Refresh()
'Dim query As String = "SELECT * FROM CUSTOMER"
' Dim da As New MySqlDataAdapter(query, db)
' Dim ds As New DataSet()
'If da.Fill(ds) Then
'dgv.DataSource = ds.Tables(0)
'End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TampilData()
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
tampilkanfoto()
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
konek()
Dim cmdb As New MySqlCommand(("update MAHASISWA set nama='" & txtnama.Text & "' where npm ='" & txtnim.Text.Trim & "'"), db)
cmdb.ExecuteNonQuery()
TampilData()
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
Try
Dim hapus As Integer
hapus = MsgBox("Anda Yakin Ingin Menghapus Data " & txtnim.Text.Trim & " ini?", MsgBoxStyle.OkCancel, "Info Program")
Select Case hapus
Case vbCancel
Exit Sub
Case vbOK
konek()
Dim cmdb As New MySqlCommand(("Delete from MAHASISWA where npm ='" & txtnim.Text.Trim & "'"), db)
cmdb.ExecuteNonQuery()
TampilData()
MessageBox.Show("Data berhasil di Hapus")
End Select
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "Pesan Kesalahan")
End Try
End Sub
End Class
Tidak ada komentar:
Posting Komentar