2015-03-13 72 views
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 

回答

0

您在「插入」 ...更改此參數需要值...

Dim Command As NpgsqlCommand = New NpgsqlCommand("INSERT INTO table2 Values(@image1)", conn)    
    Command.Parameters.Add(New NpgsqlParameter("image1", NpgsqlTypes.NpgsqlDbType.Bytea)) 

對於這個...

Dim Command As NpgsqlCommand = New NpgsqlCommand("INSERT INTO table2 Values(:image1)", conn) 
    Dim param As NpgsqlParameter = New NpgsqlParameter("image1", NpgsqlTypes.NpgsqlDbType.Bytea) 
    param.Value = image1 
    Command.Parameters.Add(param) 

而且在 '找回' 你需要採取前...長度改變了...

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 

對於類似的東西...

Dim imageInBytes As Byte() 
    reader.Read() 

    ' Retrieve the length of the necessary byte array. 
    Dim column as integer = reader.GetOrdinal("name_column") 
    Dim len As Long = reader.GetBytes(column, 0, Nothing, 0, 0) 

    ' Create a buffer to hold the bytes, and then 
    ' read the bytes from DataReader. 
    ReDim imageInBytes(CInt(len)) 
    reader.GetBytes(column, 0, imageInBytes, 0, CInt(len)) 
    PictureBox1.Image = New Bitmap(New MemoryStream(imageInBytes)) 
相關問題