0
我想從VB.net插入和檢索Postgresql數據庫中的圖像按鈕單擊事件。我的表格有一列「image」,數據類型爲bytea
。我已經寫了下面的代碼,但沒有得到期望的輸出:插入和檢索Postgresql數據庫形式VBNet中的圖像
'Insert
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Try
Dim image1 As Byte()
Dim FS As FileStream = New FileStream("C:\0.jpg", FileMode.Open, FileAccess.Read)
Dim BR As BinaryReader = New BinaryReader(FS)
Dim lg As Long = FS.Length
Dim length As Integer = Convert.ToInt32(lg)
image1 = BR.ReadBytes(length)
conn.Open()
Dim Command As NpgsqlCommand = New NpgsqlCommand("INSERT INTO table2 Values(@image1)", conn)
'Dim Command As NpgsqlCommand = New NpgsqlCommand("INSERT INTO table1 values('" & timestamp & "','" & serialno & "','" & modelname & "','0','0','0','0','0','0','0','0','0','0','0','0')", conn)
Command.Parameters.Add(New NpgsqlParameter("@image1", NpgsqlTypes.NpgsqlDbType.Bytea))
Command.ExecuteNonQuery()
conn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
'retrieve
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Try
conn.Open()
Dim com As NpgsqlCommand = New NpgsqlCommand("select image from table2", conn)
Dim reader As NpgsqlDataReader = com.ExecuteReader()
Dim imageInBytes As Byte() = reader("image")
Dim memoryStream As System.IO.MemoryStream = _
New System.IO.MemoryStream(imageInBytes, False)
Dim image As System.Drawing.Image = _
System.Drawing.Image.FromStream(memoryStream)
image.Save("E:\image")
PictureBox6.Image = image
conn.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub