H!我正在尝试在 DB2 数据库中上传图像。图像大小为 JPG(6.76 kb - 6924 字节)。
数据库表有一个长度为 1048576 的 BLOB 字段。
我插入图像的代码如下:
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
图像被插入到数据库中。
从数据库中获取图片到DataTable中,然后绑定到GridView上显示在网页上的代码如下:
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 来自数据库的罚款,并显示。但是,我没有得到图片 - 只是一个小的“缺少图片(白底红十字)图标”。
检查数据库时,BLOB 字段中图像的长度为 8192。但是,将其复制到“TEST”文件(无扩展名)时,大小为 13848 字节。我猜这可能是因为 DB2 读取/编码图像二进制文件的方式,但我不确定。有人可以强调错误的可能原因吗?关于使这项工作或调试的任何建议?