Results 1 to 34 of 34

Thread: Want your feedback on my first object in C# (with sample solution now!)

Hybrid View

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Want your feedback on my first object in C# (evolving)

    Complicated it even further...

    Base class:
    Code:
    using System;
    using System.Configuration;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Data.OleDb;
    using System.Windows.Forms;
    using DataAccess;
    
    public abstract class BaseClass : IDisposable
    {
        private DbCommand _cmd;
        private DbConnection _conn;
        private DbProviderFactory _factory;
    
        protected BaseClass()
        {}
    
        protected BaseClass(string databaseType)
        {
            MessageBox.Show(databaseType);
            ConnectionStringSettings connectionSetting = ConfigurationManager.ConnectionStrings[databaseType];
            MessageBox.Show(connectionSetting.ConnectionString);
            _factory = DbProviderFactories.GetFactory(connectionSetting.ProviderName);
            _conn = _factory.CreateConnection();
            _conn.ConnectionString = connectionSetting.ConnectionString;        
            SetCommand();
        }
    
        public void SetCommand()
        {
            _cmd = _conn.CreateCommand();
            _cmd.Connection = _conn;
        }
    
        public void AddParam(string parameterName, object parameterValue)
        {
            DbParameter param = _cmd.CreateParameter();
            param.ParameterName = parameterName;
            param.Direction = ParameterDirection.Input;
            if (parameterValue != null)
            {
                param.Value = parameterValue;
            }
            else
            {
                param.Value = System.DBNull.Value;
            }        
            _cmd.Parameters.Add(param);
        }
    
        protected DbCommand Command
        {
            get
            {
                return _cmd;
            }
        }
    
        protected DbProviderFactory Factory
        {
            get
            {
                return _factory;
            }
    
        }
    
        protected DbConnection Connection
        {
            get
            {
                return _conn;
            }
        }
    
        public void Dispose()
        {
            // dispose of the transaction if it exists
            //if (_transaction != null)
            //    _transaction.Dispose();
    
            // dispose of the connection
            if (_conn != null)
            {
                if (_conn.State == ConnectionState.Open)
                    _conn.Close();
                _conn.Dispose();
            }
        }
    
    }
    Derived class:
    Code:
    //Copyright : dee-u
    
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Data.OleDb;
    using DataAccess;
    using System.Windows.Forms;
    //
    namespace DataAccess
    {
    	public abstract class Methods : BaseClass
    	{
            protected Methods() : base()
            {}
    
            protected Methods(string databaseType) : base(databaseType)
            {}
    
            public void PrepareQuery(string pStmt)
    		{
                if ((pStmt == null))
                {
                    throw new ArgumentNullException("pStmt");
                }
                if (this.Command.CommandText != pStmt)
                {
                    SetCommand();
                }
    			this.Command.CommandType = CommandType.Text;
    			this.Command.CommandText = pStmt;			
    		}
    
    		public double ExecuteActionQuery()
    		{
                this.Connection.Open();
                double result = (double)this.Command.ExecuteNonQuery();
                this.Connection.Close();
    			return result;
    		}			
    	
            public bool RecordExists()
    		{
                this.Connection.Open();
                IDataReader reader = this.Command.ExecuteReader(CommandBehavior.SingleResult);
    			bool result = reader.Read();
    			reader.Close();
                this.Connection.Close();
    			return result;
    		}
    
    		public int RecordCount()
    		{
                this.Connection.Open();
                int result = (int)this.Command.ExecuteScalar();
                this.Connection.Close();
    			return result;
    		}
    
            public object GetFieldValue()
    		{
                this.Connection.Open();
                object result = this.Command.ExecuteScalar();
                this.Connection.Close();
                if (result != null)
                {   
                    return result;
                }
                else
                {
    			    return string.Empty;
                }
            }
    
            //overload for ComboBox
            public void PopulateList(ref ComboBox control)
            {
                this.Connection.Open();
                IDataReader reader = this.Command.ExecuteReader(CommandBehavior.SequentialAccess);
                control.Items.Clear();
                while (reader.Read())
                {
                    control.Items.Add(reader.GetValue(0));
                }
                reader.Close();
                this.Connection.Close();
            }
    
            //overload for Listbox    
            public void PopulateList(ref ListBox control)
            {
                this.Connection.Open();
                DbDataReader reader = this.Command.ExecuteReader(CommandBehavior.SequentialAccess);
                control.Items.Clear();
                while (reader.Read())
                {
                    control.Items.Add(reader.GetValue(0));
                }
                reader.Close();
                this.Connection.Close();
            }
    
            public DataSet FillDataSet()
            {
                //DbDataAdapter adapter = Factory.CreateAdapter(this.Command);
                DbDataAdapter adapter = this.Factory.CreateDataAdapter();
                adapter.SelectCommand = this.Command;
                DataSet ds = new DataSet();            
                adapter.Fill(ds);
                return ds;
            }
    	}
    }
    Access:
    Code:
    //Copyright : dee-u
    
    using System;
    using DataAccess;
    using System.Data;
    using System.Data.OleDb;
    
    namespace DataAccess
    {	
    	public class OleDb : DataAccess.Methods
    	{		
            public OleDb() : base("MS Access")
    		{				
    		}
    	}
    }
    Sample usage:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.Common;
    using System.Configuration;
    using System.Diagnostics;
    using DataAccess.CommonUtilities;
    using DataAccess;
    using BusinessLogic.CommonFunctions;
    
    namespace ApplicationLayer
    {
        public partial class Form1 : Form
        {
    
            private const string DB_INSERT = "INSERT INTO MyTable(a,b,c,d,e,f) VALUES (?,?,?,?,?,?);";        
    
            public Form1()
            {
                InitializeComponent();            
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                DataAccess.Methods x = new DataAccess.OleDb();                        
                x.PrepareQuery(DB_INSERT);
                x.AddParam("a", "reo'o");
                x.AddParam("b", null);
                x.AddParam("c", 100.000101);
                x.AddParam("d", DateTime.Now.Date);
                x.AddParam("e", true);
                x.AddParam("f", this.pictureBox1);
                if (x.ExecuteActionQuery() != 0)
                {
                    MessageBox.Show("DONE!");
                }
                else
                {
                    MessageBox.Show("NOT DONE!");
                }          
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                DataAccess.CommonUtilities.Access x = new DataAccess.CommonUtilities.Access();            
                x.PrepareQuery("SELECT * FROM MyTable WHERE x = ?");
                x.AddParam("x", 199);
                try
                {
                    CommonFunctions.Inform(x.RecordExists().ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.InnerException.ToString());
                }
                x.PrepareQuery("SELECT Count(a) FROM MyTable WHERE b = ?");
                x.AddParam("b", "rodriguez");
                MessageBox.Show(x.RecordCount().ToString());
            }
    
            private void button3_Click(object sender, EventArgs e)
            {
                DataAccess.CommonUtilities.Access x = new DataAccess.CommonUtilities.Access();
                //x.BackUpAccessDB(@"C:\dee_u.mdb",@"C:\d.mdb");
                //MessageBox.Show("done");
                x.PrepareQuery("SELECT x FROM MyTable WHERE d = ?");
                x.AddParam("d", DateTime.Now.Date);
                x.PopulateList(ref this.comboBox1);
                x.PopulateList(ref this.listBox1);
            }
    
            private void button4_Click(object sender, EventArgs e)
            {
                DataAccess.CommonUtilities.Access xx = new DataAccess.CommonUtilities.Access();
                xx.PrepareQuery("SELECT * FROM MyTable");
                DataSet y = xx.FillDataSet();
                MessageBox.Show(y.Tables.Count.ToString());
            }
    
        }
    }
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Want your feedback on my first object in C# (evolving)

    It's done, check on my sig, thanks for the people who have help me...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width