1. Buka Database Access Lalu Buat Database baru beri nama db_gambar .. dan tambahkan New Table dan beri nama tbl_gambar dan atur fieldnya sebagai berikut :
2. Setelah itu lalu buat project baru di Visual Studio 2008 dan beri nama bebas saja deh.. lalu database yang telah kita buat tersebut di copy ke folder bin –> debug dalam aplikasi yang telah dibuat baru oleh kita diatas.
3. Buat tampilan sebagai berikut :
4. Masukan Kode Lengkapnya berikut :
1: Imports System.Data.OleDb
2: Imports System.Drawing
3: Imports System.Drawing.Imaging
4: Imports System.IOPublic Class Form1
5: Dim conn As New OleDbConnection
6: Dim cmd As OleDbCommand = Nothing
7: Dim rd As OleDbDataReader = Nothing
8: Dim da As OleDbDataAdapter = Nothing
9: Class Form1
10: Sub combo()
11: Try
12: opens()
13: cmd = New OleDbCommand("select kode_gb from tbl_gambar", conn)
14: rd = cmd.ExecuteReader
15: cmbKode.Items.Clear()
16: While rd.Read
17: cmbKode.Items.Add(rd.Item("kode_gb"))
18: End While
19: closes()
20: Catch ex As Exception
21: MessageBox.Show(ex.Message)
22: End Try
23: End Sub
24: Sub tampil()
25: Try
26: opens()
27: Dim dt As New DataTable
28: da = New OleDbDataAdapter("select * from tbl_gambar", conn)
29: da.Fill(dt)
30: DataGridView1.DataSource = dt
31: DataGridView1.Columns(0).HeaderText = "Kode"
32: DataGridView1.Columns(1).HeaderText = "NAMA"
33: DataGridView1.Columns("gb").Visible = False
34:
35: Dim ImageCol As New DataGridViewImageColumn()
36: ImageCol.Name = "Gambar"
37: ImageCol.Width = 200
38: DataGridView1.Columns.Add(ImageCol)
39:
40: For Each Row As DataGridViewRow In DataGridView1.Rows
41: Try
42: Dim ImageBytes() As Byte = Row.Cells("gb").Value
43:
44: Dim ms As New MemoryStream(ImageBytes)
45: Dim img As Image = Image.FromStream(ms)
46:
47: Dim ImageCell As DataGridViewImageCell = CType(Row.Cells("Gambar"), DataGridViewImageCell)
48: ImageCell.Value = img
49:
50: Row.Cells("gb").Value = New Byte() {}
51: Row.Height = 50
52: Catch
53: 'kalau ada error abaikanlah
54: End Try
55: Next
56: Catch ex As Exception
57: MessageBox.Show(ex.Message)
58: End Try
59: End Sub
60: Sub opens()
61: If Not conn Is Nothing Then
62: conn.Close()
63: End If
64: conn.Open()
65: End Sub
66: Sub closes()
67: conn.Close()
68: End Sub
69: Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
70: conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "db_gambar.accdb"
71: combo()
72: tampil()
73: End Sub
74:
75: Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
76: Try
77: opens()
78: Dim fs As New FileStream(OFD.FileName, FileMode.OpenOrCreate, FileAccess.Read)
79: Dim MyData(fs.Length) As Byte
80: fs.Read(MyData, 0, CInt(fs.Length))
81: fs.Close()
82: cmd = New OleDbCommand("UPDATE tbl_gambar set nama_gb=@nama,gb=@Image where kode_gb=" & cmbKode.Text & "", conn)
83: cmd.Parameters.AddWithValue("@nama", txtNama.Text)
84: cmd.Parameters.AddWithValue("@Image", MyData)
85: If DirectCast(cmd.ExecuteNonQuery(), Integer) > 0 Then
86: MessageBox.Show("Foto berhasil di ubah", "Photo Album SQL", MessageBoxButtons.OK, MessageBoxIcon.Information)
87: combo()
88: tampil()
89: txtNama.Clear()
90: txtFileName.Clear()
91: PicGambar.Image = Nothing
92: End If
93: closes()
94: Catch ex As Exception
95: MessageBox.Show(ex.Message)
96: End Try
97: End Sub
98:
99: Private Sub btnTelusuri_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTelusuri.Click
100: Try
101: OFD.Filter = "Image Files (*.jpg, *.bmp, *.gif, *.png)|*.jpg; *.bmp; *.gif; *.png"
102: OFD.ShowDialog()
103: txtFileName.Text = OFD.FileName
104: txtFileName.SelectionStart = txtFileName.Text.Length
105: If Trim(txtFileName.Text) <> "" Then
106: PicGambar.Image = Image.FromFile(txtFileName.Text)
107: End If
108: Catch ex As Exception
109: MessageBox.Show(ex.Message)
110: End Try
111: End Sub
112:
113: Private Sub btnBatal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBatal.Click
114: Me.Close()
115: End Sub
116:
117: Private Sub txtNama_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtNama.Validating
118: If txtNama.Text.Length = 0 Then
119: ErrorProvider1.SetError(txtNama, "Nama Harus Diisi")
120: End If
121: End Sub
122:
123: Private Sub cmbKode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbKode.SelectedIndexChanged
124: Try
125: opens()
126: Dim arr() As Byte
127: cmd = New OleDbCommand("select nama_gb,gb from tbl_gambar where kode_gb=" & cmbKode.Text & "", conn)
128: rd = cmd.ExecuteReader
129: rd.Read()
130: txtNama.Text = rd.Item("nama_gb")
131: arr = rd.Item("gb")
132: PicGambar.Image = Image.FromStream(New IO.MemoryStream(arr))
133: closes()
134: Catch ex As Exception
135: MessageBox.Show(ex.Message)
136: End Try
137: End Sub
138:
139: Private Sub btnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHapus.Click
140: Try
141: opens()
142: cmd = New OleDbCommand("DELETE FROM tbl_gambar where kode_gb=" & cmbKode.Text & "", conn)
143: If DirectCast(cmd.ExecuteNonQuery(), Integer) > 0 Then
144: MessageBox.Show("Foto berhasil di hapus", "Photo Album SQL", MessageBoxButtons.OK, MessageBoxIcon.Information)
145: combo()
146: tampil()
147: txtNama.Clear()
148: txtFileName.Clear()
149: PicGambar.Image = Nothing
150: End If
151: closes()
152: Catch ex As Exception
153: MessageBox.Show(ex.Message)
154: End Try
155: End Sub
156:
157: Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
158: Try
159: opens()
160: Dim fs As New FileStream(OFD.FileName, FileMode.OpenOrCreate, FileAccess.Read)
161: Dim MyData(fs.Length) As Byte
162: fs.Read(MyData, 0, CInt(fs.Length))
163: fs.Close()
164: cmd = New OleDbCommand("INSERT INTO tbl_gambar (nama_gb,gb) VALUES(@nama,@Image)" & cmbKode.Text & "", conn)
165: cmd.Parameters.AddWithValue("@nama", txtNama.Text)
166: cmd.Parameters.AddWithValue("@Image", MyData)
167: If DirectCast(cmd.ExecuteNonQuery(), Integer) > 0 Then
168: MessageBox.Show("Foto berhasil di ubah", "Photo Album SQL", MessageBoxButtons.OK, MessageBoxIcon.Information)
169: combo()
170: tampil()
171: txtNama.Clear()
172: txtFileName.Clear()
173: PicGambar.Image = Nothing
174: End If
175: closes()
176: Catch ex As Exception
177: MessageBox.Show(ex.Message)
178: End Try
179: End Sub
180: End Class

0 komentar:
Posting Komentar