Send and Recieve binary data
Send and Recieve binary data
Can bytea items be sent to and recieved from the database without using a dataset as shown in the "Pictures" sample?
If we are talking about PostgreSQL database, use PostgreSQLDirect .NET PgSqlBlob class.
Hopefully, this code snippet will be of help:
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();
}
}