This paper presents a programming with C # literacy Binary code examples for beginners, is a rare reference article……
The following is quoted fragment:
/ / 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 ()) / / cycle to the next a record
(...
If (! (Rs.GetValue (0) is System.DBNull))
(...
Image_bytes byte [] = (byte []) Rs.GetValue (0);
MemoryStream ms = new MemoryStream (image_bytes);
Return ms;
)
Else
Return null;
)
Else
Return null;
)
Finally
(...
This.close ();
)
)
/ / Set the 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 [] = new Byte pReadByte [n];
Ms.Read (pReadByte, 0, n);
Cmd.Parameters.Add ( "BLOB" OleDbType.Binary). Value = pReadByte;
Cmd.ExecuteNonQuery ();
Return true;
)
Catch (Exception ex)
(...
MessageBox.Show ( "error: for" ex.Message + + ", can not be implemented:" + SQL);
Return false;
)
Finally
(...
This.close ();
)
)
Call getBlob
The following is quoted fragment:
String sqlStr = "select content from dp where id =" + ID; / / dp content for the BLOB field, ID-based bond
MemoryStream ms = DBClass.getBlob (sqlStr);
If (ms == null)
RichTextBox.Clear ();
Else
(...
If (ms.Length> 0)
(...
Ms.Position = 0;
Try
(...
RichTextBox.LoadFile (ms, RichTextBoxStreamType.RichText);
Catch () ...
RichTextBox.LoadFile (ms, RichTextBoxStreamType.PlainText);
)
) Else
RichTextBox.Clear ();
)
Call setBlob
The following is quoted fragment:
String sqlStr = "update dp set content =: BLOB where id =" + ID;
MemoryStream ms = new MemoryStream ();
RichTextBox.SaveFile (ms, RichTextBoxStreamType.RichText);
If (! DBClass.SetBlob (sqlStr, ms))
(...
MessageBox.Show ( "Save failure");
) |