예제 #1
0
        /// <summary>
        /// 获取字段及值
        /// </summary>
        /// <returns></returns>
        internal List <FieldValue> GetFieldValues()
        {
            lock (this)
            {
                List <FieldValue> fvlist = new List <FieldValue>();

                Field        identityField = this.GetIdentityField();
                List <Field> pkFields      = new List <Field>(this.GetPrimaryKeyFields());

                Field[]  fields = this.GetFields();
                object[] values = this.GetValues();

                if (fields.Length != values.Length)
                {
                    throw new DataException("字段与值无法对应!");
                }

                int index = 0;
                foreach (Field field in fields)
                {
                    FieldValue fv = new FieldValue(field, values[index]);

                    //判断是否为标识列
                    if ((IField)identityField != null)
                    {
                        if (identityField.Name == field.Name)
                        {
                            fv.IsIdentity = true;
                        }
                    }

                    //判断是否为主键
                    if (pkFields.Any(p => p.Name == field.Name))
                    {
                        fv.IsPrimaryKey = true;
                    }

                    if (isUpdate)
                    {
                        //如果是更新,则将更新的字段改变状态为true
                        if (updatelist.Any(p => p.Name == field.Name))
                        {
                            fv.IsChanged = true;
                        }
                    }
                    else
                    {
                        //如果是插入,则将移除插入的字段改变状态为true
                        if (removeinsertlist.Any(p => p.Name == field.Name))
                        {
                            fv.IsChanged = true;
                        }
                    }

                    fvlist.Add(fv);
                    index++;
                }

                return(fvlist);
            }
        }
예제 #2
0
 /// <summary>
 /// 更新数据
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="table"></param>
 /// <param name="fv"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public int Update <T>(Table table, FieldValue fv, WhereClip where)
     where T : Entity
 {
     return(Update <T>(table, new FieldValue[] { fv }, where));
 }
예제 #3
0
 /// <summary>
 /// 更新数据
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="fv"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public int Update <T>(FieldValue fv, WhereClip where)
     where T : Entity
 {
     return(Update <T>(null, fv, where));
 }
예제 #4
0
        internal DbCommand CreateInsert <T>(Table table, List <FieldValue> fvlist, Field identityfield, string autoIncrementName)
            where T : Entity
        {
            T entity = DataUtils.CreateInstance <T>();

            if (entity.GetReadOnly())
            {
                throw new MySoftException("只读实体" + typeof(T).Name + "只能用于查询!");
            }

            //移除缓存
            RemoveCache(typeof(T).Name);

            string tableName = table == null?entity.GetTable().Name : table.Name;

            List <SQLParameter> plist   = new List <SQLParameter>();
            StringBuilder       sbsql   = new StringBuilder();
            StringBuilder       sbparam = new StringBuilder();

            if (UseAutoIncrement)
            {
                //如果标识列和标识名称都不为null
                if ((IField)identityfield != null && !string.IsNullOrEmpty(autoIncrementName))
                {
                    string identityName = FormatIdentityName(autoIncrementName);
                    bool   exist        = false;
                    fvlist.ForEach(fv =>
                    {
                        if (fv.IsIdentity)
                        {
                            fv.Value      = new SysValue(identityName);
                            fv.IsIdentity = false;
                            exist         = true;
                        }
                    });

                    if (!exist)
                    {
                        object     value = new SysValue(identityName);
                        FieldValue fv    = new FieldValue(identityfield, value);
                        fvlist.Insert(0, fv);
                    }
                }
            }

            sbsql.Append("insert into " + tableName + "(");
            sbparam.Append(" values (");

            fvlist.ForEach(fv =>
            {
                if (fv.IsIdentity)
                {
                    return;
                }

                sbsql.Append(fv.Field.At((string)null).Name);
                if (CheckValue(fv.Value))
                {
                    sbparam.Append(DataUtils.FormatValue(fv.Value));
                }
                else
                {
                    SQLParameter p = null;
                    if (CheckStruct(fv.Value))
                    {
                        p = CreateOrmParameter(DataUtils.FormatValue(fv.Value));
                    }
                    else
                    {
                        p = CreateOrmParameter(fv.Value);
                    }

                    sbparam.Append(p.Name);
                    plist.Add(p);
                }

                sbsql.Append(",");
                sbparam.Append(",");
            });

            sbsql.Remove(sbsql.Length - 1, 1).Append(")");
            sbparam.Remove(sbparam.Length - 1, 1).Append(")");

            string cmdText = string.Format("{0}{1}", sbsql, sbparam);

            return(CreateSqlCommand(cmdText, plist.ToArray()));
        }
예제 #5
0
 /// <summary>
 /// 更新数据
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="table"></param>
 /// <param name="fv"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public int Update <T>(Table table, FieldValue fv, WhereClip where)
     where T : Entity
 {
     return(dbTrans.Update <T>(table, fv, where));
 }
예제 #6
0
 /// <summary>
 /// 更新数据
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="fv"></param>
 /// <param name="where"></param>
 /// <returns></returns>
 public int Update <T>(FieldValue fv, WhereClip where)
     where T : Entity
 {
     return(dbTrans.Update <T>(fv, where));
 }