首页 技术 正文
技术 2022年11月12日
0 收藏 386 点赞 2,134 浏览 2917 个字

实体类基类:

 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();
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,904
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,430
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,247
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,058
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,690
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,727