Results 1 to 34 of 34

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

Threaded View

  1. #1

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

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

    Last night I worked on this class, since I am new in C# I wish to know your feedbacks so I can improve it, it's an object that I intend to encapsulate the process of using parameters when executing action queries against an Access database...

    Code:
    using System;
    using System.Data;
    using System.Data.OleDb;
    using System.Windows.Forms;
    
    namespace Dee_U
    {
    	public class PreparedStatement
    	{
    		private OleDbConnection connApp;
    		private string strConnect;
    		private OleDbCommand cmd;
    	
    		//Constructor
    		public PreparedStatement(string db_provider, string db_path)
    		{
    			try
    			{
    				strConnect = "Provider=" + db_provider + ";Data Source=" + db_path + ";";
    				connApp = new OleDbConnection(strConnect);
    				connApp.Open();
    				//cmd.Connection = connApp;
    //				if (connApp.State.ToString()=close){
    //					MessageBox.Show("Cannot open database, pls. contact developer or system administrator.","Error");
    //					//application.exit;
    //				}				
    			}
    			catch(Exception ex)
    			{				
    				MessageBox.Show(ex.Message.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);	
    			}
    		}
    
    		public void SetParam(string pName,string pValue)
    		{
    			//cmd.Parameters.Add(pName,pValue);	
    			OleDbParameter pParam = new OleDbParameter();			
    			pParam.OleDbType = OleDbType.VarChar;
    			pParam.ParameterName = pName;
    			pParam.Value = pValue;
    			cmd.Parameters.Add(pParam);					
    		}
    
    		public void SetParam(string pName,int pValue)
    		{
    			//cmd.Parameters.Add(pName,pValue);	
    			OleDbParameter pParam = new OleDbParameter();			
    			pParam.OleDbType = OleDbType.Integer;
    			pParam.ParameterName = pName;
    			pParam.Value = pValue;
    			cmd.Parameters.Add(pParam);			
    		}
    
    		public void SetParam(string pName,bool pValue)
    		{
    			//cmd.Parameters.Add(pName,pValue);
    			OleDbParameter pParam = new OleDbParameter();			
    			pParam.OleDbType = OleDbType.Boolean;
    			pParam.ParameterName = pName;
    			pParam.Value = pValue;
    			cmd.Parameters.Add(pParam);			
    		}
    
    		public void SetParam(string pName,long pValue)
    		{
    			//cmd.Parameters.Add(pName,pValue);	
    			OleDbParameter pParam = new OleDbParameter();			
    			pParam.OleDbType = OleDbType.BigInt;
    			pParam.ParameterName = pName;
    			pParam.Value = pValue;
    			cmd.Parameters.Add(pParam);			
    		}
    
    		public void SetParam(string pName,double pValue)
    		{
    			//cmd.Parameters.Add(pName,pValue);
    			OleDbParameter pParam = new OleDbParameter();			
    			pParam.OleDbType = OleDbType.Double;
    			pParam.ParameterName = pName;
    			pParam.Value = pValue;
    			cmd.Parameters.Add(pParam);			
    		}
    
    		public void SetParam(string pName,short pValue)
    		{
    			//cmd.Parameters.Add(pName,pValue);
    			OleDbParameter pParam = new OleDbParameter();			
    			pParam.OleDbType = OleDbType.SmallInt;
    			pParam.ParameterName = pName;
    			pParam.Value = pValue;
    			cmd.Parameters.Add(pParam);			
    		}
    
    		public void SetParam(string pName,decimal pValue)
    		{
    			//cmd.Parameters.Add(pName,pValue);
    			OleDbParameter pParam = new OleDbParameter();			
    			pParam.OleDbType = OleDbType.Decimal;
    			pParam.ParameterName = pName;
    			pParam.Value = pValue;
    			cmd.Parameters.Add(pParam);			
    		}
    		
    		public void SetParam(string pName,DateTime pValue)
    		{	
    			OleDbParameter pParam = new OleDbParameter();			
    			pParam.OleDbType = OleDbType.DBDate;
    			pParam.ParameterName = pName;
    			pParam.Value = pValue;
    			cmd.Parameters.Add(pParam);			
    		}
    
    		public void SetStatement(string pStmt)
    		{
    			if (pStmt!=string.Empty)
    			{	
    				cmd = new OleDbCommand(pStmt);
    				cmd.Connection = connApp;
    				cmd.CommandType = CommandType.Text;
    				cmd.Prepare();				
    			}			
    		}
    
    		public void ExecuteSQL()
    		{
    			try				
    			{				
    				cmd.ExecuteNonQuery();
    				connApp.Close();
    			}
    			catch(Exception ex)
    			{
    				MessageBox.Show(ex.Message.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);	
    			}
    		}
    
    	}
    }
    Code:
    //Sample Usage
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    namespace Dee_U
    {
    	/// <summary>
    	/// Summary description for Form1.
    	/// </summary>
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Button button1;
    		private const string db_provider = "Microsoft.Jet.OLEDB.4.0";
    		private const string db_path = "C:\\dee_u.mdb";
    
    		private const string DB_INSERT = "INSERT INTO MyTable(a,b,c,d,e) VALUES (?,?,?,?,?);";
    		//private const string DB_INSERT = "INSERT INTO MyTable(a,b,c,e) VALUES (?,?,?,?);";
    		private const string DB_UPDATE = "UPDATE MyTable SET a = ?,b = ?,c = ?,d = ?,e = ? WHERE a = ?;";		
    		private const string DB_DELETE = "DELETE * FROM eData;";
    		/// <summary>
    		/// The main entry point for the application.
    		/// </summary>
    		[STAThread]
    		static void Main() 
    		{
    			Application.Run(new Form1());
    		}
    
    		private void button1_Click(object sender, System.EventArgs e)
    		{
    			PreparedStatement x;
    			x = new PreparedStatement(db_provider,db_path);
    			x.SetStatement(DB_INSERT);
    			x.SetParam("a","reo'o");
    			x.SetParam("b","rodriguez");
    			x.SetParam("c",100.000101);
    			x.SetParam("d",DateTime.Now.Date);
    			x.SetParam("e",true);
    			x.ExecuteSQL();
    			MessageBox.Show("DONE!");
    		}
    
    		private void button2_Click(object sender, System.EventArgs e)
    		{
    			PreparedStatement x;
    			x = new PreparedStatement(db_provider,db_path);
    			x.SetStatement(DB_UPDATE);
    			x.SetParam("a","xx");
    			x.SetParam("b","xx");
    			x.SetParam("c",104343.000101);
    			x.SetParam("d",DateTime.Now);
    			x.SetParam("e",false);
    			x.SetParam("a","x");
    			x.ExecuteSQL();
    			MessageBox.Show("DONE!");
    		}
    	}
    }
    Last edited by dee-u; Feb 21st, 2006 at 10:45 PM.
    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