Database table structure:
If EXISTS (Select Name from Sysobjects Where Name = 'Content' and type = 'u') Drop Table ContentGo
Create Table Content (- Content ID Id Bigint Identity (1) Not Null, - Content Data Image Default Null, Constraint Pk_ContentId Primary Key Clustered (ID))
Go
Const string cnnstr = "provider = sqloledb.1; user ID = sa; password =; initial catalog = mydb; data source = (local)";
Write
public bool WriteContent (byte [] data) {OleDbConnection conn = new OleDbConnection (cnnstr); String mySelectQuery = "insert into Content (data) values (?)"; OleDbCommand myCommand = new OleDbCommand (mySelectQuery, conn); myCommand.CommandTimeout = 60; OleDbParameter param = new OleDbParameter ( "@ data", OleDbType.VarBinary, data.Length); param.Direction = ParameterDirection.Input; param.Value = data; myCommand.Parameters.Add (param); bool ret = false;
Try {conn.open (); myCommand.executenonQuery (); ret= true;} catch (exception e) {string msg = E.MESSAGE;} finally {// Close database connection IF ((conn! = null) && Conn.State! = connectionState.closed) {conn.close ();}} Return Ret;
read out
private byte [] ReadContent (Int64 contentId) {byte [] buff = null; OleDbConnection conn = new OleDbConnection (cnnstr); String queryStr = String.Format ( "select data from Content where Id = {0}", contentId); OleDbCommand myCommand = new OleDbCommand (queryStr, conn); myCommand.CommandTimeout = 60; OleDbDataReader reader = null; long dataLen = 0L; try {conn.Open (); reader = myCommand.ExecuteReader (); if (reader.Read ()) {// Get the data length to read, note that the buff must be empty to reference Datalen = Reader.getbytes (0, 0, buff, 0, 1); // Assign buffer buff = new byte [datalenten]; // read Data DATALEN = Reader.getbytes (0, 0, BUFF, 0, (INT) DATALEN;}} Catch (Exception E) {INTERRROR (E); String Msg = E.MESSAGE;} Finally {// Close Data Set IF (Reader! = Null &&! Reader.isclosed) Reader.close (); // Turns the database connection IF ((conn! = null) && (conn! = NULL) && (conn.state! = connectionstate.closed) {conn.close ();} } Return BUFF;} http://blog.9cbs.net/access9cbs/archive/2004/09/08/ 98235.aspx