Kali ini saya akan memposting cara menyimpan gambar di BLOB mysql yg masih jarang digunakan. Berikut adalah contoh code program menggunakan VB.NET
Step 1 : buat Database dulu di mysql
CREATE TABLE `test`.`users` (
`USERID` varchar(20) NOT NULL default '0',
`USERNAME` varchar(45) NOT NULL default '',
`USEREMAIL` varchar(45) NOT NULL default '',
`USERPHOTO` mediumblob NOT NULL,
`USERDESC` varchar(200) NOT NULL default '',
PRIMARY KEY (`USERID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Step 2 : Buat Desain Program di VB.NET
Step 3 : Ketikkan code di bawah ini di VB.NET
Imports System.IO
Imports System.Data.OleDb
Imports MySql.Data.MySqlClient
Private curImage As Image = Nothing
Private curFileName As String = Nothing
Private connstring As String = "server=localhost;database=test;uid=root;pwd=root123"
Private savedImageName As String = "C:\\xxx.jpg"
Private Sub btncari_Click()
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
Private Sub btnsimpan_Click()
If TextBox1.Text Is String.Empty Then
MessageBox.Show("Mencari File Gambar")
Return
End If
Dim fs As FileStream = New FileStream(curFileName, FileMode.OpenOrCreate, FileAccess.Read)
Dim rawData() As Byte = New Byte(fs.Length) {}
fs.Read(rawData, 0, System.Convert.ToInt32(fs.Length))
fs.Close()
Dim sql As String = "SELECT * FROM Users"
Dim conx As MySqlConnection = New MySqlConnection
conx.ConnectionString = connstring
If conx.State <> ConnectionState.Open Then
conx.Open()
End If
Dim adapterx As MySqlDataAdapter = New MySqlDataAdapter(sql, conx)
Dim cmdBuilderx As MySqlCommandBuilder = New MySqlCommandBuilder(adapterx)
Dim ds As DataSet = New DataSet("Users")
adapterx.MissingSchemaAction = MissingSchemaAction.AddWithKey
adapterx.Fill(ds, "Users")
Dim userDes As String = "toekangprogrammer.blogspot.com"
Dim row As DataRow = ds.Tables("Users").NewRow()
row("Userid") = "userx"
row("UserName") = "toekangprogrammer"
row("UserEmail") = "toekangprogrammer@gmail.com"
row("UserDesc") = userDes
row("UserPhoto") = rawData
ds.Tables("Users").Rows.Add(row)
adapterx.Update(ds, "Users")
If conx Is Nothing Then
If conx.State = ConnectionState.Open Then
conx.Close()
End If
conx.Dispose()
End If
MessageBox.Show("Data Tersimpan")
End Sub
Private Sub btnread_Click()
Dim sql As String = "SELECT UserPhoto FROM Users where userid='Userx'"
Dim conx As MySqlConnection = New MySqlConnection
conx.ConnectionString = connstring
If conx.State <> ConnectionState.Open Then
conx.Open()
End If
Dim cmd As MySqlCommand = New MySqlCommand(sql, conx)
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 300000
Dim outbyte(300000 - 1) As Byte
Dim retval As Long
Dim startIndex As Long = 0
Dim pub_id As String = ""
Dim reader As MySqlDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
reader.Read()
fs = New FileStream(savedImageName, FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
startIndex = 0
retval = reader.GetBytes(0, 0, outbyte, 0, bufferSize)
bw.Write(outbyte)
bw.Flush()
bw.Close()
fs.Close()
reader.Close()
curImage = Image.FromFile(savedImageName)
PictureBox1.Image = curImage
PictureBox1.Invalidate()
If conx.State = ConnectionState.Open Then
conx.Close()
conx.Dispose()
End If
End Sub
Step 4 : Selesai sudah tinggal compile dan tekan F5
Semoga bisa membantu.
Jika ada pertanyaan/diskusi tentang masalah program VB.Net bisa email ke : toekangprogrammer@gmail.com
Langganan:
Postingan (Atom)