本帖最后由 yimi00 于 2013-10-13 16:12 编辑
三石先生实现了 No JavaScript,No CSS,No UpdatePanel,No ViewState,No WebServices
我看看能不能实现NO SQL(目前已经有很多持久层的框架了,而且都很成熟,本文只是作为简单的想法,希望用简单的方法实现持久层,本人很菜鸟,所以想法很简单)
一些思路
通过VS插件自动化封装数据库表实体类,
实体类实例化后,直接提交到封装类中,数据库直接识别实体类封装对应的SQL语句
使用过程中,只使用到一 个model类和sqlBase类,sqlBase类代码很少,便于扩展。
下面贴出代码,希望大家参与探讨,在博客中有详细的过程讲解,大家可以参考一下
http://blog.csdn.net/gbk8290/article/details/12679015
如操作model_order实体(自动化实体类生成工具我已经开发出来,需要的同志留言,可以全自动生成模型类)
model_order order=new model_order();
order.xxx="";
......
sqlBase sql=new sqlBase();
增加:sql.ExecuteInsert(order);
修改:sql.ExecuteUpdate(order,"id=5");
删除:sql.ExecuteDelete(order);
查询:sql.ExecuteSelect(order);
public class SqlBase
{
/// <summary>
/// 根据实体模型对象自动封装Update的语句并执行,返回受影响行数
/// </summary>
/// <param name="obj">表模型对象</param>
/// <param name="args">where后面的条件,格式为:id=15</param>
/// <returns></returns>
public string ExecuteUpdate(object obj, params string[] args)
{
string condetion = "";
for (int i = 0; i < args.Length; i++)
{
condetion += args + " and ";
}
condetion = condetion.Substring(0, condetion.Length - 6);
Type t = obj.GetType();
string[] arg = t.Name.Split('_');
string TableName = arg[1];
string vas = "";
string sql = "update {0} set {1} where {2}";
PropertyInfo[] pinfo = t.GetProperties();
foreach (PropertyInfo p in pinfo)
{
if (p.GetValue(obj, null) != null)
{
vas += p.Name + "=" + "'" + p.GetValue(obj).ToString() + "',";
}
}
vas = vas.Substring(0, vas.Length - 1);
sql = string.Format(sql, TableName, vas, condetion);
///执行SQL语句,返回影响数
///在这里可以直接调用数据库操作类进行SQL操作,根据操作结果修改返回值类型,这里返回sql字符串仅仅作为演示用
return sql;
}
/// <summary>
/// obj为需要操作的数据库模型类,自动封装
/// </summary>
/// <param name="obj">为需要操作的数据库模型类</param>
/// <returns></returns>
public string ExecuteInsert(object obj)
{
Type t = obj.GetType();
string[] arg = t.Name.Split('_');
string TableName = arg[1];
string coluns = "";
string vas = "";
string sql = "insert into {0}({1}) values ({2})";
PropertyInfo[] pinfo = t.GetProperties();
foreach (PropertyInfo p in pinfo)
{
if (p.GetValue(obj, null) != null)
{
coluns += p.Name + ",";
vas += "'" + p.GetValue(obj).ToString() + "',";
}
}
coluns = coluns.Substring(0, coluns.Length - 1);
vas = vas.Substring(0, vas.Length - 1);
///取得已经封装好的SQL语句
sql = string.Format(sql, TableName, coluns, vas);
return sql;
}
/// <summary>
/// 对传递的实体类进行删除操作,实体类中要对筛选的列进行赋值
/// </summary>
/// <param name="obj">需要操作的实体类</param>
/// <returns></returns>
public string ExecuteDel(object obj)
{
Type t = obj.GetType();
string[] arg = t.Name.Split('_');
string TableName = arg[1];
string condetion = "";
string sql = "delete from {0} where {1}";
PropertyInfo[] pinfo = t.GetProperties();
foreach (PropertyInfo p in pinfo)
{
if (p.GetValue(obj, null) != null)
{
condetion += p.Name + "='" + p.GetValue(obj).ToString()+"' and ";
}
}
condetion = condetion.Substring(0,condetion.Length-5);
sql = string.Format(sql, TableName, condetion);
///返回受影响行数
return sql;
}
/// <summary>
/// 对传递的实体类进行查询,查询的条件直接在实体类中进行赋值,目前只返回全部列,目前只支持一般条件查询,like,between之类的查询需要自己扩展
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public string ExecuteSelect(object obj)
{
Type t = obj.GetType();
string[] arg = t.Name.Split('_');
string TableName = arg[1];
string condetion = "";
string sql = "select from {0} where {1}";
PropertyInfo[] pinfo = t.GetProperties();
foreach (PropertyInfo p in pinfo)
{
if (p.GetValue(obj, null) != null)
{
condetion += p.Name + "='" + p.GetValue(obj).ToString() + "' and ";
}
}
///不设置条件返回所有行
if (condetion == "")
{
sql =string.Format("select from {0}",TableName);
}
else {
condetion = condetion.Substring(0, condetion.Length - 5);
sql = string.Format(sql, TableName,condetion);
}
return sql;
}
}
|