您的位置:首页 > 数据库

反射生成T-SQL语句,欢迎大家批评指正,有待改进

2011-08-24 21:24 429 查看
/// <summary>

/// 生成SQL语句

/// </summary>

/// <typeparam name="T"></typeparam>

public static class TSQLHelper<T> where T : new()

{

static Dictionary<string, Dictionary<CRUD, string>> tableCRUD = new Dictionary<string, Dictionary<CRUD, string>>();

public static string GetInsertStatement(T entity)

{

return GetCRUDStatement(entity)[CRUD.insert];

}

public static string GetUpdateStatement(T entity)

{

return GetCRUDStatement(entity)[CRUD.update];

}

public static string GetDeleteStatement(T entity)

{

return GetCRUDStatement(entity)[CRUD.delete];

}

public static string GetSelectStatement(T entity)

{

return GetCRUDStatement(entity)[CRUD.select];

}

public static T GetOneByWhere(string where)

{

DataTable dtb = DBHelper.GetDataTable("SELECT * FROM " + typeof(T).Name + " WHERE " + where);

T entity = new T();

Type t = entity.GetType();

if (dtb.Rows.Count > 0)

{

foreach (DataColumn item in dtb.Columns)

{

MemberInfo[] ms = t.GetMembers(BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public);

t.InvokeMember(item.ColumnName, BindingFlags.SetProperty | BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public , null, entity, new object[] { dtb.Rows[0][item.ColumnName] });

}

return entity;

}

return default(T);

}

static private Dictionary<CRUD, string> GetCRUDStatement(T entity)

{

//if (!tableCRUD.ContainsKey(t.Name))

//{

string insert = "INSERT INTO {0}(";

string value = " VALUES(";

string update = "UPDATE {0} SET ";

string where = " WHERE ";

string delete = "DELETE FROM {0} ";

string select = "SELECT * FROM {0} ";

Type t = typeof(T);

MemberInfo[] fis = t.GetMembers(BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

var result = from fi in fis where fi.MemberType == MemberTypes.Property && fi.DeclaringType.Name == t.Name select fi;

insert = string.Format(insert, t.Name);

update = string.Format(update, t.Name);

delete = string.Format(delete, t.Name);

select = string.Format(select, t.Name);

object obj = null;

Type fieldType = null;

foreach (var item in result)

{

obj = t.InvokeMember(item.Name, BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, null, entity, null);

if (obj == null)

continue;

insert = string.Concat(new object[] { insert, item.Name, "," });

fieldType = obj.GetType();

object[] pk = item.GetCustomAttributes(typeof(PrimaryKey), false);

if (fieldType.FullName == "System.Int16" || fieldType.FullName == "System.Int32" | fieldType.FullName == "System.Int64" | fieldType.FullName == "System.Single" || fieldType.FullName == "System.Double" || fieldType.FullName == "System.Boolean")

{

value = string.Concat(new object[] { value, obj, "," });

if (pk != null)

if (pk.Length > 0)

{

if (Convert.ToBoolean(pk[0].GetType().InvokeMember("IsPrimaryKey", BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, null, pk[0], null)) == true)

where = string.Concat(where, item.Name, "=", obj, " and ");

}

else

{

update = string.Concat(new object[] { update, item.Name, "=", obj, "," });

}

}

else

{

value = string.Concat(new object[] { value, "'", obj, "'", "," });

if (pk != null)

if (pk.Length > 0)

{

if (Convert.ToBoolean(pk[0].GetType().InvokeMember("IsPrimaryKey", BindingFlags.GetProperty | BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance, null, pk[0], null)) == true)

where = string.Concat(where, item.Name, "='", obj, "' and ");

}

else

{

update = string.Concat(new object[] { update, item.Name, "=", obj, "," });

}

}

}

insert = insert.Remove(insert.Length - 1);

value = value.Remove(value.Length - 1);

insert = string.Concat(new object[] { insert, ")", value, ")" });

update = update.Remove(update.Length - 1);

where = where.Remove(where.Length - 4);

update = string.Concat(update, where);

delete = string.Concat(delete, where);

select = string.Concat(select, where);

Dictionary<CRUD, string> curd = new Dictionary<CRUD, string>();

curd.Add(CRUD.insert, insert);

curd.Add(CRUD.delete, delete);

curd.Add(CRUD.update, update);

curd.Add(CRUD.select, select);

//tableCRUD.Add(t.Name, curd);

return curd;

//}

//return tableCRUD[t.Name];

}

}

enum CRUD

{

insert,

update,

delete,

select

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: