Click here to Skip to main content
15,895,709 members
Articles / Programming Languages / C#

An ADO.NET multi-database, multi-tier solution

Rate me:
Please Sign up or sign in to vote.
3.78/5 (26 votes)
9 Mar 2004CPOL8 min read 110.3K   4.3K   96  
A view of how ADO.NET can be used in a multi-database, multi-tier environment.
using System;
using System.Data;
using System.Data.OleDb;
using System.Globalization;

namespace MultiTier.Data {

	public abstract class OleDBTier : DataTier {

		private OleDbConnection cnConnection;
		private OleDbDataAdapter daDataAdapter;
		
		protected OleDBTier(string connection) : base(connection) {
		}

		protected OleDBTier(string connection, string tableName) : base(connection, tableName) {
		}

		public OleDbConnection Connection {
			get { return cnConnection; }
			set { cnConnection = value; }
		}

		public OleDbDataAdapter DataAdapter {
			get { return daDataAdapter; }
			set { daDataAdapter = value; }
		}

		protected abstract override void ConstructDataAdapter();

		public override DataTable ReadData() {
			/*--- Create a new DataSet ---*/
			this.Table = new DataTable(this.TableName);
			this.Table.Locale = CultureInfo.InvariantCulture;

			try {
				/*--- Fill the DataSet with the Customers ---*/
				this.AffectedRecords = daDataAdapter.Fill(this.Table);
				/*--- Return the DataSet ---*/
				return this.Table;
			}
			catch (OleDbException ex) {
				/*--- An error occurred, so we roll the transaction back ---*/
				this.Exception = ex;
				/*--- Return the DataSet ---*/
				return this.Table;
			}
		}

		public override void SaveData(DataTable dataTable) {
			this.Table = dataTable;
			OleDbTransaction trTransaction = null;

			/*--- Save the data ---*/
			try {
				/*--- Set up the conection manually ---*/
				InitializeConnection();
				cnConnection.Open();
				/*--- Begin a transaction ---*/ 
				trTransaction = cnConnection.BeginTransaction();
				/*--- Make all database changes ---*/
				this.AffectedRecords = daDataAdapter.Update(this.Table);
				/*--- Commit the changes ---*/
				trTransaction.Commit();
			}
			catch (DBConcurrencyException ex) {
				/*--- An error occurred, so we roll the transaction back ---*/
				this.Exception = ex;
				trTransaction.Rollback();
			}
			catch (OleDbException ex) {
				/*--- An error occurred, so we roll the transaction back ---*/
				this.Exception = ex;
				trTransaction.Rollback();
			}
			finally {
				/*--- Close the connection that we manually opened ---*/
				trTransaction = null;
				cnConnection.Close();
				cnConnection = null;
			}
		}

		public override void InitializeConnection() {
			cnConnection = new OleDbConnection(this.ConnectionString);
		}
	}
}

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
Web Developer
Belgium Belgium
I started using C# in the spring of 2003.
For the moment I'm working for a company that makes healthcare related software and do the ASP.NET programming in C# and VB.NET.

Comments and Discussions