How to Upload Picture to Access Database

Developer Scripts

Create an access database like the picture:

Design the base form:

Create the CSharp (C#) class:
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(); }
        }
    }
}

The code for the "Set Picture" button:
private void btnSetPicture_Click(object sender, EventArgs e)
{
    try
    {
        OpenFileDialog m_OpenFile = new OpenFileDialog();
        m_OpenFile.InitialDirectory = Application.ExecutablePath.ToString();
        m_OpenFile.Title = "Buscar Imágenes";
        m_OpenFile.Filter = "Todos los archivos(*.*)|*.*|" +
            "Imagenes JPG (*.jpg)|*.jpg|" +
            "Imagenes GIF (*.gif)|*.gif";
        m_OpenFile.FilterIndex = 2;
        if (m_OpenFile.ShowDialog() == DialogResult.OK)
        {
            Class.AccessPictures pic = new Class.AccessPictures();
            pic.ConnStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;"+
                "Data Source={0};"+
                "Persist Security Info=False",
                "D:\\DOCS\\tyrodeveloper\\BlogPubCSharp\\BlogPubCSharp.accdb");
            pic.SetPicture("CUSTOMER", "ID_CUSTOMER", 
                txtId.Text, "LOGO", m_OpenFile.FileName);
        }
    }
    catch (Exception ex) {
        MessageBox.Show(ex.Message);
    }
}

The code for the "Get Picture":
private void btnGetPicture_Click(object sender, EventArgs e)
{
    try {
        Class.AccessPictures pic = new Class.AccessPictures();
        pic.ConnStr = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;"+
            "Data Source={0};Persist Security Info=False", 
            "D:\\DOCS\\tyrodeveloper\\BlogPubCSharp\\BlogPubCSharp.accdb");
        picAccessPicture.Image = 
            Image.FromStream(new MemoryStream(pic.GetPicture("LOGO", 
                "ID_CUSTOMER", "CUSTOMER", txtId.Text)));
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

Before run the program, you must add records to database.

Please Clic on +1

No hay comentarios:

Publicar un comentario