Send and Recieve binary data

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
Duane
Posts: 3
Joined: Wed 29 Mar 2006 20:41

Send and Recieve binary data

Post by Duane » Wed 29 Mar 2006 20:43

Can bytea items be sent to and recieved from the database without using a dataset as shown in the "Pictures" sample?

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Thu 30 Mar 2006 09:41

Yes.
Please refer to MySqlBlob class for more information on how to send and recieve blobs from a database.

Duane
Posts: 3
Joined: Wed 29 Mar 2006 20:41

Post by Duane » Thu 30 Mar 2006 20:02

Ummm I thought this was the PostgreSQLDirect.NET forum not the MySQLDirect .NET form. So are you saying I can use the MySQL methods (like GetMySqlBlob)?

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Fri 31 Mar 2006 06:12

If we are talking about PostgreSQL database, use PostgreSQLDirect .NET PgSqlBlob class.
Hopefully, this code snippet will be of help:

Code: Select all

public Bitmap DownloadBlob(PgSqlConnection Connection, string picname)
    {
      PgSqlCommand Command = new PgSqlCommand("SELECT Picture FROM Test.pgsqlnet_pictures WHERE Name=:picname", Connection); 
      Command.Parameters.Add("picname", picname);
      Connection.Open(); 
      try 
      { 
        using (PgSqlDataReader Reader = Command.ExecuteReader())
        {
          if (Reader.Read()) 
            return new Bitmap(Reader.GetPgSqlBlob(0));
        } 
      }
      finally 
      { 
        Connection.Close(); 
      } 
      return null;
    }            

    public void UploadBlob(PgSqlConnection Connection, Bitmap bitmap, string picname) 
    { 
      PgSqlBlob myBlob = new PgSqlBlob();
      bitmap.Save(myBlob, ImageFormat.Bmp);
      PgSqlCommand Command = new PgSqlCommand("INSERT INTO Test.pgsqlnet_pictures (NAME, PICTURE) VALUES(:picname,:picture)", Connection); 
      Command.Parameters.Add("picname", picname); 
      Command.Parameters.Add("picture", myBlob); 
      Connection.Open(); 
      try 
      { 
        Console.WriteLine(Command.ExecuteNonQuery() + " rows affected."); 
      } 
      finally 
      { 
        Connection.Close();
      } 
    }

Duane
Posts: 3
Joined: Wed 29 Mar 2006 20:41

Post by Duane » Fri 31 Mar 2006 15:37

That code looks interesting, is that C# code?

Alexey
Posts: 2756
Joined: Mon 13 Mar 2006 07:43

Post by Alexey » Mon 03 Apr 2006 06:24

Yes. This is all C#.

Post Reply