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,DataGridView, OpenFileDialog, Button, TextBox 4.
Kita buat Codingnya - lalu kita jalankan Selesai
Lihat Gambar :
Persiapan Form
Hasil yg di Rekam di Tabel MYSQL
Hasil Perekaman Gambar / Foto
Ini Rincian Codingnya Untuk yg di Module :
Imports MySql.Data.MySqlClient
Imports System.Data
Module modul_foto
Public cmd As New MySqlCommand
Public dadapter As New MySqlDataAdapter
Public datardr As MySqlDataReader
Public strSql As String
Dim serv As String = "Server=192.168.1.8;"
Dim dbase As String = "Database=jualan;"
Dim uid As String = "uid=root;"
Dim pwd As String = "pwd=root;"
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
Ini Coding untuk yang di Form1 :
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 coneksi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles coneksi.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
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
MessageBox.Show("Data sudah disimpan", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.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
End Class
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,DataGridView, OpenFileDialog, Button, TextBox 4.
Kita buat Codingnya - lalu kita jalankan Selesai
Lihat Gambar :
Persiapan Form
Hasil yg di Rekam di Tabel MYSQL
Hasil Perekaman Gambar / Foto
Ini Rincian Codingnya Untuk yg di Module :
Imports MySql.Data.MySqlClient
Imports System.Data
Module modul_foto
Public cmd As New MySqlCommand
Public dadapter As New MySqlDataAdapter
Public datardr As MySqlDataReader
Public strSql As String
Dim serv As String = "Server=192.168.1.8;"
Dim dbase As String = "Database=jualan;"
Dim uid As String = "uid=root;"
Dim pwd As String = "pwd=root;"
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
Ini Coding untuk yang di Form1 :
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 coneksi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles coneksi.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
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
MessageBox.Show("Data sudah disimpan", "Informasi", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.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
End Class
Tidak ada komentar:
Posting Komentar