Public Sub DownloadBlob(ByVal pgConnection As PgSqlConnection)
Dim pgCommand As New PgSqlCommand("SELECT * FROM Test.Pictures", pgConnection)
pgConnection.Open()
Dim myReader As PgSqlDataReader = pgCommand.ExecuteReader(System.Data.CommandBehavior.Default)
Try
While myReader.Read()
Dim myBlob As PgSqlBlob = myReader.GetPgSqlBlob(myReader.GetOrdinal("Picture"))
If Not myBlob.IsNull Then
Dim FN As String = myReader.GetString(myReader.GetOrdinal("PicName"))
Dim fs As FileStream = New FileStream("D:\Tmp\" + FN + ".bmp", FileMode.Create)
Dim w As BinaryWriter = New BinaryWriter(fs)
w.Write(myBlob.Value)
w.Close()
fs.Close()
Console.WriteLine(String.Concat(FN, " downloaded."))
End If
End While
Finally
myReader.Close()
pgConnection.Close()
End Try
End Sub
Public Sub UploadBlob(ByVal pgConnection As PgSqlConnection)
Dim fs As FileStream = New FileStream("D:\Tmp\_Water.bmp", FileMode.Open, FileAccess.Read)
Dim r As BinaryReader = New BinaryReader(fs)
Dim myBlob As PgSqlBlob = New PgSqlBlob(r.ReadBytes(Convert.ToInt32(fs.Length)))
Dim pgCommand As PgSqlCommand = New PgSqlCommand("INSERT INTO Test.Pictures (ID, PicName, Picture) VALUES(2,'Water',:Pictures)", pgConnection)
pgCommand.Parameters.Add("Pictures", myBlob)
pgConnection.Open()
Try
Console.WriteLine(String.Concat(pgCommand.ExecuteNonQuery(), " rows affected."))
Finally
pgConnection.Close()
r.Close()
End Try
End Sub