Click here to Skip to main content
15,884,537 members
Articles / Programming Languages / C#

Data Access Component - declarative transactions at the method level without EnterpriseServices, different data sources supported, Part 1

Rate me:
Please Sign up or sign in to vote.
4.75/5 (18 votes)
29 Oct 20029 min read 259.5K   1.7K   108  
Data Access Component - thin wrapper of ADO.NET eliminating the need for the transactional support provided by COM+, supplying a uniform interface to different data sources thus allowing for easy switching to another data source eventually
using System;
using System.Data;
using System.Collections;
using System.Diagnostics;

namespace DAC {
	/// <summary>
	/// IDataReader thin wrapper
	/// Close() calls TransactionManager.SetHoldComplete()
	/// </summary>
	public class DbDataReader : IDataReader, IEnumerable {
		private IDataReader _dr = null;
		private IDbConnection _con = null;
		private DbData _dbData = null;

		public DbDataReader(IDataReader dr) {
			_dr = dr;
		}

		public IDbConnection Connection {
			set { _con = value; }
		}

		public DbData DbData {
			set { _dbData = value; }
		}

		public void Close() {
			_dr.Close();

			if(_dbData != null && _dbData._dataParams != null) {
				_dbData.PopParams(_dbData._cmd, _dbData._dataParams);
				_dbData.RecordsAffected = _dr.RecordsAffected;
				_dbData = null;
			}

			_dr = null;

			if(_con != null) {
				_con.Close();
				_con = null;
				Debug.WriteLine("Connection closed after IDataReader.");
			}
			else {
				TransactionManager.SetHoldComplete();
			}
		}

		public void Dispose() {
			Close();
		}

		#region IEnumerable implementation
		public IEnumerator GetEnumerator() {
			return (IEnumerator)(_dr as IEnumerable).GetEnumerator();
		}
		#endregion

		#region IDataReader implementation
		public int Depth {
			get { return _dr.Depth;  }
		}

		public bool IsClosed {
			get  { return _dr.IsClosed; }
		}

		public int RecordsAffected {
			get { return _dr.RecordsAffected; }
		}

		public bool NextResult() {
			return _dr.NextResult();
		}

		public bool Read() {
			return _dr.Read();
		}

		public DataTable GetSchemaTable() {
			return _dr.GetSchemaTable();
		}

		public int FieldCount {
			get { return _dr.FieldCount; }
		}

		public String GetName(int i) {
			return _dr.GetName(i);
		}

		public String GetDataTypeName(int i) {
			return _dr.GetDataTypeName(i);
		}

		public Type GetFieldType(int i) {
			return _dr.GetFieldType(i);
		}

		public Object GetValue(int i) {
			return _dr.GetValue(i);
		}

		public int GetValues(object[] values) {
			return _dr.GetValues(values);
		}

		public int GetOrdinal(string name) {
			return _dr.GetOrdinal(name);
		}

		public object this [ int i ] {
			get { return _dr[i]; }
		}

		public object this [ String name ] {
			get { return _dr[name]; }
		}

		public bool GetBoolean(int i) {
			return _dr.GetBoolean(i);
		}

		public byte GetByte(int i) {
			return _dr.GetByte(i);
		}

		public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length) {
			return _dr.GetBytes(i, fieldOffset, buffer, bufferoffset, length);
		}

		public char GetChar(int i) {
			return _dr.GetChar(i);
		}

		public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length) {
			return _dr.GetChars(i, fieldoffset, buffer, bufferoffset, length);
		}

		public Guid GetGuid(int i) {
			return _dr.GetGuid(i);
		}

		public Int16 GetInt16(int i) {
			return _dr.GetInt16(i);
		}

		public Int32 GetInt32(int i) {
			return _dr.GetInt32(i);
		}

		public Int64 GetInt64(int i) {
			return _dr.GetInt64(i);
		}

		public float GetFloat(int i) {
			return _dr.GetFloat(i);
		}

		public double GetDouble(int i) {
			return _dr.GetDouble(i);
		}

		public String GetString(int i) {
			return _dr.GetString(i);
		}

		public Decimal GetDecimal(int i) {
			return _dr.GetDecimal(i);
		}

		public DateTime GetDateTime(int i) {
			return _dr.GetDateTime(i);		
		}

		public IDataReader GetData(int i) {
			return _dr.GetData(i);
		}

		public bool IsDBNull(int i) {
			return _dr.IsDBNull(i);
		}
		#endregion
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions