Insert picture into the database.

tiffany

Well-known member
Joined
Aug 7, 2005
Messages
57
Programming Experience
1-3
I have problem inserting picture to the database. After it had insert to the database, will they show picture inside or only show the file name?

Here are the codes for inserting to the db:

Dim cnn As New OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Inetpub\wwwroot\button\movie.mdb")

Dim strSQL As String = "INSERT INTO movieDet([pic]) VALUES('" + File1.PostedFile.FileName + " ')"

Dim cmd2 As New OleDbCommand(strSQL, cnn)

Dim dr As OleDbDataReader

If Not File1.PostedFile Is Nothing And _

File1.PostedFile.ContentLength > 0
Then

Dim fn As String = System.IO.Path.GetFileName(File1.PostedFile.FileName)



Try

cnn.Open()

dr = cmd.ExecuteReader()

Response.Write("The file has been uploaded.")

Catch Exc As Exception

Response.Write("Error: " & Exc.Message)

End Try

Else

Label14.Text = "Please select a file to upload."

End If

After i had clicked submit, it gave me an error message. Said that the connection is closed. Anybody can help me? Urgent. Thank you!

Rgds,
tiffany
 
The command has to be executed non-query, not reader.

Eg. dr = cmd.ExecuteNonQuery()

Only the filename will be stored, not the image.

dr must be declared as int32, as a non-query return the amount of affected records.

Hope it helps.
 
When I did this a while ago I had to get the image into a byte array and then upload the byte array to the database. Can't find the code though sorry, but there is lots of code out there if you do a search on google
 
Back
Top