这段时间在做一个类似博客的教师发布系统,要求用三层架构搭建。以前,做东西只会用文件夹的层状结构搭建,自己思路也比较清晰,自从看了三层架构觉得很不错,完全解释了OO。下面就用一个实例探索哈~
三层架构(3-tier application)分如下3层:UI(User Interface,表现层),BLL(Business Logic,业务逻辑层),DAL(DataAccess,数据访问层)。
UI(表现层)--它的职责是数据的展示和采集,数据采集的结果通常以Entity object提交给BLL层处理。Service Interface侧层用于将业务或数据资源发布为服务(如WebServices)。
BLL(业务逻辑层)--它的任务是按预定的逻辑处理UI提交的请求,我觉得它有点像一个传送带,把数据访问层的函数返回,并等待被调用。
DAL(数据访问层)--负责从数据库中存取资源,并向BEM子层屏蔽所有的SQL语句以及数据库类型差异,它是直接和数据库打交道的,实施数据库的操作。
下面咱们看实际代码吧:
UI层就不说了,它主要是调用业务逻辑层的东东。
DAL层:
Code
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Reflection;
/// <summary>
//========高处不胜寒Asp.Net三层结构数据层公共类V1.1=========
//
// '''
// (0 0)
// +-----oOO----(_)-------------------+
// | |
// | |
// QQ:352623642 |
// | |
// | 类型:Web版 |
// | 适用数据库:Sql Sever |
// | 更新时间:2005-08-13 |
// | 技术支持网站:ASP.NET技术团队 |
// | |
// +------------------oOO-------------+
// |__|__|
// || ||
// ooO Ooo
//
//============================================================
/// </summary>
namespace ThreeLayer.DAL
{
public abstract class Data
{
// ===数据库连接串设置===
public static readonly string conn_Default = ConfigurationSettings.AppSettings["conn_Default"]; // 系统默认数据库连接串
// ==============================================================
// ========================数据库底层操作==============================
// ==============================================================
/// <summary>
/// 执行ExecuteNonQuery
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="cmdType">Sql语句类型</param>
/// <param name="cmdText">Sql语句</param>
/// <param name="cmdParms">Parm数组</param>
/// <returns>返回影响行数</returns>
public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
cmd.Connection=conn;
cmd.CommandText = cmdText;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
return val;
}
}
/// <summary>
/// 返回一个SqlParameter实例
/// </summary>
/// <param name="ParamName">字段名</param>
/// <param name="stype">字段类型</param>
/// <param name="size">范围</param>
/// <param name="Value">赋值</param>
/// <returns>返回一个SqlParameter实例</returns>
public static SqlParameter MakeParam(string ParamName,System.Data.SqlDbType stype,int size,Object Value)
{
SqlParameter para=new SqlParameter(ParamName,Value);
para.SqlDbType=stype;
para.Size=size;
return para;
}
/// <summary>
/// 获得SqlParameter实例
/// </summary>
/// <param name="ParamName">字段名</param>
/// <param name="Value">赋值</param>
/// <returns>返回一个SqlParameter实例</returns>
public static SqlParameter MakeParam(string ParamName,string Value)
{
return new SqlParameter(ParamName, Value);
}
/// <summary>
/// 获得DateSet实例(获得单页记录)
/// </summary>
/// <param name="int_PageSize">一页显示的记录数</param>
/// <param name="int_CurrentPageIndex">当前页码</param>
/// <param name="connString">数据库连接串</param>
/// <param name="cmdType">Sql语句类型</param>
/// <param name="cmdText">Sql语句</param>
/// <param name="cmdParms">Parm数组</param>
/// <returns></returns>
public static DataSet ExecuteDataSet(int int_PageSize,int int_CurrentPageIndex,string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlConnection conn = new SqlConnection(connString);
try
{
conn.Open();
System.Data.SqlClient.SqlDataAdapter da=new SqlDataAdapter(cmdText,conn);
da.SelectCommand.CommandType=cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
da.SelectCommand.Parameters.Add(parm);
}
conn.Close();
DataSet ds=new DataSet();
if (int_PageSize==0 && int_CurrentPageIndex==0)
{
da.Fill(ds,"12news1234567890");
}
else
{
int int_Page=int_PageSize*(int_CurrentPageIndex-1);
if (int_Page<0)
{
int_Page=0;
}
da.Fill(ds,int_Page,int_PageSize,"12news1234567890");
}
return ds;
}
catch
{
conn.Close();
throw;
}
}
/// <summary>
/// 获得DateSet实例(获得全部记录)
/// </summary>
/// <param name="connString">数据库连接串</param>
/// <param name="cmdType">Sql语句类型</param>
/// <param name="cmdText">Sql语句</param>
/// <param name="cmdParms">Parm数组</param>
/// <returns></returns>
public static DataSet ExecuteDataSet(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlConnection conn = new SqlConnection(connString);
try
{
conn.Open();
System.Data.SqlClient.SqlDataAdapter da=new SqlDataAdapter(cmdText,conn);
da.SelectCommand.CommandType=cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
da.SelectCommand.Parameters.Add(parm);
}
conn.Close();
DataSet ds=new DataSet();
da.Fill(ds,"12news1234567890");
return ds;
}
catch
{
conn.Close();
throw;
}
}
/// <summary>
/// 执行ExecuteScalar
/// </summary>
/// <param name="connString">数据库连接串</param>
/// <param name="cmdType">Sql语句类型</param>
/// <param name="cmdText">Sql语句</param>
/// <param name="cmdParms">Parm数组</param>
/// <returns>返回第一行第一列记录值</returns>
public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
conn.Close();
return val;
}
}
// ==============================================================
// ===================数据库操作:插入,修改,列表显示,以及获得详细记录=================
// ==============================================================
/// <summary>
/// 执行Sql语句
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="str_Sql">sql语句(比如:insert into tablename set name='北京'')</param>
public static void RunSql(string connString,string str_Sql)
{
Data.ExecuteNonQuery(connString,CommandType.Text,str_Sql);
}
/// <summary>
/// 插入记录
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="cmdType">sql语句类型</param>
/// <param name="str_Sql">sql语句</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
public static void Insert(string connString,string TableName,Hashtable ht)
{
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
// 作哈希表循环
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=Data.MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp; // 添加SqlParameter对象
i=i+1;
}
string str_Sql=GetInsertSqlbyHt(TableName,ht); // 获得插入sql语句
Data.ExecuteNonQuery(connString,CommandType.Text,str_Sql,Parms);
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="cmdType">sql语句类型</param>
/// <param name="str_Sql">sql语句</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
public static void Del(string connString,string TableName,string ht_Where,Hashtable ht)
{
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
// 作哈希表循环
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=Data.MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp; // 添加SqlParameter对象
i=i+1;
}
string str_Sql=GetDelSqlbyHt(TableName,ht_Where,ht); // 获得删除sql语句
Data.ExecuteNonQuery(connString,CommandType.Text,str_Sql,Parms);
}
/// <summary>
/// 修改记录
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="TableName">数据库表名</param>
/// <param name="str_Where">传递条件,比如Id=@Id</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
public static void Update(string connString,string TableName,string ht_Where, Hashtable ht)
{
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
// 作哈希表循环
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=Data.MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp; // 添加SqlParameter对象
i=i+1;
}
string str_Sql=GetUpdateSqlbyHt(TableName,ht_Where,ht); // 获得插入sql语句
Data.ExecuteNonQuery(connString,CommandType.Text,str_Sql,Parms);
}
/// <summary>
/// 获得数字字段最大值(注:当该表记录为空,返回0)
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="id">Key值字段名</param>
/// <param name="table_name">数据库名</param>
/// <returns>返回数字字段最大值</returns>
public static int GetMaxId(string connString,string id,string table_name)
{
string str_Sql="Select Max("+id+") from "+table_name;
int int_MaxId=0;
object obj=Data.ExecuteScalar(connString,CommandType.Text,str_Sql,null);
if (obj==System.DBNull.Value)
{
int_MaxId=0;
}
else
{
int_MaxId = Convert.ToInt32(obj);
}
return int_MaxId;
}
/// <summary>
/// 通过传递条件获得记录条数
/// </summary>
/// <param name="ht">表示层传递过来的条件字段参数</param>
/// <returns>返回记录条数</returns>
public static int GetRsCount(string connString,string Table,string ht_Where,Hashtable ht)
{
if (ht==null)
{
string str_Sql=GetPageListCountSqlbyHt(Table,ht_Where,null);
return (int)ExecuteScalar(connString,CommandType.Text,str_Sql,null);
}
else
{
string str_Sql=GetPageListCountSqlbyHt(Table,ht_Where,ht);
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
// 作哈希表循环
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=Data.MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp; // 添加SqlParameter对象
i=i+1;
}
return (int)ExecuteScalar(connString,CommandType.Text,str_Sql,Parms);
}
}
/// <summary>
/// 通过传递条件获得记录条数
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="str_Sql">Sql语句</param>
/// <returns>返回记录条数</returns>
public static int GetRsCount(string connString,string str_Sql)
{
return (int)ExecuteScalar(connString,CommandType.Text,str_Sql,null);
}
/// <summary>
/// 获得单个字段值
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="str_Sql">Sql语句,比如Select Name from Table where id=2</param>
/// <returns></returns>
public static string GetFiledValue(string connString,string str_Sql)
{
return ExecuteScalar(connString,CommandType.Text,str_Sql,null).ToString();
}
/// <summary>
/// 通过运行Sql语句获得IList数据源
/// </summary>
/// <param name="conn_Default">数据库连接</param>
/// <param name="int_PageSize">一页显示记录数</param>
/// <param name="int_CurrentPageIndex">当前页码</param>
/// <param name="str_Sql">Sql语句</param>
/// <param name="class_Name">实体类名</param>
/// <returns></returns>
public static IList RunSql(string conn_Default,int int_PageSize,int int_CurrentPageIndex,string str_Sql,string class_Name)
{
// ===获得数据库源,返回IList为数据源===
IList Ilst=new ArrayList();
// 当没有传递条件参数时作的操作
using (DataSet ds = ExecuteDataSet(int_PageSize,int_CurrentPageIndex,conn_Default, CommandType.Text, str_Sql, null))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
Ilst.Add(o_Instance);
}
}
return Ilst;
}
public static IList RunSql(string conn_Default,int int_PageSize,int int_CurrentPageIndex,string procName,SqlParameter[] prams,string class_Name)
{
// ===获得数据库源,返回IList为数据源===
IList Ilst=new ArrayList();
// 当没有传递条件参数时作的操作
using (DataSet ds = ExecuteDataSet(int_PageSize,int_CurrentPageIndex,conn_Default, CommandType.StoredProcedure, procName,prams))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
Ilst.Add(o_Instance);
}
}
return Ilst;
}
/// <summary>
/// 通过页大小,当前页数返回IList数据源
/// </summary>
/// <param name="int_PageSize">一页记录数</param>
/// <param name="int_CurrentPageIndex">当前页数</param>
/// <param name="Sql_Sel_Code">SQl语句</param>
/// <param name="ht">传递条件哈希表</param>
/// <param name="class_Name">实体类名</param>
/// <returns>表示层传递过来的条件字段参数</returns>
public static IList GetPageList(string conn_Default,int int_PageSize,int int_CurrentPageIndex,string Table,string ht_Where,string orderby,Hashtable ht,string class_Name)
{
// ===获得数据库源,返回IList为数据源===
IList Ilst=new ArrayList();
if (ht==null)
{
// 当没有传递条件参数时作的操作
string str_Sql=GetPageListSqlbyHt(Table,ht_Where,orderby,null,class_Name);
using (DataSet ds = ExecuteDataSet(int_PageSize,int_CurrentPageIndex,conn_Default, CommandType.Text, str_Sql, null))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
Ilst.Add(o_Instance);
}
}
}
else // 当没有传递条件参数时作的操作
{
// 处理传递过来的参数
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp;
i=i+1;
}
string str_Sql=GetPageListSqlbyHt(Table,ht_Where,orderby,ht,class_Name);
// 返回ILst
using (DataSet ds = ExecuteDataSet(int_PageSize,int_CurrentPageIndex,conn_Default, CommandType.Text, str_Sql, Parms))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
Ilst.Add(o_Instance);
}
}
}
return Ilst;
}
/// <summary>
/// ===通过页大小,当前页数返回IList数据源===
/// </summary>
/// <param name="int_PageSize">一页记录数</param>
/// <param name="int_CurrentPageIndex">当前页数</param>
/// <param name="Sql_Sel_Code">SQl语句</param>
/// <param name="ht">传递条件哈希表</param>
/// <param name="class_Name">实体类名</param>
/// <returns>表示层传递过来的条件字段参数</returns>
public static Object GetDetail(string conn_Default,string Table,string ht_Where,Hashtable ht,string class_Name)
{
// ===获得数据库源,返回IList为数据源===
IList Ilst=new ArrayList();
if (ht==null)
{
string str_Sql=GetPageListSqlbyHt(Table,ht_Where,null,null,class_Name);
// 当没有传递条件参数时作的操作
using (DataSet ds = ExecuteDataSet(conn_Default, CommandType.Text, str_Sql, null))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
return o_Instance;
}
}
}
else // 当没有传递条件参数时作的操作
{
// 处理传递过来的参数
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp;
i=i+1;
}
string str_Sql=GetPageListSqlbyHt(Table,ht_Where,null,ht,class_Name);
// 返回ILst
using (DataSet ds = ExecuteDataSet(conn_Default, CommandType.Text, str_Sql, Parms))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
return o_Instance;
}
}
}
return Ilst;
}
// ==============================================================
// ===========================内部调用函数============================
// ==============================================================
/// <summary>
/// 获得删除Sql语句
/// </summary>
/// <param name="Table">数据库表名</param>
/// <param name="ht_Where">传递条件,比如Id=@Id</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns>返回删除sql语句</returns>
public static string GetDelSqlbyHt(string Table,string ht_Where,Hashtable ht)
{
string str_Sql="";
int i=0;
int ht_Count=ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
if (i==0)
{
if (ht_Where.ToString().ToLower().IndexOf((myEnumerator.Key+"=@"+myEnumerator.Key).ToLower())==-1)
{
str_Sql=myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
else
{
if (ht_Where.ToString().ToLower().IndexOf(("@"+myEnumerator.Key+" ").ToLower())==-1)
{
str_Sql=str_Sql+","+myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
i=i+1;
}
if (ht_Where==null || ht_Where.Replace(" ","")=="") // 更新时候没有条件
{
str_Sql="Delete "+Table;
}
else
{
str_Sql="Delete "+Table+" where "+ht_Where;
}
return str_Sql;
}
/// <summary>
/// 获得插入Sql语句
/// </summary>
/// <param name="TableName">数据库表名</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns>返回插入Sql语句</returns>
public static string GetInsertSqlbyHt(string TableName, Hashtable ht)
{
string str_Sql="";
int i=0;
int ht_Count=ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
string before="";
string behide="";
while ( myEnumerator.MoveNext() )
{
if (i==0)
{
before="("+myEnumerator.Key;
}
else if (i+1==ht_Count)
{
before=before+","+myEnumerator.Key+")";
}
else
{
before=before+","+myEnumerator.Key;
}
i=i+1;
}
behide=" Values"+before.Replace(",",",@").Replace("(","(@");
str_Sql="Insert into "+TableName+before+behide;
return str_Sql;
}
/// <summary>
/// 获得记录数sql语句
/// </summary>
/// <param name="Table">数据库表</param>
/// <param name="ht_Where">条件</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns></returns>
public static string GetPageListCountSqlbyHt(string Table,string ht_Where,Hashtable ht)
{
string str_Sql="";
if (ht_Where=="" || ht_Where==null)
{
string str_Ht="";
if (ht!=null) // 用ht做条件
{
IDictionaryEnumerator et = ht.GetEnumerator();
int k=0;
while ( et.MoveNext() )
{
if (k==0)
{
str_Ht=" "+et.Key.ToString()+"=@"+et.Key.ToString();
}
else
{
str_Ht=str_Ht+" and "+et.Key.ToString()+"=@"+et.Key.ToString();
}
k=k+1;
}
}
if (str_Ht!="")
{
str_Sql="Select Count(*) From "+Table+" where "+str_Ht;
}
else
{
str_Sql="Select Count(*) From "+Table;
}
}
else
{
str_Sql="Select Count(*) From "+Table+" where "+ht_Where;
}
return str_Sql;
}
/// <summary>
/// 通过传递哈希表参数,获得更新Sql语句
/// </summary>
/// <param name="Table">数据库表名</param>
/// <param name="ht_Where">传递条件,比如Id=@Id</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns></returns>
public static string GetUpdateSqlbyHt(string Table,string ht_Where,Hashtable ht)
{
string str_Sql="";
int i=0;
int ht_Count=ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
if (i==0)
{
if (ht_Where.ToString().ToLower().IndexOf((myEnumerator.Key+"=@"+myEnumerator.Key).ToLower())==-1)
{
str_Sql=myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
else
{
if (ht_Where.ToString().ToLower().IndexOf(("@"+myEnumerator.Key+" ").ToLower())==-1)
{
str_Sql=str_Sql+","+myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
i=i+1;
}
if (ht_Where==null || ht_Where.Replace(" ","")=="") // 更新时候没有条件
{
str_Sql="update "+Table+" set "+str_Sql;
}
else
{
str_Sql="update "+Table+" set "+str_Sql+" where "+ht_Where;
}
str_Sql=str_Sql.Replace("set ,","set ").Replace("update ,","update ");
return str_Sql;
}
/// <summary>
/// 获得IList分页Sql语句
/// </summary>
/// <param name="Table">数据库表</param>
/// <param name="ht_Where">条件</param>
/// <param name="orderby">排序</param>
/// <param name="ht">表示层传递过来的条件字段参数</param>
/// <param name="class_Name">实体类名</param>
/// <returns></returns>
public static string GetPageListSqlbyHt(string Table,string ht_Where,string orderby,Hashtable ht,String class_Name)
{
string str_Sql="";
// 选择类型只能实现 Select * from table where a=@a and b=@b效果
// where 后面优先权,当ht_Where不为空或者不为null,条件应该是ht_Where参数,否则,用ht做循环
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
if (k==0)
{
str_Sql=myPropInfo.Name.ToString();
}
else
{
str_Sql=str_Sql+","+myPropInfo.Name.ToString();
}
}
if (ht_Where=="" || ht_Where==null)
{
string str_Ht="";
if (ht!=null) // 用ht做条件
{
IDictionaryEnumerator et = ht.GetEnumerator();
int k=0;
while ( et.MoveNext() )
{
if (k==0)
{
str_Ht=" "+et.Key.ToString()+"=@"+et.Key.ToString();
}
else
{
str_Ht=str_Ht+" and "+et.Key.ToString()+"=@"+et.Key.ToString();
}
k=k+1;
}
}
if (orderby=="" || orderby==null)
{
if (str_Ht!="")
{
str_Sql="Select "+str_Sql+" From "+Table+" where "+str_Ht;
}
else
{
str_Sql="Select "+str_Sql+" From "+Table;
}
}
else
{
if (str_Ht!="")
{
str_Sql="Select "+str_Sql+" From "+Table+" where "+str_Ht+" order by "+orderby;
}
else
{
str_Sql="Select "+str_Sql+" From "+Table;
}
}
}
else // 用ht_Where做条件
{
if (orderby=="" || orderby==null)
{
str_Sql="Select "+str_Sql+" From "+Table+" Where "+ht_Where;
}
else
{
str_Sql="Select "+str_Sql+" From "+Table+" where "+ht_Where+" order by "+orderby;
}
}
return str_Sql;
}
}
}
下面看看DLL层:
using System;
using System.Collections;
using ThreeLayer.Model.Sys;
namespace ThreeLayer.BLL.Sys
{
/// <summary>
/// Message 的摘要说明。
/// </summary>
public class Message
{
ThreeLayer.DAL.Sys.Message dal=new ThreeLayer.DAL.Sys.Message(); // 实例化信息维护数据类
// =============================================================
// ============================列表函数=============================
// =============================================================
/// <summary>
/// 获得信息维护单页列表
/// </summary>
/// <param name="int_PageSize">一页显示的记录数</param>
/// <param name="int_CurrentPageIndex">当前页码</param>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public IList GetMessages(int int_PageSize,int int_CurrentPageIndex,Hashtable ht)
{
return dal.GetMessages(int_PageSize,int_CurrentPageIndex,ht);
}
/// <summary>
/// 获得信息维护全部列表
/// </summary>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public IList GetMessages(Hashtable ht)
{
return dal.GetMessages(ht);
}
/// <summary>
/// 获得一个信息维护详细信息维护
/// </summary>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public MessageInfo GetMessageDetail(Hashtable ht)
{
return dal.GetMessageDetail(ht);
}
/// <summary>
/// 获得信息维护总记录数
/// </summary>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public int GetMessagesCount(Hashtable ht)
{
return dal.GetMessagesCount(ht);
}
// ==============================================================
// ============================数据操作函数===========================
// ==============================================================
/// <summary>
/// 增加记录时判断帐号是否重复
/// </summary>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public int GetAddMessagesCount(Hashtable ht)
{
return dal.GetAddMessagesCount(ht);
}
/// <summary>
/// 修改记录时判断帐号是否重复
/// </summary>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public int GetEditMessagesCount(Hashtable ht)
{
return dal.GetEditMessagesCount(ht);
}
/// <summary>
/// 增加记录
/// </summary>
/// <param name="ht">字段,字段值参数</param>
public void Insert(Hashtable ht)
{
dal.Insert(ht);
}
/// <summary>
/// 修改记录
/// </summary>
/// <param name="Message">字段,字段值参数</param>
public void Update(Hashtable ht)
{
dal.Update(ht);
}
}
}
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Reflection;
/// <summary>
//========高处不胜寒Asp.Net三层结构数据层公共类V1.1=========
//
// '''
// (0 0)
// +-----oOO----(_)-------------------+
// | |
// | |
// QQ:352623642 |
// | |
// | 类型:Web版 |
// | 适用数据库:Sql Sever |
// | 更新时间:2005-08-13 |
// | 技术支持网站:ASP.NET技术团队 |
// | |
// +------------------oOO-------------+
// |__|__|
// || ||
// ooO Ooo
//
//============================================================
/// </summary>
namespace ThreeLayer.DAL
{
public abstract class Data
{
// ===数据库连接串设置===
public static readonly string conn_Default = ConfigurationSettings.AppSettings["conn_Default"]; // 系统默认数据库连接串
// ==============================================================
// ========================数据库底层操作==============================
// ==============================================================
/// <summary>
/// 执行ExecuteNonQuery
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="cmdType">Sql语句类型</param>
/// <param name="cmdText">Sql语句</param>
/// <param name="cmdParms">Parm数组</param>
/// <returns>返回影响行数</returns>
public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
cmd.Connection=conn;
cmd.CommandText = cmdText;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
conn.Close();
return val;
}
}
/// <summary>
/// 返回一个SqlParameter实例
/// </summary>
/// <param name="ParamName">字段名</param>
/// <param name="stype">字段类型</param>
/// <param name="size">范围</param>
/// <param name="Value">赋值</param>
/// <returns>返回一个SqlParameter实例</returns>
public static SqlParameter MakeParam(string ParamName,System.Data.SqlDbType stype,int size,Object Value)
{
SqlParameter para=new SqlParameter(ParamName,Value);
para.SqlDbType=stype;
para.Size=size;
return para;
}
/// <summary>
/// 获得SqlParameter实例
/// </summary>
/// <param name="ParamName">字段名</param>
/// <param name="Value">赋值</param>
/// <returns>返回一个SqlParameter实例</returns>
public static SqlParameter MakeParam(string ParamName,string Value)
{
return new SqlParameter(ParamName, Value);
}
/// <summary>
/// 获得DateSet实例(获得单页记录)
/// </summary>
/// <param name="int_PageSize">一页显示的记录数</param>
/// <param name="int_CurrentPageIndex">当前页码</param>
/// <param name="connString">数据库连接串</param>
/// <param name="cmdType">Sql语句类型</param>
/// <param name="cmdText">Sql语句</param>
/// <param name="cmdParms">Parm数组</param>
/// <returns></returns>
public static DataSet ExecuteDataSet(int int_PageSize,int int_CurrentPageIndex,string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlConnection conn = new SqlConnection(connString);
try
{
conn.Open();
System.Data.SqlClient.SqlDataAdapter da=new SqlDataAdapter(cmdText,conn);
da.SelectCommand.CommandType=cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
da.SelectCommand.Parameters.Add(parm);
}
conn.Close();
DataSet ds=new DataSet();
if (int_PageSize==0 && int_CurrentPageIndex==0)
{
da.Fill(ds,"12news1234567890");
}
else
{
int int_Page=int_PageSize*(int_CurrentPageIndex-1);
if (int_Page<0)
{
int_Page=0;
}
da.Fill(ds,int_Page,int_PageSize,"12news1234567890");
}
return ds;
}
catch
{
conn.Close();
throw;
}
}
/// <summary>
/// 获得DateSet实例(获得全部记录)
/// </summary>
/// <param name="connString">数据库连接串</param>
/// <param name="cmdType">Sql语句类型</param>
/// <param name="cmdText">Sql语句</param>
/// <param name="cmdParms">Parm数组</param>
/// <returns></returns>
public static DataSet ExecuteDataSet(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlConnection conn = new SqlConnection(connString);
try
{
conn.Open();
System.Data.SqlClient.SqlDataAdapter da=new SqlDataAdapter(cmdText,conn);
da.SelectCommand.CommandType=cmdType;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
da.SelectCommand.Parameters.Add(parm);
}
conn.Close();
DataSet ds=new DataSet();
da.Fill(ds,"12news1234567890");
return ds;
}
catch
{
conn.Close();
throw;
}
}
/// <summary>
/// 执行ExecuteScalar
/// </summary>
/// <param name="connString">数据库连接串</param>
/// <param name="cmdType">Sql语句类型</param>
/// <param name="cmdText">Sql语句</param>
/// <param name="cmdParms">Parm数组</param>
/// <returns>返回第一行第一列记录值</returns>
public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText, params SqlParameter[] cmdParms)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (cmdParms != null)
{
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
conn.Close();
return val;
}
}
// ==============================================================
// ===================数据库操作:插入,修改,列表显示,以及获得详细记录=================
// ==============================================================
/// <summary>
/// 执行Sql语句
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="str_Sql">sql语句(比如:insert into tablename set name='北京'')</param>
public static void RunSql(string connString,string str_Sql)
{
Data.ExecuteNonQuery(connString,CommandType.Text,str_Sql);
}
/// <summary>
/// 插入记录
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="cmdType">sql语句类型</param>
/// <param name="str_Sql">sql语句</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
public static void Insert(string connString,string TableName,Hashtable ht)
{
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
// 作哈希表循环
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=Data.MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp; // 添加SqlParameter对象
i=i+1;
}
string str_Sql=GetInsertSqlbyHt(TableName,ht); // 获得插入sql语句
Data.ExecuteNonQuery(connString,CommandType.Text,str_Sql,Parms);
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="cmdType">sql语句类型</param>
/// <param name="str_Sql">sql语句</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
public static void Del(string connString,string TableName,string ht_Where,Hashtable ht)
{
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
// 作哈希表循环
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=Data.MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp; // 添加SqlParameter对象
i=i+1;
}
string str_Sql=GetDelSqlbyHt(TableName,ht_Where,ht); // 获得删除sql语句
Data.ExecuteNonQuery(connString,CommandType.Text,str_Sql,Parms);
}
/// <summary>
/// 修改记录
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="TableName">数据库表名</param>
/// <param name="str_Where">传递条件,比如Id=@Id</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
public static void Update(string connString,string TableName,string ht_Where, Hashtable ht)
{
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
// 作哈希表循环
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=Data.MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp; // 添加SqlParameter对象
i=i+1;
}
string str_Sql=GetUpdateSqlbyHt(TableName,ht_Where,ht); // 获得插入sql语句
Data.ExecuteNonQuery(connString,CommandType.Text,str_Sql,Parms);
}
/// <summary>
/// 获得数字字段最大值(注:当该表记录为空,返回0)
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="id">Key值字段名</param>
/// <param name="table_name">数据库名</param>
/// <returns>返回数字字段最大值</returns>
public static int GetMaxId(string connString,string id,string table_name)
{
string str_Sql="Select Max("+id+") from "+table_name;
int int_MaxId=0;
object obj=Data.ExecuteScalar(connString,CommandType.Text,str_Sql,null);
if (obj==System.DBNull.Value)
{
int_MaxId=0;
}
else
{
int_MaxId = Convert.ToInt32(obj);
}
return int_MaxId;
}
/// <summary>
/// 通过传递条件获得记录条数
/// </summary>
/// <param name="ht">表示层传递过来的条件字段参数</param>
/// <returns>返回记录条数</returns>
public static int GetRsCount(string connString,string Table,string ht_Where,Hashtable ht)
{
if (ht==null)
{
string str_Sql=GetPageListCountSqlbyHt(Table,ht_Where,null);
return (int)ExecuteScalar(connString,CommandType.Text,str_Sql,null);
}
else
{
string str_Sql=GetPageListCountSqlbyHt(Table,ht_Where,ht);
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
// 作哈希表循环
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=Data.MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp; // 添加SqlParameter对象
i=i+1;
}
return (int)ExecuteScalar(connString,CommandType.Text,str_Sql,Parms);
}
}
/// <summary>
/// 通过传递条件获得记录条数
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="str_Sql">Sql语句</param>
/// <returns>返回记录条数</returns>
public static int GetRsCount(string connString,string str_Sql)
{
return (int)ExecuteScalar(connString,CommandType.Text,str_Sql,null);
}
/// <summary>
/// 获得单个字段值
/// </summary>
/// <param name="connString">数据库连接</param>
/// <param name="str_Sql">Sql语句,比如Select Name from Table where id=2</param>
/// <returns></returns>
public static string GetFiledValue(string connString,string str_Sql)
{
return ExecuteScalar(connString,CommandType.Text,str_Sql,null).ToString();
}
/// <summary>
/// 通过运行Sql语句获得IList数据源
/// </summary>
/// <param name="conn_Default">数据库连接</param>
/// <param name="int_PageSize">一页显示记录数</param>
/// <param name="int_CurrentPageIndex">当前页码</param>
/// <param name="str_Sql">Sql语句</param>
/// <param name="class_Name">实体类名</param>
/// <returns></returns>
public static IList RunSql(string conn_Default,int int_PageSize,int int_CurrentPageIndex,string str_Sql,string class_Name)
{
// ===获得数据库源,返回IList为数据源===
IList Ilst=new ArrayList();
// 当没有传递条件参数时作的操作
using (DataSet ds = ExecuteDataSet(int_PageSize,int_CurrentPageIndex,conn_Default, CommandType.Text, str_Sql, null))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
Ilst.Add(o_Instance);
}
}
return Ilst;
}
public static IList RunSql(string conn_Default,int int_PageSize,int int_CurrentPageIndex,string procName,SqlParameter[] prams,string class_Name)
{
// ===获得数据库源,返回IList为数据源===
IList Ilst=new ArrayList();
// 当没有传递条件参数时作的操作
using (DataSet ds = ExecuteDataSet(int_PageSize,int_CurrentPageIndex,conn_Default, CommandType.StoredProcedure, procName,prams))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
Ilst.Add(o_Instance);
}
}
return Ilst;
}
/// <summary>
/// 通过页大小,当前页数返回IList数据源
/// </summary>
/// <param name="int_PageSize">一页记录数</param>
/// <param name="int_CurrentPageIndex">当前页数</param>
/// <param name="Sql_Sel_Code">SQl语句</param>
/// <param name="ht">传递条件哈希表</param>
/// <param name="class_Name">实体类名</param>
/// <returns>表示层传递过来的条件字段参数</returns>
public static IList GetPageList(string conn_Default,int int_PageSize,int int_CurrentPageIndex,string Table,string ht_Where,string orderby,Hashtable ht,string class_Name)
{
// ===获得数据库源,返回IList为数据源===
IList Ilst=new ArrayList();
if (ht==null)
{
// 当没有传递条件参数时作的操作
string str_Sql=GetPageListSqlbyHt(Table,ht_Where,orderby,null,class_Name);
using (DataSet ds = ExecuteDataSet(int_PageSize,int_CurrentPageIndex,conn_Default, CommandType.Text, str_Sql, null))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
Ilst.Add(o_Instance);
}
}
}
else // 当没有传递条件参数时作的操作
{
// 处理传递过来的参数
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp;
i=i+1;
}
string str_Sql=GetPageListSqlbyHt(Table,ht_Where,orderby,ht,class_Name);
// 返回ILst
using (DataSet ds = ExecuteDataSet(int_PageSize,int_CurrentPageIndex,conn_Default, CommandType.Text, str_Sql, Parms))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
Ilst.Add(o_Instance);
}
}
}
return Ilst;
}
/// <summary>
/// ===通过页大小,当前页数返回IList数据源===
/// </summary>
/// <param name="int_PageSize">一页记录数</param>
/// <param name="int_CurrentPageIndex">当前页数</param>
/// <param name="Sql_Sel_Code">SQl语句</param>
/// <param name="ht">传递条件哈希表</param>
/// <param name="class_Name">实体类名</param>
/// <returns>表示层传递过来的条件字段参数</returns>
public static Object GetDetail(string conn_Default,string Table,string ht_Where,Hashtable ht,string class_Name)
{
// ===获得数据库源,返回IList为数据源===
IList Ilst=new ArrayList();
if (ht==null)
{
string str_Sql=GetPageListSqlbyHt(Table,ht_Where,null,null,class_Name);
// 当没有传递条件参数时作的操作
using (DataSet ds = ExecuteDataSet(conn_Default, CommandType.Text, str_Sql, null))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
return o_Instance;
}
}
}
else // 当没有传递条件参数时作的操作
{
// 处理传递过来的参数
SqlParameter[] Parms=new SqlParameter[ht.Count];
IDictionaryEnumerator et = ht.GetEnumerator();
int i=0;
while ( et.MoveNext() )
{
System.Data.SqlClient.SqlParameter sp=MakeParam("@"+et.Key.ToString(),et.Value.ToString());
Parms[i]=sp;
i=i+1;
}
string str_Sql=GetPageListSqlbyHt(Table,ht_Where,null,ht,class_Name);
// 返回ILst
using (DataSet ds = ExecuteDataSet(conn_Default, CommandType.Text, str_Sql, Parms))
{
DataTable dt=ds.Tables[0];
for (int j=0;j<dt.Rows.Count;j++)
{
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
Object filed_Val=dt.Rows[j][myPropInfo.Name];
switch (myPropInfo.PropertyType.ToString())
{
case "System.Int32":
myPropInfo.SetValue(o_Instance,(int)filed_Val,null);
break;
case "System.String":
myPropInfo.SetValue(o_Instance,filed_Val.ToString(),null);
break;
case "System.DateTime":
myPropInfo.SetValue(o_Instance,Convert.ToDateTime(filed_Val.ToString()),null);
break;
}
}
// 把一行类记录赋值给ILst对象
return o_Instance;
}
}
}
return Ilst;
}
// ==============================================================
// ===========================内部调用函数============================
// ==============================================================
/// <summary>
/// 获得删除Sql语句
/// </summary>
/// <param name="Table">数据库表名</param>
/// <param name="ht_Where">传递条件,比如Id=@Id</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns>返回删除sql语句</returns>
public static string GetDelSqlbyHt(string Table,string ht_Where,Hashtable ht)
{
string str_Sql="";
int i=0;
int ht_Count=ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
if (i==0)
{
if (ht_Where.ToString().ToLower().IndexOf((myEnumerator.Key+"=@"+myEnumerator.Key).ToLower())==-1)
{
str_Sql=myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
else
{
if (ht_Where.ToString().ToLower().IndexOf(("@"+myEnumerator.Key+" ").ToLower())==-1)
{
str_Sql=str_Sql+","+myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
i=i+1;
}
if (ht_Where==null || ht_Where.Replace(" ","")=="") // 更新时候没有条件
{
str_Sql="Delete "+Table;
}
else
{
str_Sql="Delete "+Table+" where "+ht_Where;
}
return str_Sql;
}
/// <summary>
/// 获得插入Sql语句
/// </summary>
/// <param name="TableName">数据库表名</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns>返回插入Sql语句</returns>
public static string GetInsertSqlbyHt(string TableName, Hashtable ht)
{
string str_Sql="";
int i=0;
int ht_Count=ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
string before="";
string behide="";
while ( myEnumerator.MoveNext() )
{
if (i==0)
{
before="("+myEnumerator.Key;
}
else if (i+1==ht_Count)
{
before=before+","+myEnumerator.Key+")";
}
else
{
before=before+","+myEnumerator.Key;
}
i=i+1;
}
behide=" Values"+before.Replace(",",",@").Replace("(","(@");
str_Sql="Insert into "+TableName+before+behide;
return str_Sql;
}
/// <summary>
/// 获得记录数sql语句
/// </summary>
/// <param name="Table">数据库表</param>
/// <param name="ht_Where">条件</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns></returns>
public static string GetPageListCountSqlbyHt(string Table,string ht_Where,Hashtable ht)
{
string str_Sql="";
if (ht_Where=="" || ht_Where==null)
{
string str_Ht="";
if (ht!=null) // 用ht做条件
{
IDictionaryEnumerator et = ht.GetEnumerator();
int k=0;
while ( et.MoveNext() )
{
if (k==0)
{
str_Ht=" "+et.Key.ToString()+"=@"+et.Key.ToString();
}
else
{
str_Ht=str_Ht+" and "+et.Key.ToString()+"=@"+et.Key.ToString();
}
k=k+1;
}
}
if (str_Ht!="")
{
str_Sql="Select Count(*) From "+Table+" where "+str_Ht;
}
else
{
str_Sql="Select Count(*) From "+Table;
}
}
else
{
str_Sql="Select Count(*) From "+Table+" where "+ht_Where;
}
return str_Sql;
}
/// <summary>
/// 通过传递哈希表参数,获得更新Sql语句
/// </summary>
/// <param name="Table">数据库表名</param>
/// <param name="ht_Where">传递条件,比如Id=@Id</param>
/// <param name="ht">表示层传递过来的哈希表对象</param>
/// <returns></returns>
public static string GetUpdateSqlbyHt(string Table,string ht_Where,Hashtable ht)
{
string str_Sql="";
int i=0;
int ht_Count=ht.Count; // 哈希表个数
IDictionaryEnumerator myEnumerator = ht.GetEnumerator();
while ( myEnumerator.MoveNext() )
{
if (i==0)
{
if (ht_Where.ToString().ToLower().IndexOf((myEnumerator.Key+"=@"+myEnumerator.Key).ToLower())==-1)
{
str_Sql=myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
else
{
if (ht_Where.ToString().ToLower().IndexOf(("@"+myEnumerator.Key+" ").ToLower())==-1)
{
str_Sql=str_Sql+","+myEnumerator.Key+"=@"+myEnumerator.Key;
}
}
i=i+1;
}
if (ht_Where==null || ht_Where.Replace(" ","")=="") // 更新时候没有条件
{
str_Sql="update "+Table+" set "+str_Sql;
}
else
{
str_Sql="update "+Table+" set "+str_Sql+" where "+ht_Where;
}
str_Sql=str_Sql.Replace("set ,","set ").Replace("update ,","update ");
return str_Sql;
}
/// <summary>
/// 获得IList分页Sql语句
/// </summary>
/// <param name="Table">数据库表</param>
/// <param name="ht_Where">条件</param>
/// <param name="orderby">排序</param>
/// <param name="ht">表示层传递过来的条件字段参数</param>
/// <param name="class_Name">实体类名</param>
/// <returns></returns>
public static string GetPageListSqlbyHt(string Table,string ht_Where,string orderby,Hashtable ht,String class_Name)
{
string str_Sql="";
// 选择类型只能实现 Select * from table where a=@a and b=@b效果
// where 后面优先权,当ht_Where不为空或者不为null,条件应该是ht_Where参数,否则,用ht做循环
Type myType =Type.GetType(class_Name);// 获得“类”类型
Object o_Instance=System.Activator.CreateInstance(myType); // 实例化类
// 获得类的所有属性数组
PropertyInfo[] myPropertyInfo1 = myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
// 循环属性数组,并给数组属性赋值
for(int k=0;k<myPropertyInfo1.Length;k++)
{
PropertyInfo myPropInfo = (PropertyInfo)myPropertyInfo1[k];
if (k==0)
{
str_Sql=myPropInfo.Name.ToString();
}
else
{
str_Sql=str_Sql+","+myPropInfo.Name.ToString();
}
}
if (ht_Where=="" || ht_Where==null)
{
string str_Ht="";
if (ht!=null) // 用ht做条件
{
IDictionaryEnumerator et = ht.GetEnumerator();
int k=0;
while ( et.MoveNext() )
{
if (k==0)
{
str_Ht=" "+et.Key.ToString()+"=@"+et.Key.ToString();
}
else
{
str_Ht=str_Ht+" and "+et.Key.ToString()+"=@"+et.Key.ToString();
}
k=k+1;
}
}
if (orderby=="" || orderby==null)
{
if (str_Ht!="")
{
str_Sql="Select "+str_Sql+" From "+Table+" where "+str_Ht;
}
else
{
str_Sql="Select "+str_Sql+" From "+Table;
}
}
else
{
if (str_Ht!="")
{
str_Sql="Select "+str_Sql+" From "+Table+" where "+str_Ht+" order by "+orderby;
}
else
{
str_Sql="Select "+str_Sql+" From "+Table;
}
}
}
else // 用ht_Where做条件
{
if (orderby=="" || orderby==null)
{
str_Sql="Select "+str_Sql+" From "+Table+" Where "+ht_Where;
}
else
{
str_Sql="Select "+str_Sql+" From "+Table+" where "+ht_Where+" order by "+orderby;
}
}
return str_Sql;
}
}
}
下面看看DLL层:
using System;
using System.Collections;
using ThreeLayer.Model.Sys;
namespace ThreeLayer.BLL.Sys
{
/// <summary>
/// Message 的摘要说明。
/// </summary>
public class Message
{
ThreeLayer.DAL.Sys.Message dal=new ThreeLayer.DAL.Sys.Message(); // 实例化信息维护数据类
// =============================================================
// ============================列表函数=============================
// =============================================================
/// <summary>
/// 获得信息维护单页列表
/// </summary>
/// <param name="int_PageSize">一页显示的记录数</param>
/// <param name="int_CurrentPageIndex">当前页码</param>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public IList GetMessages(int int_PageSize,int int_CurrentPageIndex,Hashtable ht)
{
return dal.GetMessages(int_PageSize,int_CurrentPageIndex,ht);
}
/// <summary>
/// 获得信息维护全部列表
/// </summary>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public IList GetMessages(Hashtable ht)
{
return dal.GetMessages(ht);
}
/// <summary>
/// 获得一个信息维护详细信息维护
/// </summary>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public MessageInfo GetMessageDetail(Hashtable ht)
{
return dal.GetMessageDetail(ht);
}
/// <summary>
/// 获得信息维护总记录数
/// </summary>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public int GetMessagesCount(Hashtable ht)
{
return dal.GetMessagesCount(ht);
}
// ==============================================================
// ============================数据操作函数===========================
// ==============================================================
/// <summary>
/// 增加记录时判断帐号是否重复
/// </summary>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public int GetAddMessagesCount(Hashtable ht)
{
return dal.GetAddMessagesCount(ht);
}
/// <summary>
/// 修改记录时判断帐号是否重复
/// </summary>
/// <param name="ht">字段,字段值参数</param>
/// <returns></returns>
public int GetEditMessagesCount(Hashtable ht)
{
return dal.GetEditMessagesCount(ht);
}
/// <summary>
/// 增加记录
/// </summary>
/// <param name="ht">字段,字段值参数</param>
public void Insert(Hashtable ht)
{
dal.Insert(ht);
}
/// <summary>
/// 修改记录
/// </summary>
/// <param name="Message">字段,字段值参数</param>
public void Update(Hashtable ht)
{
dal.Update(ht);
}
}
}
好了,到这里应该对三层架构有个清晰的认识了吧。