dotConnect for PostgreSQL Documentation
Devart.Data.PostgreSql Namespace / PgSqlBlob Class
Members Example

PgSqlBlob Class
Represents a variable-length stream of binary data to be stored in or retrieved from a database.
Syntax
Remarks
PgSqlBlob is used to insert or retrieve BLOB fields in databases.
Example

In this example two functions are presented. First can upload a file onto a server, and second can retrieve a BLOB field and paste it into a file. Notice that two classes used here (BinaryWriter and BinaryReader) to provide more flexible stream manipulation are actually redundant for simple data transfer operations.

public void DownloadBlob(PgSqlConnection pgConnection)
{
  PgSqlCommand pgCommand = new PgSqlCommand("SELECT * FROM Test.Pictures", pgConnection);
  pgConnection.Open();
  PgSqlDataReader myReader = pgCommand.ExecuteReader(System.Data.CommandBehavior.Default);
  try
  {
    while (myReader.Read())
    {
      PgSqlBlob myBlob = myReader.GetPgSqlBlob(myReader.GetOrdinal("Picture"));
      if(!myBlob.IsNull)
      {
        string FN = myReader.GetString(myReader.GetOrdinal("PicName"));
        FileStream fs = new FileStream("D:\\Tmp\\"+FN+".bmp", FileMode.Create);
        BinaryWriter w = new BinaryWriter(fs);
        w.Write(myBlob.Value);
        w.Close();
        fs.Close();
        Console.WriteLine(FN+" downloaded.");
      }
    }
  }
  finally
  {
    myReader.Close();
    pgConnection.Close();
  }
}                                                                                                                   

public void UploadBlob(PgSqlConnection pgConnection)
{
  FileStream fs = new FileStream("D:\\Tmp\\_Water.bmp", FileMode.Open, FileAccess.Read);
  BinaryReader r = new BinaryReader(fs);
  PgSqlBlob myBlob = new PgSqlBlob(r.ReadBytes((int)fs.Length));
  PgSqlCommand pgCommand = new PgSqlCommand("INSERT INTO Test.Pictures (ID, PicName, Picture) VALUES(1,'Water',:Pictures)", pgConnection);
  pgCommand.Parameters.Add("Pictures",myBlob);
  pgConnection.Open();
  try
  {
    Console.WriteLine(pgCommand.ExecuteNonQuery()+" rows affected.");
  }
  finally
  {
    pgConnection.Close();
    r.Close();
  }
}
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
Inheritance Hierarchy

System.Object
   System.MarshalByRefObject
      System.IO.Stream
         Devart.Data.PostgreSql.PgSqlBlob
            Devart.Data.PostgreSql.PgSqlText

Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also