i want to design a windows application using 3 tier architecture help me in this
hiii i want to create a windows application in this i want to use 3 tier architecture plz send me separate code for business layer data layer and presentation layer suppose my application is taking data from text boxes and inserting,updating and deleting in listview as well as in data base. friends help me regarding this
Naeem KhanPosted Jul 4, 2012, 3:30 AM
I m sending you one Database Layer which is related to MySql you can make as SQl server 2005 using same code just change the name SqlConnection inseted of MySqlconnection...
this is DataBase Layer... please same file in CS file...
i made this tier for MySql ...
// Code
using System;
using System.Xml.XPath;
using System.Data.SqlClient;
using System.Data;
using System.Xml;
using System.Xml.Xsl;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;
namespace DatabaseLayer
{
public class DatabaseLayer
{
private MySqlConnection _connection = null;
private string _connectionString = string.Empty;
public string ConnectionString
{
set { _connectionString = value; }
get { return _connectionString; }
}
public DatabaseLayer()
{
//
// TODO: Add constructor logic here
//
}
public bool OpenConnection()
{
ValidateConnectionString();
_connection = new MySqlConnection();
_connection.ConnectionString = _connectionString;
string exx;
try
{
_connection.Open();
}
catch (Exception ex)
{
exx = ex.Message;
_connection = null;
return false;
}
return true;
}
public bool CloseConnection()
{
bool result = true;
try
{
if (_connection.State == ConnectionState.Open)
{
_connection.Close();
}
}
catch
{
result = false;
}
finally
{
_connection = null;
}
return result;
}
public void ValidateConnectionString()
{
if (0 == _connectionString.Length)
{
throw new Exception("No connection string has been supplied");
}
}
public int ExecuteNonQuery(string storedProcedure, MySqlParameter[] parameterArray, MySqlParameter outputParam)
{
int result;
MySqlCommand comm = null;
string msg;
result = 0;
try
{
//ValidateConnectionString();
//conn = new SqlConnection();
//conn.ConnectionString = _connectionString;
comm = _connection.CreateCommand();
//conn.Open();
comm.CommandText = storedProcedure;
comm.CommandType = CommandType.StoredProcedure;
foreach (MySqlParameter param in parameterArray)
{
comm.Parameters.Add(param);
}
outputParam.Direction = ParameterDirection.Output;
comm.Parameters.Add(parameterArray);
result = comm.ExecuteNonQuery();
result = int.Parse(outputParam.Value.ToString());
}
catch (SqlException ex)
{
msg = ex.Message;
}
finally
{
comm = null;
}
// result = SqlHelper.ExecuteNonQuery(_connectionString, CommandType.StoredProcedure, storedProcedure, parameterArray);
return result;
}
public int ExecuteNonQuery(string storedProcedure, MySqlParameter[] parameterArray)
{
int result;
MySqlCommand comm = null;
string msg;
result = 0;
try
{
//ValidateConnectionString();
//conn = new SqlConnection();
//conn.ConnectionString = _connectionString;
comm = _connection.CreateCommand();
//conn.Open();
comm.CommandText = storedProcedure;
comm.CommandType = CommandType.StoredProcedure;
foreach (MySqlParameter param in parameterArray)
{
comm.Parameters.Add(param);
}
result = comm.ExecuteNonQuery();
}
catch (Exception ex)
{
msg = ex.Message;
}
finally
{
comm = null;
}
// result = SqlHelper.ExecuteNonQuery(_connectionString, CommandType.StoredProcedure, storedProcedure, parameterArray);
return result;
}
public object ExecuteScalar(string storedProcedure, MySqlParameter[] parameterArray)
{
object result;
MySqlCommand comm = null;
string msg;
try
{
comm = _connection.CreateCommand();
comm.CommandText = storedProcedure;
comm.CommandType = CommandType.StoredProcedure;
foreach (MySqlParameter param in parameterArray)
{
comm.Parameters.Add(param);
}
result = comm.ExecuteScalar();
}
// catch (Exception ex)
// {
// msg = ex.Message;
// }
finally
{
comm = null;
}
//ValidateConnectionString();
//result = SqlHelper.ExecuteScalar(_connectionString, CommandType.StoredProcedure, storedProcedure, parameterArray);
return result;
}
public DataSet ExecuteDataSet(string storedProcedure, MySqlParameter[] parameterArray)
{
DataSet result = new DataSet();
string msg;
MySqlDataAdapter da = null;
da = new MySqlDataAdapter();
MySqlCommand comm = null;
try
{
comm = _connection.CreateCommand();
comm.CommandText = storedProcedure;
comm.CommandType = CommandType.StoredProcedure;
foreach (MySqlParameter param in parameterArray)
{
comm.Parameters.Add(param);
}
da.SelectCommand = comm;
da.Fill(result, "data");
}
// catch (Exception ex)
// {
// msg = ex.Message;
// }
finally
{
da = null;
comm = null;
}
return result;
}
public MySqlDataReader ExecuteReader(string storedProcedure, MySqlParameter[] parameterArray)
{
MySqlDataReader result = null;
MySqlCommand comm = null;
comm = _connection.CreateCommand();
string msg;
comm.CommandText = storedProcedure;
comm.CommandType = CommandType.StoredProcedure;
foreach (MySqlParameter param in parameterArray)
{
comm.Parameters.Add(param);
}
try
{
result = comm.ExecuteReader();
}
catch (Exception ex)
{
msg = ex.Message;
}
finally
{
comm = null;
}
// ValidateConnectionString();
// result = SqlHelper.ExecuteReader(_connectionString, CommandType.StoredProcedure, storedProcedure, parameterArray);
return result;
}
public DataTable ExecuteDataTable(string storedProcedure, MySqlParameter[] parameterArray)
{
//SqlParameter[] parameter = new SqlParameter[2];
//parameter[0] = new SqlParameter("@userIDN", SqlDbType.Int);
//parameter[1] = new SqlParameter("@activeFlag", SqlDbType.Bit);
//parameter[0].Value = applicationUserIDN;
//parameter[1].Value = activeFlag;
DataTable result = new DataTable();
string msg;
MySqlDataAdapter dataAdapter = null;
dataAdapter = new MySqlDataAdapter();
MySqlCommand comm = null;
//OpenConnection();
comm = _connection.CreateCommand();
comm.CommandText = storedProcedure;
comm.CommandType = CommandType.StoredProcedure;
//comm.Parameters.Add(parameter[0]);
//comm.Parameters.Add(parameter[1]);
foreach (MySqlParameter param in parameterArray)
{
comm.Parameters.Add(param);
}
try
{
dataAdapter.SelectCommand = comm;
dataAdapter.Fill(result);
}
// catch (Exception ex)
// {
// msg = ex.Message;
// }
finally
{
CloseConnection();
dataAdapter = null;
comm = null;
}
return result;
}
public MySqlDataReader ExecuteReader(string storedProcedure)
{
MySqlDataReader result;
MySqlCommand comm = null;
string msg;
comm = _connection.CreateCommand();
comm.CommandText = storedProcedure;
comm.CommandType = CommandType.StoredProcedure;
// foreach (SqlParameter param in parameterArray)
// {
// comm.Parameters.Add(param);
// }
try
{
result = comm.ExecuteReader();
}
// catch (Exception ex)
// {
// msg = ex.Message;
//}
finally
{
comm = null;
}
// ValidateConnectionString();
// result = SqlHelper.ExecuteReader(_connectionString, CommandType.StoredProcedure, storedProcedure, parameterArray);
return result;
}
}
}
if you understand this code then reply me i will give you bussiness layer...
hemant kumarPosted Jul 4, 2012, 5:19 AM