Field Name Field Type
IdReg Autonumber
Keterangan Text
MMData OleObject
Buat Sebuah Module
Imports System.Data
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Drawing.Image
Imports System.Diagnostics
Imports System.Exception
Imports System.NullReferenceException
Imports System.IO
Imports Microsoft.VisualBasic
Module MainModule
#Region “Deklarasi Variable”
Public Cn As OleDbConnection
Public Da As OleDbDataAdapter
Public Cm As OleDbCommand
Public Ds As DataSet
Public Dv As DataView
Public Fs As FileStream, Br As BinaryReader, Bitgmb() As Byte
Public ViewGbr As Image, ModeNewRec As Boolean
Public CnString, Csql As String
#End Region
#Region “Koneksi Database”
Public Function KoneksiDB(Optional ByVal CnStr As String = “”) As String
Cn = New OleDbConnection
Try
CnString = “Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=cisarua2006;Data Source=D:\Source\Polda\Polda\bin\Debug\Database\TNI.mdb”
Cn.ConnectionString = CnString
Cn.Open()
Catch ex As OleDb.OleDbException
MessageBox.Show(“Koneksi Database Gagal Hubungi Administrator”)
Application.Exit()
End Try
Return CnString
End Function
#End Region
#Region “Multimedia”
Public Function SimpanGambar(Optional ByVal OFile As String = “”, Optional ByVal Ket As String = “”) As Byte
Fs = New FileStream(OFile, FileMode.OpenOrCreate, FileAccess.Read)
Br = New BinaryReader(Fs)
Dim ArrFile(Fs.Length – 1) As Byte
Br.Read(ArrFile, 0, CInt(Fs.Length))
‘——-Koneksi ke database
Cn = New OleDbConnection
If Cn.State = ConnectionState.Closed Then KoneksiDB()
Csql = “INSERT INTO Picture (Keterangan,MMData) VALUES (?,?)”
Cm = New OleDbCommand(Csql, Cn)
Cm.Parameters.Add(“@Keterangan”, OleDbType.LongVarChar).Value = Ket
Cm.Parameters.Add(“@MMData”, OleDbType.Binary).Value = ArrFile
Cm.ExecuteNonQuery()
MessageBox.Show(“File Gambar Telah Tersimpan Di Database”, “Simpan Gambar”, MessageBoxButtons.OK, MessageBoxIcon.Information)
Cn.Close()
End Function
Public Function UpdateGambar(Optional ByVal OFile As String = “”, Optional ByVal noUrut As Integer = 0, Optional ByVal Ket As String = “”) As Byte
Fs = New FileStream(OFile, FileMode.OpenOrCreate, FileAccess.Read)
Br = New BinaryReader(Fs)
Dim ArrFile(Fs.Length – 1) As Byte
Br.Read(ArrFile, 0, CInt(Fs.Length – 1))
Cn = New OleDbConnection
If Cn.State = ConnectionState.Closed Then KoneksiDB()
Csql = “Update Picture Keterangan=?,MMData=? Where IdReg=” & noUrut & “”
Cm = New OleDbCommand(Csql, Cn)
Cm.Parameters.Add(“@Keterangan”, OleDbType.VarChar).Value = Ket
Cm.Parameters.Add(“@MMData”, OleDbType.Binary).Value = ArrFile
Cm.ExecuteNonQuery()
MessageBox.Show(“File Gambar Telah di Update”, “Update Gambar”, MessageBoxButtons.OK, MessageBoxIcon.Information)
Cn.Close()
End Function
Public Function HapusGambar(ByVal noUrut As Integer) As Integer
Csql = “Delete From Picture Where IdReg=” & noUrut & “”
Ds = New DataSet
Cn = New OleDbConnection
If Cn.State = ConnectionState.Closed Then KoneksiDB()
Cm = New OleDbCommand(Csql, Cn)
Cm.ExecuteNonQuery()
MessageBox.Show(“File Gambar Telah Terhapus dari Database”, “Hapus File Gambar”, MessageBoxButtons.OK, MessageBoxIcon.Information)
Cn.Close()
End Function
Public Function LihatGambar(Optional ByVal XSql As String = “”) As Image
Try
Dim BitGmb() As Byte
Cn = New OleDbConnection
If Cn.State = ConnectionState.Closed Then
KoneksiDB()
Cm = New OleDbCommand(XSql, Cn)
BitGmb = Cm.ExecuteScalar
If BitGmb.Length > 0 Then
Dim Ms As New MemoryStream(BitGmb, True)
Ms.Write(BitGmb, 0, System.Convert.ToInt32(Ms.Length))
LihatGambar = New Bitmap(Ms)
Ms.Close()
End If
End If
Catch ex As Exception
End Try
End Function
#End Region
End Module
Buat Sebuah Form dengan Nama FrmPicture
Tambahkan Object OpenFileDialog beri nama OPFile
Imports System.Data
Imports System.Data.OleDb
Public Class FrmPicture
Private NmFile As String
Private noUrut As Integer
Private pertama As Boolean
Private NewStat As Boolean
Private Gambar As Image
Private b As DataGridViewRowEventArgs
#Region “Multimedia”
Private Sub AmbilGambar()
Try
SimpanGambar(NmFile, txtKet.Text)
Catch ex As Exception
‘MessageBox.Show(“File Gambar Gagal di Simpan”, “Gagal Simpan”, MessageBoxButtons.OK, MessageBoxIcon.Warning)
MessageBox.Show(ex.Message, “”, MessageBoxButtons.OK)
End Try
End Sub
Private Sub UpdateGbr()
Try
UpdateGambar(NmFile, txtKet.Text)
Catch ex As Exception
End Try
End Sub
Private Sub LihatGbr()
Try
Gambar = LihatGambar(“Select MMData From Picture Where IdReg=” & noUrut & “”)
If IsDBNull(Gambar) = True Then
Pb1.Image = Nothing
Else
Pb1.Image = Gambar
End If
Catch ex As Exception
‘
End Try
End Sub
#End Region
Private Sub FrmPicture_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
KoneksiDB()
pertama = True
SemuaGambar()
End Sub
Private Sub KlikImage()
OPFile.Filter = “JPEG *.JPEG|*.JPG|GIF *.GIF|*.GIF”
OPFile.FileName = “”
If OPFile.ShowDialog = Windows.Forms.DialogResult.OK Then
NmFile = OPFile.FileName
Pb1.Image = Image.FromFile(NmFile)
Else
OPFile.Dispose()
NewStat = False
End If
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
If NewStat = False Then
NewStat = True
KlikImage()
Else
NewStat = False
End If
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
AmbilGambar()
End Sub
Private Sub SemuaGambar()
Csql = “Select IdReg,Keterangan from Picture”
Ds = New DataSet
Dv = New DataView
If Cn.State = ConnectionState.Closed Then KoneksiDB()
Cm = New OleDbCommand(Csql, Cn)
Da = New OleDbDataAdapter(Cm)
Da.Fill(Ds, “Picture”)
Dv = New DataView(Ds.Tables(“Picture”), “”, “”, DataViewRowState.CurrentRows)
grdPicture.DataSource = Dv
grdPicture.Columns(0).Width = 50
grdPicture.Columns(1).Width = 200
Cn.Close()
End Sub
Private Sub grdPicture_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grdPicture.SelectionChanged
noUrut = grdPicture.CurrentRow.Cells(0).Value
LihatGbr()
End Sub
End Class