恩,之前已经展示了一个简单粗暴的 DBHelper 类。
该类不做任何检查就直接操作数据库了。
DBHelper最后出现了直接操作数据库实体对象(SqlPojo)的方法,明显,这需要SqlPojo的支持。
这里描述一下SqlPojo需要对此提供的支持:
- 自动生成SQL语句,增删改,查的话,暂不考虑
- 直接自查询结果中加载属性,明显,一个SqlPojo实例对应一个DataRow
啊,C#的注释没太搞清楚,谁能告诉我怎么让注释换行?
最好给个C#注释规则给我参考下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Collections;
using System.Data;
/// <summary>
/// <br>SqlPojo -- 对应数据库的实体</br>
/// <br>模仿 Java 里 Hibernate JavaBean 而制作的虚基类</br>
/// </summary>
public abstract class SqlPojo
{
public SqlPojo()
{
//
// TODO: 在此处添加构造函数逻辑
//
this.AddTime = DateTime.Now;
}
private const string Delimiter = ", ";
private const string SingleTemplate = "Select * From [{0}] Where ({1})";
private const string InsertTemplate = "Insert Into [{0}] ({1}) Values ({2})";
private const string DeleteTemplate = "Delete From [{0}] Where ({1})";
private const string UpdateTemplate = "Update [{0}] Set {1} Where ({2})";
private const string FieldTemplate = "[{0}]";
private const string ValueTemplate = "'{0}'";
private const string SetNullTemplate = "[{0}] = NULL";
private const string SetValueTemplate = "[{0}] = '{1}'";
protected const string KeyTemplate = "[{0}] = '{1}'";
private DateTime _AddTime;
private DateTime _UpdTime;
/// <summary>
/// <br>返回此数据库实体进入数据库时的时间</br>
/// </summary>
public virtual DateTime AddTime
{
get { return _AddTime; }
set { _AddTime = value; }
}
/// <summary>
/// <br>返回此数据库实体的最近一次更新时间</br>
/// </summary>
public virtual DateTime UpdTime
{
get { return _UpdTime; }
set { _UpdTime = value; }
}
/// <summary>
/// <br>返回此类实体对于数据库的主键限定语句</br>
/// <br>例如:"Uid = this.Uid"</br>
/// <example>"Oid = this.Oid AND Gid = this.Gid"</example>
/// </summary>
protected abstract string GetKeyCondition();
/// <summary>
/// <br>返回此类实体所属的数据库表</br>
/// <example>"User.Login"</example>
/// </summary>
protected abstract string GetTableName();
/// <summary>
/// <br>返回此实体的默认状态值</br>
/// <br>已放弃使用,因为并不是所有表都需要StatusCode这一列</br>
/// <example value = "1500">1500</example>
/// </summary>
protected int GetDefaultST() { return 0; }
/// <summary>
/// <br>返回此实体所对应的 Insert 语句.</br>
/// <br>注意,添加实体的时候应设定 AddTime 为 Now</br>
/// </summary>
public string ToInsertString()
{
//this._UpdTime = DateTime.Now;
string fieldstr = "";
string valuestr = "";
bool first = true;
foreach (PropertyInfo property in this.GetType().GetProperties())
{
// this.InsertString 也会出现在 this.GetType().GetProperties() 中,从而导致 堆栈溢出,但它是只读的.
// 剔除 只读 的属性
if (property.CanWrite == false) { continue; }
// 剔除 空值 属性 和 零值属性
if (Constants.IsNull(property.GetValue(this))) { continue; }
//
if (first != true)
{
fieldstr += SqlPojo.Delimiter;
valuestr += SqlPojo.Delimiter;
}
fieldstr += string.Format(SqlPojo.FieldTemplate, property.Name);
valuestr += string.Format(SqlPojo.ValueTemplate, property.GetValue(this).ToString());
first = false;
}
return string.Format(SqlPojo.InsertTemplate, this.GetTableName(), fieldstr, valuestr);
}
/// <summary>
/// <br>返回此实体对应的 Delete 语句</br>
/// <br>请确保此时此实体的 GetKeyCondition 可用</br>
/// </summary>
public string ToDeleteString()
{
return string.Format(SqlPojo.DeleteTemplate, this.GetTableName(), this.GetKeyCondition());
}
/// <summary>
/// <br>返回此实体对应的 Update 语句</br>
/// <br>请确保此时此实体的 GetKeyCondition 可用</br>
/// <br>注意,更新的时候将 UpdateTime 设定为 Now</br>
/// </summary>
public string ToUpdateString()
{
//this._UpdTime = DateTime.Now;
string fieldstr = "";
bool first = true;
foreach (PropertyInfo property in this.GetType().GetProperties())
{
// 剔除 只读 的属性
if (property.CanWrite == false) { continue; }
if (first != true) { fieldstr += SqlPojo.Delimiter; }
if (property.GetValue(this) == null)
{
fieldstr += string.Format(SqlPojo.SetNullTemplate, property.Name);
}
else
{
fieldstr += string.Format(SqlPojo.SetValueTemplate, property.Name, property.GetValue(this));
}
first = false;
}
return string.Format(SqlPojo.UpdateTemplate, this.GetTableName(), fieldstr, this.GetKeyCondition());
}
/// <summary>
/// 返回此以 GetKeyCondition() 为条件的查询语句以获取单条记录
/// </summary>
/// <returns></returns>
public string ToSingleString()
{
return string.Format(SqlPojo.SingleTemplate, this.GetTableName(), this.GetKeyCondition());
}
/// <summary>
/// <br>从 Hashtable 中加载属性到实体中</br>
/// </summary>
/// <param name="param">哈希集合</param>
public void LoadFromHashtable(Hashtable param)
{
foreach (PropertyInfo property in this.GetType().GetProperties())
{
// 剔除 只读 的属性
if (property.CanWrite == false) { continue; }
if (param.ContainsKey(property.Name) && param[property.Name] != null)
{
try
{
Type propertyType = property.PropertyType;
string s = param[property.Name].ToString();
if (propertyType == typeof(string))
{
property.SetValue(this, s);
}
else if (propertyType == typeof(long))
{
property.SetValue(this, long.Parse(s));
}
else if (propertyType == typeof(int))
{
property.SetValue(this, int.Parse(s));
}
else if (propertyType == typeof(bool))
{
property.SetValue(this, bool.Parse(s));
}
else if (propertyType == typeof(DateTime))
{
property.SetValue(this, DateTime.Parse(s));
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
public void LoadFromDataRow(DataRow dr)
{
foreach (PropertyInfo property in this.GetType().GetProperties())
{
if (property.CanWrite == false) { continue; }
if(dr.Table.Columns.Contains(property.Name))
{
if(dr[property.Name] is System.DBNull)
{
property.SetValue(this, null);
}
else
{
property.SetValue(this, dr[property.Name]);
}
}
}
}
public override string ToString()
{
Type tp = this.GetType();
return this.GetType().Name;
}
}
啊,发现有些注释是旧的,额,应该没关系,大家能理解就行。