实体类基类:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks; namespace Common
{
/// <summary>
/// 实体类基类
/// </summary>
[Serializable]
public abstract class EntityBase
{
/// <summary>
/// 获取主键
/// </summary>
/// <returns></returns>
public abstract string GetPrimaryKey();
/// <summary>
/// 获取INSERT语句
/// </summary>
/// <returns></returns>
public string GetInsertSql()
{
try
{
Type t = this.GetType();
string tableName = t.Name,pKey=this.GetPrimaryKey(),fields=string.Empty,values=string.Empty,temp=null;
foreach (PropertyInfo pi in t.GetProperties())
{
if (!pi.CanWrite) continue;
if (pi.Name.Equals(pKey))
{
continue;
}
temp = GetByTypeStr(pi);
fields += pi.Name + ",";
values += temp + ",";
}
return string.Format("Insert into {0}({1}) Values({2})", tableName, fields.TrimEnd(','), values.TrimEnd(','));
}
catch
{
throw;
}
}
/// <summary>
/// 获取UPDATE语句
/// </summary>
/// <returns></returns>
public string GetUpdateSql()
{
try
{
Type t = this.GetType();
PropertyInfo[] pInfos = t.GetProperties();
string tableName = t.Name, pKey = this.GetPrimaryKey(), str_fields=string.Empty;
int keyIndex = -;
for (int i = ; i < pInfos.Length; i++)
{
if (pInfos[i].Name.Equals(this.GetPrimaryKey()))
{
keyIndex = i;
continue;
}
str_fields += pInfos[i].Name + " = " + GetByTypeStr(pInfos[i]) + ",";
}
return string.Format("Update {0} Set {1} Where {2} = {3}", tableName, str_fields.TrimEnd(','),this.GetPrimaryKey(), GetByTypeStr(pInfos[keyIndex]));
}
catch
{
throw;
}
}
/// <summary>
/// 根据数据类型反射字段值
/// </summary>
/// <param name="pInfo">公共属性</param>
/// <returns></returns>
private string GetByTypeStr(PropertyInfo pInfo)
{
try
{
string result_str = string.Empty;
Type t = pInfo.PropertyType;
object obj = pInfo.GetValue(this, null);
bool valueNull = StringUtil.isNullOrBlank(obj);
if (t == typeof(string))
{
result_str = valueNull ? "null" : "'" + obj.ToString().Replace("--","") + "'";
}
else if (t == typeof(System.Decimal) || t == typeof(System.Int16) || t == typeof(System.Int32) || t == typeof(System.Int64))
{
result_str = t.Name == "Nullable`1"&& valueNull ? "null" : obj.ToString();
//if ()
//{ //}
//else
//{
// result_str = valueNull ? "0" : obj.ToString();
//}
}
else if(t==typeof(DateTime)||t.Name== "Nullable`1")
{
if (valueNull||DateTime.MinValue.Equals(obj)|| t.Name == "Nullable`1")
{
result_str = "null";
}
else
{
result_str = "'"+obj.ToString().Replace("年", "-").Replace("月", "-").Replace("日", "")+"'";
}
}
return result_str;
}
catch
{
throw;
}
}
}
}

实体类:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common; namespace Model
{
public class MainModel:EntityBase
{
public decimal id { get; set; }
public string title { get; set; }
public string contents { get; set; }
public string type { get; set; }
public DateTime? date { get; set; }
public string people { get; set; }
public string picurl { get; set; }
/// <summary>
/// 设置主键
/// </summary>
/// <returns></returns>
public override string GetPrimaryKey()
{
return "id";
}
}
}

调用:

             Model.MainModel model = new Model.MainModel();
model.title = context.Request.Form["txtTitle"];
model.people = context.Request.Form["txtName"];
model.contents = context.Request.Form["txtContent"];
string resSql = model.GetInsertSql();
05-23 22:13