Wrox Programmer Forums
|
Visual Basic 2010 General Discussion For any discussions about Visual Basic 2010 topics which aren't related to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 2nd, 2011, 10:47 AM
Authorized User
 
Join Date: Apr 2010
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Default Insert image into access database

Hi!

I am building a form where I want to insert an image, or the data source of the image, into an Access database.

With this code I am able to display an image in a PictureBox.

Code:
Dim OpenFileDialog1 As New OpenFileDialog
With OpenFileDialog1
    If OpenFileDialog1.ShowDialog = DialogResult.OK Then
        picArt.Image = Image.FromFile(.FileName)
    End If
End With
But now I want to send it and save it in my database when I click a button, and I can't figure out how.

I am using this code to open my database connection;

Code:
Dim cn As OleDbConnection
Dim cmd As OleDbCommand
Dim icount As Integer
Dim str As String
    
cn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=H:\programs\Project\Database.mdb;")
cn.Open()
str = "INSERT INTO Table1 (Picture) VALUES (???)"
cmd = New OleDb.OleDbCommand(str, cn)
icount = cmd.ExecuteNonQuery
MessageBox.Show("Saved")
cn.Close()
The ??? in my string means I don't know what to write here.

Help would be most appreciated.
 
Old March 2nd, 2011, 04:39 PM
Authorized User
 
Join Date: Jan 2011
Posts: 86
Thanks: 1
Thanked 12 Times in 12 Posts
Default

Hi,

you would need a method to convert your Image to a byteArray and save this.
Somethin like the following should do the trick:
Code:
Private Sub StoreImage(myImage As Image)

	Dim myMStream As New MemoryStream()
	myImage.Save(myMStream, ImageFormat.Gif)
	Dim byteArr As Byte() = myMStream.ToArray()
	myMStream.Close()

	Dim oCmd As OleDbCommand = oConn.CreateCommand()
	oCmd.CommandText = "INSERT INTO Table1(Picture) VALUES @Picture)"
	oCmd.Parameters.AddWithValue("@Picture", byteArr)
	oCmd.ExecuteNonQuery()


	oCmd.Dispose()
	oConn.Close()

End Sub
The ImageFormat needs to be specified. You might need to use FileInfo to inspect your fileextension if you have multipl image formats.
Hope this helps.





Similar Threads
Thread Thread Starter Forum Replies Last Post
How to insert an image in a (*.MDB) database thomaz C# 1 May 3rd, 2007 07:02 AM
asp.net code to insert image in to database madhusrp ASP.NET 1.0 and 1.1 Professional 2 March 16th, 2006 02:54 AM
Insert row into MS-ACCESS database ITladybug ADO.NET 1 January 28th, 2006 07:57 AM
INSERT info into a Access database for display sideshow245 Classic ASP Databases 2 January 7th, 2004 04:25 PM
INSERT string into a Microsoft Access Database gsolis Classic ASP Databases 1 December 31st, 2003 05:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.