sqlite或者其他使用反射的方式来实现Insert和Update,Delete的方法

插入模型:

      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;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值