插入模型:
public ResultMsg InsertModel<T>(T model)//插入模型
{
ResultMsg msg = new ResultMsg();
List<string> CloNameList = new List<string>();
List<string> ValueList = new List<string>();
StringBuilder SBValue = new StringBuilder();
PropertyInfo[] Properties = typeof(T).GetProperties();
foreach (PropertyInfo one in Properties)
{
if ((DescriptionAttribute)Attribute.GetCustomAttribute(one, typeof(DescriptionAttribute)) != null&& ((DescriptionAttribute)Attribute.GetCustomAttribute(one, typeof(DescriptionAttribute))).Description.Equals("DB"))
{
Type tp = one.GetType();
if (one.Name == "Id")
{
continue;
}
CloNameList.Add(one.Name);
if (one.PropertyType.Name.Contains("Int"))//如果是int不适用单引号
{
ValueList.Add(one.GetValue(model, null).ToString());
}
else if (one.PropertyType.IsEnum)//若是枚举取所在索引
{
//Type ty = one.PropertyType;
string ValEn = one.GetValue(model, null).ToString();
int valInt = (int)Enum.Parse(one.PropertyType, ValEn);
ValueList.Add(valInt.ToString());
}
else
{
ValueList.Add("'" + one.GetValue(model, null).ToString() + "'"); //获取数据为全部为string
}
}
}
string TabelName = typeof(T).Name;
string ColS = string.Join(",",CloNameList.ToArray());//逗号隔开
string ValS = string.Join(",",ValueList.ToArray());
string sql = $"insert into {TabelName} ({ColS}) values({ValS})";//插入命令
using (IDbConnection conn = GetConnect())
{
msg.ResInt = conn.Execute(sql);
conn.Close();
if (msg.ResInt >0)
{
msg.Succeed = true;
}
}
return msg;
}
更新数据的方法:
public ResultMsg UpdateModel<T>(T model)
{
ResultMsg msg = new ResultMsg();
List<string> CloAndVal = new List<string>();
StringBuilder SBValue = new StringBuilder();
PropertyInfo[] Properties = typeof(T).GetProperties();
string UpId = "0" ;
foreach (PropertyInfo one in Properties)
{
if ((DescriptionAttribute)Attribute.GetCustomAttribute(one, typeof(DescriptionAttribute)) != null && ((DescriptionAttribute)Attribute.GetCustomAttribute(one, typeof(DescriptionAttribute))).Description.Equals("DB"))
{
if (one.Name == "Id")
{
UpId = one.GetValue(model, null).ToString();
}
string CloName = one.Name;
string Val = "";
//CloNameList.Add(CloName);
if (one.PropertyType.Name.Contains("Int"))//如果是int不适用单引号
{
Val = one.GetValue(model, null).ToString();
}
else if (one.PropertyType.IsEnum)//若是枚举取所在索引
{
//Type ty = one.PropertyType;
string ValEn = one.GetValue(model, null).ToString();
int valInt = (int)Enum.Parse(one.PropertyType, ValEn);
Val = valInt.ToString();
}
else
{
Val = "'" + one.GetValue(model, null).ToString() + "'"; //获取数据为全部为string
}
CloAndVal.Add(CloName + "=" + Val);
}
}
string TabelName = typeof(T).Name;
string sqlOp = string.Join(",", CloAndVal);
string sql = $"update {TabelName} set {sqlOp} where Id = {UpId} ";//插入命令
using (IDbConnection conn = GetConnect())
{
msg.ResInt = conn.Execute(sql);
conn.Close();
if (msg.ResInt > 0)
{
msg.Succeed = true;
}
}
return msg;
}
删除数据
/// <summary>
/// 删除数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="model"></param>
/// <returns></returns>
public ResultMsg DelModel<T>(T model)
{
ResultMsg msg = new ResultMsg();
List<string> CloAndVal = new List<string>();
StringBuilder SBValue = new StringBuilder();
PropertyInfo[] Properties = typeof(T).GetProperties();
string UpId = "0";
foreach (PropertyInfo one in Properties)
{
if ((DescriptionAttribute)Attribute.GetCustomAttribute(one, typeof(DescriptionAttribute)) != null && ((DescriptionAttribute)Attribute.GetCustomAttribute(one, typeof(DescriptionAttribute))).Description.Equals("DB"))
{
if (one.Name == "Id")
{
UpId = one.GetValue(model, null).ToString();
break;
}
}
}
string TabelName = typeof(T).Name;
string sql = $"delete from {TabelName} where Id = {UpId} ";//删除命令
using (IDbConnection conn = GetConnect())
{
msg.ResInt = conn.Execute(sql);
if (msg.ResInt > 0)
{
msg.Succeed = true;
}
conn.Close();
}
return msg;
}