Click here to Skip to main content
15,896,726 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.SqlClient;

namespace MultiTier.Example.Data {

	public class CustomersSql : MultiTier.Data.SqlTier {

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

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

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

			//--- Set up the SELECT Command
			strQuery = @"SELECT CustomerID, CompanyName, ContactName, City, Region, Timestamp
						 FROM Customers
						 ORDER BY CompanyName";
			cmSelect = null;
			cmSelect = new SqlCommand(strQuery, this.Connection);
			cmSelect.CommandType = CommandType.Text;

			//--- Set up the UPDATE Command
			strQuery = @"UPDATE Customers
						 SET CompanyName = @CompanyName , ContactName = @ContactName, City = @City, Region = @Region
						 WHERE CustomerID = @CustomerID
						 AND Timestamp = @Timestamp";
			cmUpdate = null;
			cmUpdate = new SqlCommand(strQuery, this.Connection);
			cmUpdate.CommandType = CommandType.Text;
			cmUpdate.Parameters.Add(new SqlParameter("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"));
			cmUpdate.Parameters.Add(new SqlParameter("@ContactName", SqlDbType.NVarChar, 30, "ContactName"));
			cmUpdate.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 15, "City"));
			cmUpdate.Parameters.Add(new SqlParameter("@Region", SqlDbType.NVarChar, 15, "Region"));
			cmUpdate.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.NChar, 5, "CustomerID"));
			cmUpdate.Parameters.Add(new SqlParameter("@Timestamp", SqlDbType.Timestamp, 8, "Timestamp"));

			//--- Set up the INSERT Command
			strQuery = @"INSERT INTO Customers (CompanyName, ContactName, City, Region, CustomerID)
						 VALUES (@CompanyName, @ContactName, @City, @Region, @CustomerID)";
			cmInsert = null;
			cmInsert = new SqlCommand(strQuery, this.Connection);
			cmInsert.CommandType = CommandType.Text;
			cmInsert.Parameters.Add(new SqlParameter("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"));
			cmInsert.Parameters.Add(new SqlParameter("@ContactName", SqlDbType.NVarChar, 30, "ContactName"));
			cmInsert.Parameters.Add(new SqlParameter("@City", SqlDbType.NVarChar, 15, "City"));
			cmInsert.Parameters.Add(new SqlParameter("@Region", SqlDbType.NVarChar, 15, "Region"));
			cmInsert.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.NChar, 5, "CustomerID"));

			//--- Set up the DELETE Command
			strQuery = @"DELETE FROM Customers
						 WHERE CustomerID = @CustomerID";
			cmDelete = null;
			cmDelete = new SqlCommand(strQuery, this.Connection);
			cmDelete.CommandType = CommandType.Text;
			cmDelete.Parameters.Add(new SqlParameter("@CustomerID", SqlDbType.NChar, 5, "CustomerID"));
			//---------------------------------------------------------
			//--- Create and set up the DataAdapter
			//---------------------------------------------------------
			this.DataAdapter = new SqlDataAdapter();
			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