Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use following code for storing image into access database and now I want to retrieve image through data reader please help me.

[removed duplicate code]

What I have tried:

VB
 Try

	Dim ms As New System.IO.MemoryStream
	Dim bmpImage As New Bitmap(stphoto.Image)

	bmpImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
	bytImage = ms.ToArray()
	ms.Close()

	Dim con As New OleDb.OleDbConnection
	Dim cmd As New OleDb.OleDbCommand
	Dim sqlstring As String
	Dim query As String
	sqlstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=bin\fee.accdb"
	con = New OleDb.OleDbConnection(sqlstring)
	con.Open()
	query = "INSERT INTO smaster ([sid],[sname],[fname],[add],[dob],[class],[college],[session],[cnumber],[gnumber],[photo]) Values (" & sidtxt.Text & ",'" & snametxt.Text & "','" & fnametxt.Text & "','" & addtxt.Text & "','" & dobpick.Value.ToShortDateString & "','" & classcombo.Text & "', '" & collegecombo.Text & "','" & sessioncombo.Text & "','" & mobtxt.Text & "','" & gnumbertxt.Text & "','@bytImage')"

	cmd = New OleDb.OleDbCommand(query, con)
	cmd.ExecuteNonQuery()

	studentid = sidtxt.Text
	studentname = snametxt.Text
	'code for opening add subject form

	Dim result As Integer = MessageBox.Show("Master Data Saved, Please Click OK for adding Subjects", "Student Data", MessageBoxButtons.OK)
	If result = DialogResult.OK Then
		addsub.Show()
	End If
Catch ex As Exception
	MessageBox.Show(ex.Message)
End Try
Posted
Updated 7-Sep-17 1:32am
v2
Comments
Graeme_Grant 7-Sep-17 6:44am    
You pull it out the same way that you put it in.

Also, never concat SQL queries, always use parameters.
Member 13397933 7-Sep-17 12:03pm    
Can you please explain through the code for the above problem, it will easy to understand for me.

1 solution

Don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And if you have used string concatenation to insert your images, well, you have another problem. They are all garbage, and you can't retrieve them...
See here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - the code is all in C#, but it's pretty simple and online convertes can change it if you can't cope.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900