This article gives a C # programming to achieve read-write Binary example code, for beginners is a rare reference article ...
The following is a reference fragment:
//Return BLOB data
public MemoryStream GetBlob (string SQL)
... {
Try
... {
Db_conn ();
cmd = new OleDbCommand (SQL, Conn);
Cmd.commandtype = commandtype.text;//is sql
OleDbDataReader Rs = cmd. ExecuteReader ();
if (Rs.read ())//loop to next record
... {
if (!) ( Rs.getvalue (0) is system.dbnull))
... {
Byte[] image_bytes = (byte[]) rs.getvalue (0);
MemoryStream ms = new MemoryStream (image_bytes);
Return MS;
}
Else
return null;
}
Else
return null;
}
Finally
... {
This.close ();
}
//Set BLOB
public bool Setblob (string SQL, MemoryStream Ms)
... {
Try
... {
Db_conn ();
cmd = new OleDbCommand (SQL, Conn);
Cmd.commandtype = commandtype.text;//is sql
int n=convert.toint32 (Ms.Length.ToString ());
ms.position = 0;
byte[] Preadbyte = new Byte[n];
Ms.read (preadbyte, 0, N);
Cmd. Parameters.Add ("BLOB", oledbtype.binary). Value = Preadbyte;
Cmd. ExecuteNonQuery ();
ReturnTrue
}
catch (Exception ex)
... {
MessageBox.Show ("Error: Cause" + ex.) Message + ", unable to execute:" + SQL);
return false;
}
Finally
... {
This.close ();
}
}