H! I am trying to upload an image to a DB2 database. Image size - JPG (6.76 kb - 6924 bytes).
There is a BLOB field of 1048576 in the database table.
My code for inserting an image is as follows:
If fileup.PostedFile IsNot Nothing AndAlso fileup.PostedFile.FileName <> "" Then
Dim imagesize As Byte() = New Byte(fileup.PostedFile.ContentLength - 1) {}
Dim uploadedimage1 As HttpPostedFile = fileup.PostedFile
uploadedimage1.InputStream.Read(imagesize, 0, CInt(fileup.PostedFile.ContentLength))
Dim uploadedimage2 As New OleDbParameter("@Image", OleDbType.VarBinary, imagesize.Length)
uploadedimage2.Value = imagesize
Dim cmd As New OleDbCommand()
cmd.CommandText = "INSERT INTO xxx_TBL(x, IMAGE) VALUES (?, ?)"
cmd.Parameters.Add(x)
cmd.Parameters.Add(uploadedimage2)
cmd.Connection = clsDatabase.Open_DB()
Dim result As Integer = cmd.ExecuteNonQuery()
If result > 0 Then
Return True
The image is inserted into the database.
The code for retrieving an image from a database in a DataTable, which is then bound to a GridView to display on a web page, looks like this:
Dim cmd As New OleDbCommand()
cmd.CommandText = "SELECT x, IMAGE FROM xxx_TBL WHERE y = 1"
cmd.Connection = clsDatabase.Open_DB()
Dim dReader As OleDbDataReader
dReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As New DataTable
dt.Columns.Add(New DataColumn("Comments", GetType(String)))
dt.Columns.Add(New DataColumn("Picture", GetType(Bitmap)))
Do While (dReader.Read())
Dim dr As DataRow = dt.NewRow()
dr("Comments") = dReader(0).ToString
Dim imageobj = dReader(1)
Using ms As New System.IO.MemoryStream
Dim bm As Bitmap
Dim bytearray = DirectCast(imageobj, Byte())
ms.Write(bytearray, 0, bytearray.length)
bm = New Bitmap(ms)
dr("Picture") = bm
End Using
dt.Rows.Add(dr)
Loop
GridView1.DataSource = dt
GridView1.DataBind()
x comes from the database accurately and is displayed. However, I do not have an image - just a small “missing photo (red cross on white)”.
BLOB 8192. "TEST" ( ) 13848 . , - , DB2 / , .
- ? ?