Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / C#

Firebird 2 and Images

Rate me:
Please Sign up or sign in to vote.
4.06/5 (10 votes)
9 Jun 2007CPOL1 min read 85.6K   1.6K   38  
Firebird 2 using stored procedure, manage Image ‘BLOB’ and easy to save and get back your images
/*
 * Created by SharpDevelop. [anish mm]
 * User: anish m m
 * Date: 18/Jan/2007
 * Time: 1:17 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using FirebirdSql.Data.FirebirdClient;

namespace FireB
{
	/// <summary>
	/// Description of iU1_Table.
	/// </summary>
	    public class iU1_Table
    {
        private string _Fname;
        private int _Uid;
        private int _Field01;
        private int _Autono;
        private String _FNAME;
        private String _LANEM;
        private String _SNAME;
        private String _PHONE;
        private String _MOB;
        private String _EMAIL;
        private MemoryStream _img;
        private static string connstring =ConfigurationManager.AppSettings["DBPathDef"].ToString();
        private static string connstring1 = ConfigurationManager.AppSettings["DBPathDef1"].ToString();        
        public FbConnection cnn = new FbConnection(connstring);
        public void OpenCon()
        {
          cnn.Open();
 
        }
        public void CloseCon()
        {
            cnn.Close();
        }
        public iU1_Table()
        {
        }
        public static DataTable iU1_TableD()
        {
            try
            {
                iU1_Table m = new iU1_Table();
                m.OpenCon();
                //string str = "select AUTONO,USERNAME,UID,FIELD01 from taB; ";
                DataTable dt = new DataTable();
                FbDataAdapter adb = new FbDataAdapter();
                FbCommand cmd = new FbCommand("SELECTUSER", m.cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                adb.SelectCommand = cmd;
                adb.Fill(dt);
                m.CloseCon();
                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public iU1_Table(string _uids)
        {
            try
            {
                iU1_Table m = new iU1_Table();
                DataSet dst =new DataSet();
                FbDataAdapter adb = new FbDataAdapter();
                FbCommand cmd = new FbCommand("SELECTUSERNO", cnn);
                cmd.Parameters.Add("@ID",FbDbType.Integer).Value =Convert.ToInt32(_uids);
                cmd.CommandType = CommandType.StoredProcedure;
                adb.SelectCommand = cmd;
                adb.Fill(dst,"tab");
                if(dst.Tables[0].Rows.Count >0)
                {
                    DataRow dr;
                    dr = dst.Tables[0].Rows[0];
                    if (!dr["USERNAME"].Equals(DBNull.Value))
                        FName =(string)dr["USERNAME"];
                    if(!dr["FNAME"].Equals(DBNull.Value))
                        FNAME = (string)dr["FNAME"];
                    
                    if (!dr["LNAME"].Equals(DBNull.Value))
                        LNAME = (string)dr["LNAME"];

                    if (!dr["SNAME"].Equals(DBNull.Value))
                        SNAME = (string)dr["SNAME"];

                    if (!dr["PHONE"].Equals(DBNull.Value))
                        PHONE = (string)dr["PHONE"];

                    if (!dr["MOB"].Equals(DBNull.Value))
                        MOB = (string)dr["MOB"];

                    if (!dr["IMG"].Equals(DBNull.Value))
                    {
                        byte[] content = (byte[])dr["IMG"];
                        MemoryStream stream = new MemoryStream(content);
                        Image = stream;
                    }

                    if (!dr["FNAME"].Equals(DBNull.Value))
                        FNAME = (string)dr["FNAME"];

                    if (!dr["EMAIL"].Equals(DBNull.Value))
                        EMAIL = (string)dr["EMAIL"];
 
                    AutoNo = Convert.ToInt32(dr["AUTONO"]);



                }
                m.cnn.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Save()
        {
            byte[] content = null;
            try
            {
                FbCommand CMD = new FbCommand("INSERTUSER", cnn);
                CMD.CommandType = CommandType.StoredProcedure;
                CMD.Parameters.Add("@USERNAME", FbDbType.VarChar).Value = _Fname;
                CMD.Parameters.Add("@F_NAME", FbDbType.VarChar).Value = _FNAME;
                CMD.Parameters.Add("@L_NAME", FbDbType.VarChar).Value = _LANEM;
                CMD.Parameters.Add("@S_NAME", FbDbType.VarChar).Value = _SNAME;
                CMD.Parameters.Add("@PHONE", FbDbType.VarChar).Value = _PHONE;
                CMD.Parameters.Add("@MOB", FbDbType.VarChar).Value = _MOB;
                CMD.Parameters.Add("@EMAIL", FbDbType.VarChar).Value = _EMAIL;
                if (_img != null)
                    content = _img.ToArray();
                CMD.Parameters.Add("@IMG", FbDbType.Binary).Value = content;


                CMD.Connection.Open();
                CMD.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Update()
        {
            byte[] content =null;
            try
            {
                FbCommand cmd = new FbCommand("UPDATEUSER", cnn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("@USERNAME", FbDbType.VarChar).Value = _Fname;
                cmd.Parameters.Add("@F_NAME", FbDbType.VarChar).Value = _FNAME;
                cmd.Parameters.Add("@L_NAME", FbDbType.VarChar).Value = _LANEM;
                cmd.Parameters.Add("@S_NAME", FbDbType.VarChar).Value = _SNAME;
                cmd.Parameters.Add("@PHONE", FbDbType.VarChar).Value = _PHONE;
                cmd.Parameters.Add("@MOB", FbDbType.VarChar).Value = _MOB;
                cmd.Parameters.Add("@EMAIL", FbDbType.VarChar).Value = _EMAIL;
                cmd.Parameters.Add("@ID", FbDbType.Integer).Value = _Autono;
                if (_img != null)
                    content = _img.ToArray();
                cmd.Parameters.Add("@EMP_IMG", FbDbType.Binary).Value = content;

                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void Delete(string _id)
        {
            iU1_Table m = new iU1_Table();
            try
            {
                
                FbCommand cmd = new FbCommand("DELETEUSER",m.cnn);
                cmd.Parameters.Add("@ID",FbDbType.Integer).Value =_id;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void AddField(string _string )
        {
            try
            {
                iU1_Table m = new iU1_Table();
                m.OpenCon();
                FbCommand cmd = new FbCommand(_string, m.cnn);
                cmd.ExecuteScalar();


            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void DropField(string _string)
        {
            try
            {
                iU1_Table m = new iU1_Table();
                m.OpenCon();
                FbCommand cmd = new FbCommand(_string, m.cnn);
                cmd.ExecuteScalar();
                m.CloseCon();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void BackUp(string _Pstring)
        {
            try
            {
                FirebirdSql.Data.Services.FbBackup n = new FirebirdSql.Data.Services.FbBackup();
                FirebirdSql.Data.Services.FbBackupFile fl = new FirebirdSql.Data.Services.FbBackupFile(_Pstring.ToUpper(), 12485);
                n.BackupFiles.Add(fl);
                n.ConnectionString = connstring;
                n.Execute();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void CreateData()
        {
            FbConnectionStringBuilder csb = new FbConnectionStringBuilder();
            csb.ServerType = FbServerType.Embedded;
            csb.Database = "CRM.fdb";
            csb.UserID = "SYSDBA";
            csb.Password = "218026";

            if (File.Exists(csb.Database))
            {
                File.Delete(csb.Database);
            }
            FbConnection.CreateDatabase(csb.ToString());
        }
        public static void ReStore(string _Pstring)
        {
            try
            {
                FbConnection.ClearAllPools();
                FirebirdSql.Data.Services.FbRestore n = new FirebirdSql.Data.Services.FbRestore();
                FirebirdSql.Data.Services.FbBackupFile fl = new FirebirdSql.Data.Services.FbBackupFile(_Pstring.ToUpper(),12485);
                n.BackupFiles.Add(fl);
                n.Options = FirebirdSql.Data.Services.FbRestoreFlags.Replace;
                n.Verbose = false;
                n.ConnectionString = connstring;
                n.Execute();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static string Connection()
        {
            try
            {
                iU1_Table m = new iU1_Table();
                return FbConnection.GetPooledConnectionCount(m.cnn).ToString();
             }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public static void CreateUser()
        {
        	try {
        		
        	} catch (Exception) {
        		
        	}
	
        }
        public string FName
        {
            get { return this._Fname; }
            set { this._Fname = value; }
        }
        public int Uid
        {
            get { return this._Uid; }
            set { this._Uid = value; }
        }
        public int Field
        {
            get { return this._Field01; }
            set { this._Field01 = value; }
        }
        public int AutoNo
        {
            get { return this._Autono; }
            set { this._Autono = value; }
        }
        public String FNAME
        {
            get
            {
                      return this._FNAME;
            }
            set
            {
                if (value == null)
                    this._FNAME = " ";
                else
                    this._FNAME = value;
            }
        }
        public String LNAME
        {
            get
            {
                    return this._LANEM;
            }
            set
            {
                if (value == null)
                    this._LANEM = " ";
                else
                    this._LANEM = value;
            }
        }
        public String SNAME
        {
            get
            {
                    return this._SNAME;
            }
            set
            {
                if (value == null)
                    this._SNAME = " ";
                else
                    this._SNAME=value;
            }
        }
        public String PHONE
        {
            get
            {
                    return this._PHONE;
            }
            set
            {
                if (value == null)
                    this._PHONE = " ";
                else
                    this._PHONE=value;
            }
        }
        public String MOB
        {
            get{return this._MOB;}
            set
            {
                if (value == null)
                    this._MOB = " ";
                else
                    this._MOB=value;
            }
        }
        public String EMAIL
        {
            get{return this._EMAIL;}
            set
            {
                if (value == null)
                    this._EMAIL = " ";
                else
                    this._EMAIL=value;
            }
        }
        public MemoryStream Image
        {
            get { return this._img; }
            set { this._img = value; }

        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions