Complicated it even further...![]()
Base class:
Derived 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(); } } }
Access: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; } } }
Sample usage: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") { } } }
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()); } } }




Reply With Quote
