Hey guys. So I'm trying to have a program which saves images to the database and also retrieves and displays the ones that are in the database. Here is my layout if it helps:
http://imageshack.us/photo/my-images/207/guiq.png/[
^]
Following a tutorial, my program correctly browses, deletes and saves to the database. It also displays the first image correctly. Now the only part missing is moving through the database with the Next/Previous buttons and I also can't figure out how to load the name associated with the image to the textbox name... =(
Please help me with this?
Here's my code:
Imports System.IO
Imports System.Data.OleDb
Public Class Form1
Dim dbConnection As New OleDbConnection
Dim dbDA As New OleDbDataAdapter
Dim dbCMD As New OleDbCommand
Dim SQL As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dbConnection.ConnectionString = "provider=microsoft.ace.oledb.12.0; data source = |datadirectory|\database1.accdb;"
dbConnection.Open()
SQL = "select * from table1"
Dim dt_images As New DataTable
dbCMD.Connection = dbConnection
acscmd.CommandText = CommandType.Text
dbCMD.CommandText = SQL
dbDA.SelectCommand = dbCMD
dbDA.Fill(dt_images)
For Each dr As DataRow In dt_images.Rows
Dim img_buffer() As Byte
img_buffer = CType(dr("picture"), Byte())
Dim img_stream As New MemoryStream(img_buffer, True)
img_stream.Write(img_buffer, 0, img_buffer.Length)
PictureBox1.Image = New Bitmap(img_stream)
Next
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
If OpenFileDialog1.FileName <> Nothing Or OpenFileDialog1.FileName <> "" Then
txtname.Text = OpenFileDialog1.FileName.Substring( _
OpenFileDialog1.FileName.LastIndexOf("\") + 1, _
(OpenFileDialog1.FileName.IndexOf(".", 0) - (OpenFileDialog1.FileName.LastIndexOf("\") + 1)))
End If
End Sub
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
OpenFileDialog1.Filter = "image file (*.jpg, *.bmp, *.png) | *.jpg; *.bmp; *.png| all files (*.*) | *.* "
If OpenFileDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim fsreader As New FileStream(OpenFileDialog1.FileName, FileMode.Open, FileAccess.Read)
Dim breader As New BinaryReader(fsreader)
Dim imgbuffer(fsreader.Length) As Byte
breader.Read(imgbuffer, 0, fsreader.Length)
fsreader.Close()
SQL = "insert into table1 (name, picture) values (@dtaname, @dtapic)"
dbCMD.CommandText = SQL
dbCMD.Connection = dbConnection
dbCMD.Parameters.AddWithValue("@dtaname", txtname.Text)
dbCMD.Parameters.AddWithValue("@dtapic", imgbuffer)
dbCMD.ExecuteNonQuery()
dbCMD.Dispose()
MsgBox("Saved")
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
SQL = "delete * from table1 where name ='" & txtname.Text & "'"
dbCMD.CommandText = SQL
dbCMD.Connection = dbConnection
dbCMD.ExecuteNonQuery()
dbCMD.Dispose()
MsgBox("deleted")
End Sub
End Class