using System; using System.Data.OleDb; using System.IO; namespace TyroDeveloper.Class { public class AccessPictures { string _connStr = ""; public string ConnStr { get { return _connStr; } set { _connStr = value; } } public bool SetPicture(string tableName, string fieldId, string fieldIdValue, string targetFieldName, string fileName){ OleDbConnection conn = new OleDbConnection(_connStr); try { System.IO.FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); System.IO.BinaryReader br = new System.IO.BinaryReader(fs); byte[] picture = new byte[fs.Length]; br.Read(picture, 0, (int)fs.Length); br.Close(); fs.Close(); string SQL = String.Format("UPDATE [{0}] "+ " SET [{1}]=@IMG WHERE [{2}]=@ID", tableName, targetFieldName, fieldId); conn.Open(); OleDbCommand comm = new OleDbCommand(SQL, conn); //IMAGE OleDbParameter parImagen = new OleDbParameter("@IMG", OleDbType.VarBinary, picture.Length); parImagen.Value = picture; comm.Parameters.Add(parImagen); //ID OleDbParameter parId = new OleDbParameter("@ID", fieldIdValue); comm.Parameters.Add(parId); comm.ExecuteNonQuery(); return (true); } catch (Exception ex) { throw (ex); } finally { conn.Close(); } } public byte[] GetPicture(string targetFieldName,string fieldIdName, string tableName, string fieldIdValue){ OleDbConnection conn = new OleDbConnection(_connStr); try { conn.Open(); string SQL = string.Format("SELECT [{0}], [{1}] "+ " FROM [{2}] WHERE [{1}]=@ID AND [{0}] IS NOT NULL", targetFieldName, fieldIdName, tableName); OleDbCommand comm = new OleDbCommand(SQL, conn); OleDbParameter parId = new OleDbParameter("@ID", fieldIdValue); comm.Parameters.Add(parId); OleDbDataReader dr = null; dr = comm.ExecuteReader(); byte[] aBytes = null; if (dr.Read()) { aBytes = (byte[])dr[targetFieldName]; } dr.Close(); return (aBytes); } catch (Exception ex) { throw (ex); } finally { conn.Close(); } } } }
Please visit: www.TyroDeveloper.com
Please Click on +1
No comments:
Post a Comment