Menyimpan Gambar dengan VB.Net di MySQL

Tools yang digunakan

›Label => sebagai keterangan
›Button => proses
›OpenFileDialog => komponen open file
›PictureBox => tempat menampilkan gambar
›Konektor database mysql
Langkah – Langkah
1. ›buat database dbdata
2.›Buat tabel tbgambar

CREATE TABLE IF NOT EXISTS `tbgambar` (

`kd_gambar` varchar(5) DEFAULT NULL,

`gambar` mediumblob

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

3. Buat Desain tampilan

4. Buat Koneksi dan variabel

Imports MySql.Data.MySqlClient

Public Class Form1

Private curFileName As String = Nothing

Dim kon As New MySqlConnection(“server=localhost;userid=root;database=dbdata”)

Dim perintah As New MySqlCommand

Dim cek As MySqlDataReader

Dim Simpan As Boolean

Dim Data As New MySqlDataAdapter

Dim ds As New DataSet

Dim sql As String = String.Empty

5. Tombol Cari

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

‘open file

Dim openDlg As OpenFileDialog = New OpenFileDialog

openDlg.Filter = “All JPEG files|*.jpg”

Dim filter As String = openDlg.Filter

openDlg.Title = “Open a JPEG File”

If (openDlg.ShowDialog() = DialogResult.OK) Then

curFileName = openDlg.FileName

TextBox1.Text = curFileName

End If

End Sub

6. Tombol Simpan

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

‘simpan file

If TextBox1.Text Is String.Empty Then

MessageBox.Show(“Mencari File Gambar”)

Return

End If

kon.Open()

perintah.Connection = kon

perintah.CommandType = CommandType.Text

sql = “INSERT INTO tbgambar (kd_gambar,gambar) VALUES (@kd_gambar,@gambar)” ‘ ini perintah insert biasa

Using perintah = New MySqlCommand(sql, kon)

With perintah

.Parameters.Clear()

.Parameters.AddWithValue(“@kd_gambar”, TextBox2.Text) ‘ foto

.Parameters.AddWithValue(“@gambar”, IO.File.ReadAllBytes(curFileName)) ‘ foto

.ExecuteNonQuery()

End With

End Using

›        kon.Close()

MsgBox(“Data Sukses Tersimpan”, MsgBoxStyle.Information, “Pesan”)

End Sub

7. Tombol Tampil

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

kon.Open()

perintah.Connection = kon

perintah.CommandType = CommandType.Text

sql = “SELECT gambar FROM tbgambar WHERE kd_gambar = @kd_gambar” ‘ ini perintah insert biasa

Using perintah = New MySqlCommand(sql, kon)

With perintah

.Parameters.Clear()

.Parameters.AddWithValue(“@kd_gambar”, TextBox2.Text) ‘ foto

Using cek = .ExecuteReader()

If cek.HasRows = True Then

If cek.Read() Then

Dim ms As New IO.MemoryStream(CType(cek.Item(“gambar”), Byte()))

PictureBox1.Image = Image.FromStream(ms) ‘ tampilkan gambarnya

End If

End If

End Using

‘.ExecuteNonQuery()

End With

End Using

kon.Close()

End Sub

8. Tombol keluar

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
End
End Sub

10. Hasil

simpangambar

referensi : http://coding4ever.wordpress.com/2011/03/15/menyimpan-dan-menampilkan-gambar-di-mysql/

Category: Belajar, Pemrograman .NET  Tags: , , ,
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply

Your email address will not be published. Required fields are marked *