.NET餐厅管理系统sql数据帮助类执行对单个Entity的更新(这个可以添加额外的约束条件)
admin
2024-02-28 05:36:48

    #region  //执行对单个Entity的更新(这个可以添加额外的约束条件)
    ///


    /// 执行对单个Entity的更新
    ///

    /// 由控制器组装而来的实体
    /// 其他约束条件用(and开头,例如:"and name='王斐'"
    public void ExecuteObjectUpdate(BaseEntity baseEntity, string otherConditions)
    {
        string sql_ = "";//values 之前
        try
        {
            Type type = baseEntity.GetType();
            IList tableAttribute = type.GetCustomAttributesData();//该方法是根据Type属性找到的
            string tableName = "";//表名
            tableName = (String)tableAttribute[0].ConstructorArguments[0].Value;//解刨Type属性中偶然发现

            string operation = "update";//执行操作方法,此处可以考虑将操作类型operation由调用者确定
            PropertyInfo[] propertyList = type.GetProperties();//返回PropertyInfo类型,用于取得该类的属性的信息

            //生成SQL语句
            string propertyName = "";//属性名
            string propertyValue = "";//属性值

            sql_ = operation + " " + tableName + " set ";

            int updateCount = 0;//用于计数更新

            for (int i = 1; i < propertyList.Length; i++)
            //这里考虑并没有写死跟添加一样,也从0开始,有一段时间考虑想从i=1开始,
            //但是存在部分情况主键可以改变的情况,因此,这里会默认的将首字段进行拼接,
            //会出现update tableName set PKName=value.... where  PKName=value;的情况
            //个人认为这并不影响


            //2015年5月28日02:14:26
            //由于部分主键是自增的,如果默认主键参与跟新,则会因为自增字段不能update而报错
            //但是没有找到合适方法
            //这里还是从第2个开始判断
            {
                //propertyList.Add("");
                propertyName = propertyList[i].Name;
                propertyValue = GetObjectPropertyValue(type, baseEntity, propertyName);//获取属性值
                if (propertyValue != null)
                {
                    if (updateCount != 0)//判断是否是第一个更新的字段如果是则不添加逗号
                    {
                        sql_ += " , ";
                    }
                    sql_ += "[" + propertyName + "]"+"= N'{0}'";
                    sql_ = string.Format(sql_, propertyValue);
                    updateCount++;
                }

            }
            string PKPropertyName = propertyList[0].Name;//此处还在考虑 是否需要解析一下 ,可以改为支持两个主键或者以上
            string PKPropertyValue = GetObjectPropertyValue(type, baseEntity, PKPropertyName);

            sql_ = sql_ + " where " + PKPropertyName + "=N'{0}'" + " " + otherConditions;//此处添加而外约束条件跟主键
            sql_ = String.Format(sql_, PKPropertyValue);

            OpenDb();
            SqlCommand cm = new SqlCommand(sql_, conn);
            cm.ExecuteNonQuery();
            cm.Dispose();
            cm = null;
            CloseDb();
        }
        catch (Exception e)
        {
            throw new Exception(e.ToString() + "  " + sql_);
        }

    }
    #endregion
 

相关内容

热门资讯

银河航天徐鸣:太空科创最终目标... 格隆汇1月25日丨在2026北京国际商业航天论坛上,银河航天创始人、董事长兼CEO徐鸣发表了题为《太...
一周新车盘点 | 吉利银河V9... 吉利银河V900正式上市关键词:大型MPV/30.98至36.98万元/增程动力1月20日,吉利银河...
事关每一位基民,新规3月起施行 投资者选基金,有了明确可参考的“尺子”!《公开募集证券投资基金业绩比较基准指引》以及《公开募集证券投...
“计划有变”!北京降雪情况有“... 转自:新华社新华社北京1月25日电(记者 田晨旭)记者25日从北京气象部门了解到,由于降雪天气系统强...
中欧班列(成渝)年度开行量超5... 转自:新华社新华社重庆1月25日电(记者 李晓婷)记者25日从重庆市政府口岸物流办获悉,2025年中...