示例#1
0
        private void AddUpdateObject(string keyName, T entity, Set_ updateSet, params string[] excludeColumns)
        {
            UpdateObject updateObject = new UpdateBuilder <T> .UpdateObject();

            if (keyName != null && keyName.Trim().Length > 0)
            {
                Dictionary <string, object> dit = new Dictionary <string, object>();
                Where_ tempWhere = new Where_();

                object[]        objs           = null;
                PropertyInfo [] pis            = typeof(T).GetProperties();
                bool            isKeyValueNull = true; // 主键值是否为null
                for (int i = 0; i < pis.Length; i++)
                {
                    if (excludeColumns != null && excludeColumns.Length > 0)
                    {
                        bool isExclude = false;
                        foreach (string excludeColumn in excludeColumns)
                        {
                            if (pis[i].Name.ToLower() == excludeColumn.Trim().ToLower())
                            {
                                isExclude = true;
                                break;
                            }
                        }
                        if (isExclude)
                        {
                            continue;
                        }
                    }

                    // 排除只显示列
                    objs = pis[i].GetCustomAttributes(false);
                    if (objs != null && objs.Length > 0)
                    {
                        EntityAttribute cusatt = (EntityAttribute)objs[0];
                        if (cusatt.DisplayOnly)
                        {
                            continue;
                        }
                    }
                    object value = pis[i].GetValue(entity, null);
                    if (value == null)
                    {
                        continue;
                    }

                    if (pis[i].Name.ToLower() == keyName.Trim().ToLower())
                    {
                        isKeyValueNull = false;
                        tempWhere.Equal(keyName, value);
                        continue;
                    }

                    dit.Add(pis[i].Name, value);
                }

                if (isKeyValueNull)
                {
                    throw new Exception(string.Format("属性{0}的值不能为null", keyName));
                }

                updateObject.Where         = tempWhere;
                updateObject.DitUpdateInfo = dit;
            }
            else if (updateSet != null)
            {
                updateObject.DitUpdateInfo = updateSet.DitUpdateInfo;
                updateObject.Where         = updateSet.MyWhere;
            }

            if (tranBuilder != null)
            {
                updateObject.CmdIndex = tranBuilder.GetCmdTextsCount();

                // 在集合中站位,等创建脚本的时候再将内容填充
                tranBuilder.AddCommondType(CommandType.Text);
                tranBuilder.AddCmdText(null);
                tranBuilder.AddCmdParam(null);
            }
            else
            {
                // 不是事务操作时,一次只操作一条语句
                this.listUpdateObject.Clear();
                updateObject.CmdIndex = listUpdateObject.Count;
            }
            this.listUpdateObject.Add(updateObject);
        }
示例#2
0
 private void AddUpdateObject(string keyName, T entity, Set_ updateSet)
 {
     AddUpdateObject(keyName, entity, updateSet, null);
 }