Click here to Skip to main content
15,892,746 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;

namespace MultiTier.Example.Data {

	public class OrdersOle : MultiTier.Common.OleDBTier {

		public OrdersOle(string connection) : base(connection) {
		}

		public OrdersOle(string connection, string tableName) : base(connection, tableName) {
		}
	
		protected override void ConstructDataAdapter() {
			string strQuery = "";
			OleDbCommand cmSelect;
			OleDbCommand cmUpdate;
			OleDbCommand cmInsert;
			OleDbCommand cmDelete;

			//--- Set up the Connection
			InitializeConnection();

			//--- Set up the SELECT Command
			strQuery = @"SELECT OrderID, CustomerID, OrderDate, RequiredDate, ShippedDate, Freight
						 FROM Orders";
			cmSelect = null;
			cmSelect = new OleDbCommand(strQuery, this.Connection);
			cmSelect.CommandType = CommandType.Text;

			//--- Set up the UPDATE Command
			strQuery = @"UPDATE Orders
						 SET CustomerID = @CustomerID , OrderDate = @OrderDate, RequiredDate = @RequiredDate, ShippedDate = @ShippedDate, Freight = @Freight
						 WHERE OrderID = @OrderID";
			cmUpdate = null;
			cmUpdate = new OleDbCommand(strQuery, this.Connection);
			cmUpdate.CommandType = CommandType.Text;
			cmUpdate.Parameters.Add(new OleDbParameter("@CustomerID", OleDbType.WChar, 5, "CustomerID"));
			cmUpdate.Parameters.Add(new OleDbParameter("@OrderDate", OleDbType.DBTimeStamp, 14, "OrderDate"));
			cmUpdate.Parameters.Add(new OleDbParameter("@RequiredDate", OleDbType.DBTimeStamp, 14, "RequiredDate"));
			cmUpdate.Parameters.Add(new OleDbParameter("@ShippedDate", OleDbType.DBTimeStamp, 14, "ShippedDate"));
			cmUpdate.Parameters.Add(new OleDbParameter("@Freight", OleDbType.Currency, 16, "Freight"));
			cmUpdate.Parameters.Add(new OleDbParameter("@OrderID", OleDbType.Integer, 4, "OrderID"));

			//--- Set up the INSERT Command
			strQuery = @"INSERT INTO Customers (CustomerID, OrderDate, RequiredDate, ShippedDate, Freight)
						 VALUES (@CustomerID, @OrderDate, @RequiredDate, @ShippedDate, @Freight)";
			cmInsert = null;
			cmInsert = new OleDbCommand(strQuery, this.Connection);
			cmInsert.CommandType = CommandType.Text;
			cmInsert.Parameters.Add(new OleDbParameter("@CustomerID", OleDbType.WChar, 5, "CustomerID"));
			cmInsert.Parameters.Add(new OleDbParameter("@OrderDate", OleDbType.DBTimeStamp, 14, "OrderDate"));
			cmInsert.Parameters.Add(new OleDbParameter("@RequiredDate", OleDbType.DBTimeStamp, 14, "RequiredDate"));
			cmInsert.Parameters.Add(new OleDbParameter("@ShippedDate", OleDbType.DBTimeStamp, 14, "ShippedDate"));
			cmInsert.Parameters.Add(new OleDbParameter("@Freight", OleDbType.Currency, 16, "Freight"));

			//--- Set up the DELETE Command
			strQuery = @"DELETE FROM Orders
						 WHERE OrderID = @OrderID";
			cmDelete = null;
			cmDelete = new OleDbCommand(strQuery, this.Connection);
			cmDelete.CommandType = CommandType.Text;
			cmDelete.Parameters.Add(new OleDbParameter("@OrderID", OleDbType.Integer, 4, "OrderID"));
			//---------------------------------------------------------
			//--- Create and set up the DataAdapter
			//---------------------------------------------------------
			this.DataAdapter = new OleDbDataAdapter();
			this.DataAdapter.SelectCommand = cmSelect;
			this.DataAdapter.UpdateCommand = cmUpdate;
			this.DataAdapter.InsertCommand = cmInsert;
			this.DataAdapter.DeleteCommand = cmDelete;
			
			//--- Destroy connection object
			this.Connection = null;
		}
	}
}

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