using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Collections.Generic;
namespace CustomObjects
{
[System.ComponentModel.DataObject]
public class DataLayer
{
private static OleDbConnection _connection;
private static OleDbConnection Connection
{
get
{
if (DataLayer._connection == null)
{
DataLayer._connection = new OleDbConnection();
DataLayer._connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;
}
return DataLayer._connection;
}
}
#region Customers DataLayer
private static Customer CreateCustomerData(OleDbDataReader dataReader)
{
Customer customer = new Customer();
customer.CustomerID = dataReader.IsDBNull(0) ? string.Empty : dataReader.GetString(0);
customer.CompanyName = dataReader.IsDBNull(1) ? string.Empty : dataReader.GetString(1);
customer.ContactName = dataReader.IsDBNull(2) ? string.Empty : dataReader.GetString(2);
customer.ContactTitle = dataReader.IsDBNull(3) ? string.Empty : dataReader.GetString(3);
customer.Address = dataReader.IsDBNull(4) ? string.Empty : dataReader.GetString(4);
customer.City = dataReader.IsDBNull(5) ? string.Empty : dataReader.GetString(5);
customer.Region = dataReader.IsDBNull(6) ? string.Empty : dataReader.GetString(6);
customer.PostalCode = dataReader.IsDBNull(7) ? string.Empty : dataReader.GetString(7);
customer.Country = dataReader.IsDBNull(8) ? string.Empty : dataReader.GetString(8);
customer.Phone = dataReader.IsDBNull(9) ? string.Empty : dataReader.GetString(9);
customer.Fax = dataReader.IsDBNull(10) ? string.Empty : dataReader.GetString(10);
return customer;
}
public static CustomerCollection GetCustomers()
{
OleDbCommand command = new OleDbCommand();
command.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax From Customers";
command.Connection = DataLayer.Connection;
CustomerCollection customers = new CustomerCollection();
try
{
command.Connection.Open();
OleDbDataReader dataReader = command.ExecuteReader();
while (dataReader.Read())
{
Customer customer = DataLayer.CreateCustomerData(dataReader);
customers.Add(customer);
}
}
finally
{
command.Connection.Close();
}
return customers;
}
public static Customer GetCustomer(string customerID)
{
OleDbCommand command = new OleDbCommand();
command.CommandText = "SELECT CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax From Customers WHERE CustomerID = ?";
command.Parameters.Add(DataLayer.CreateParameter("CustomerID", customerID));
command.Connection = DataLayer.Connection;
try
{
command.Connection.Open();
OleDbDataReader dataReader = command.ExecuteReader();
if (dataReader.Read())
{
Customer customer = DataLayer.CreateCustomerData(dataReader);
return customer;
}
}
finally
{
command.Connection.Close();
}
return null;
}
public static int InsertCustomer(Customer customer)
{
OleDbCommand command = new OleDbCommand();
command.CommandText = "INSERT INTO Customers (CustomerID, CompanyName, ContactName, ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax) Values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
command.Connection = DataLayer.Connection;
command.Parameters.Add(DataLayer.CreateParameter("CustomerID", customer.CustomerID));
command.Parameters.Add(DataLayer.CreateParameter("CompanyName", customer.CompanyName));
command.Parameters.Add(DataLayer.CreateParameter("ContactName", customer.ContactName));
command.Parameters.Add(DataLayer.CreateParameter("ContactTitle", customer.ContactTitle));
command.Parameters.Add(DataLayer.CreateParameter("Address", customer.Address));
command.Parameters.Add(DataLayer.CreateParameter("City", customer.City));
command.Parameters.Add(DataLayer.CreateParameter("Region", customer.Region));
command.Parameters.Add(DataLayer.CreateParameter("PostalCode", customer.PostalCode));
command.Parameters.Add(DataLayer.CreateParameter("Country", customer.Country));
command.Parameters.Add(DataLayer.CreateParameter("Phone", customer.Phone));
command.Parameters.Add(DataLayer.CreateParameter("Fax", customer.Fax));
return DataLayer.ExecuteNonQuery(command);
}
public static int UpdateCustomer(Customer customer)
{
OleDbCommand command = new OleDbCommand();
command.CommandText = "UPDATE Customers SET CustomerID = ?, CompanyName = ?, ContactName = ?, ContactTitle = ?, " +
"Address = ?, City = ?, Region = ?, PostalCode = ?, Country = ?, Phone = ?, Fax = ? WHERE CustomerID = ?";
command.Parameters.Add(DataLayer.CreateParameter("CustomerID", customer.CustomerID));
command.Parameters.Add(DataLayer.CreateParameter("CompanyName", customer.CompanyName));
command.Parameters.Add(DataLayer.CreateParameter("ContactName", customer.ContactName));
command.Parameters.Add(DataLayer.CreateParameter("ContactTitle", customer.ContactTitle));
command.Parameters.Add(DataLayer.CreateParameter("Address", customer.Address));
command.Parameters.Add(DataLayer.CreateParameter("City", customer.City));
command.Parameters.Add(DataLayer.CreateParameter("Region", customer.Region));
command.Parameters.Add(DataLayer.CreateParameter("PostalCode", customer.PostalCode));
command.Parameters.Add(DataLayer.CreateParameter("Country", customer.Country));
command.Parameters.Add(DataLayer.CreateParameter("Phone", customer.Phone));
command.Parameters.Add(DataLayer.CreateParameter("Fax", customer.Fax));
command.Parameters.Add(DataLayer.CreateParameter("OriginalCustomerID", customer.CustomerID));
command.Connection = DataLayer.Connection;
return DataLayer.ExecuteNonQuery(command);
}
public static int DeleteCustomer(Customer customer)
{
OleDbCommand command = new OleDbCommand();
command.CommandText = "DELETE FROM Customers WHERE CustomerID = ?";
command.Connection = DataLayer.Connection;
command.Parameters.Add(DataLayer.CreateParameter("OriginalCustomerID", customer.CustomerID));
return DataLayer.ExecuteNonQuery(command);
}
#endregion
}
}
|