|
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.
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.