Пример #1
0
        /// <summary>
        /// 更新数据实体对应的记录。
        /// </summary>
        /// <example>
        /// <para>下面的代码演示了Update()方法的用法</para>
        /// <code>
        /// //Contract类需要继承自BaseEntity,并且需要放在以*.Entity.dll结尾的程序集中
        /// Contract contract = new Contract();
        ///
        /// //为类的主键赋值
        /// contract.ContractGUID = Reuqest.QueryString["ContractGUID"];
        ///
        /// //为类的其他字段赋值
        /// //contract.ContractName = Request.Form["ContractName"];
        /// //...
        ///
        /// int count = contract.Update();
        /// //更新成功后,count等于1
        /// </code>
        /// </example>
        /// <exception cref="InvalidProgramException">如果数据实体类型的定义不符合规范,就会抛出此异常</exception>
        /// <exception cref="InvalidOperationException">类没有定义主键,即没有任何一个属性被标记为PrimaryKey=true,则抛出此异常</exception>
        /// <returns>返回ADO.NET的原始结果</returns>
        public virtual int Update()
        {
            CPQuery query = GetCPQuery(7, new object[] { this, bakObject });

            if (query == null)
            {
                return(0);
            }

            if (_funcBefore != null)
            {
                if (_funcBefore(query) == false)
                {
                    return(-1);
                }
            }

            return(query.ExecuteNonQuery());
        }
Пример #2
0
        /// <summary>
        /// 将数据实体插到对应的数据库表中。
        /// </summary>
        /// <example>
        /// <para>下面的代码演示了Insert()方法的用法</para>
        /// <code>
        /// //Contract类需要继承自BaseEntity,并且需要放在以*.Entity.dll结尾的程序集中
        /// Contract contract = new Contract();
        ///
        /// contract.ContractGUID = Guid.NewGuid();
        /// contract.ContractName = "...";
        /// //...其他字段
        ///
        /// int count = contract.Insert();
        /// //插入成功后,count等于1
        /// </code>
        /// </example>
        /// <exception cref="InvalidOperationException">1.如果没有对实体类的任何一个字段赋值,就进行Insert()操作,则会抛出此异常2.类没有定义主键,即没有任何一个属性被标记为PrimaryKey=true,则抛出此异常</exception>
        /// <exception cref="InvalidProgramException">如果数据实体类型的定义不符合规范,就会抛出此异常</exception>
        /// <returns>返回ADO.NET的原始结果</returns>
        public virtual int Insert()
        {
            CPQuery query = GetCPQuery(3, new object[] { this });

            if (query == null)
            {
                throw new InvalidOperationException("传入对象不能生成有效的SQL语句。");
            }

            if (_funcBefore != null)
            {
                if (_funcBefore(query) == false)
                {
                    return(-1);
                }
            }

            return(query.ExecuteNonQuery());
        }
Пример #3
0
        /// <summary>
        /// 通过参数化SQL、匿名对象的方式,创建CPQuery对象实例
        /// </summary>
        /// <example>
        /// <para>下面的代码演示了通过参数化SQL,匿名对象的方式,创建CPQuery对象实例的用法</para>
        /// <code>
        /// //声明匿名类型
        /// var product = new {
        ///		ProductName = "产品名称",
        ///		Quantity = 10
        /// };
        ///
        /// //SQL中的参数名就是@加匿名类型的属性名
        /// CPQuery.From("INSERT INTO Products(ProductName, Quantity) VALUES(@ProductName, @Quantity)", product).ExecuteNonQuery();
        /// </code>
        /// </example>
        /// <param name="parameterizedSQL">参数化的SQL字符串</param>
        /// <param name="argsObject">匿名对象</param>
        /// <returns>CPQuery对象实例</returns>
        public static CPQuery From(string parameterizedSQL, object argsObject)
        {
            if (string.IsNullOrEmpty(parameterizedSQL))
            {
                throw new ArgumentNullException("parameterizedSQL");
            }


            CPQuery query = new CPQuery(parameterizedSQL);

            if (argsObject != null)
            {
                PropertyInfo[] properties = argsObject.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
                foreach (PropertyInfo pInfo in properties)
                {
                    object value = pInfo.FastGetValue(argsObject);
                    string name  = "@" + pInfo.Name;

                    if (value == null || value == DBNull.Value)
                    {
                        query._command.Parameters.AddWithValue(name, DBNull.Value);
                        //SqlParameter paramter = new SqlParameter(name, DBNull.Value);
                        //paramter.SqlDbType = SqlDbType.Variant;
                        //query._command.Parameters.Add(paramter);
                        //throw new ArgumentException("输入参数的属性值不能为空。");
                    }
                    else
                    {
                        SqlParameter parameter = value as SqlParameter;
                        if (parameter != null)
                        {
                            query._command.Parameters.Add(parameter);
                        }
                        else
                        {
                            query._command.Parameters.AddWithValue(name, value);
                        }
                    }
                }
            }

            return(query);
        }
Пример #4
0
        /// <summary>
        /// 用并发检测的方式,更新数据实体对应的记录。
        /// </summary>
        /// <example>
        /// <para>下面的代码演示了并发检测模式下,Update()方法的用法</para>
        /// <code>
        /// <![CDATA[
        /// //Contract类需要继承自BaseEntity,并且需要放在以*.Entity.dll结尾的程序集中
        ///
        /// public void Load(){
        ///
        ///     //在页面加载时,查询数据库信息,时间戳字段需要通过CAST转换为长整型,绑定到界面中
        ///     Contract contract = CPQuery.From("SELECT ContractGUID, CAST(ContractVersion AS BigInt) ContractVersion  .... FROM cb_Contract WHERE ...").ToSingle<Contract>();
        ///
        ///     //其他数据绑定代码
        ///     //...
        /// }
        ///
        /// public void Update(string dataXML, long contractVersion){
        ///		//将AppFrom的xml直接转换为实体对象
        ///		Contract contract = XmlDataEntity.ConvertXmlToSingle<CbContract>(dataXML)
        ///
        ///		//构造用于并发检测的原对象
        ///		Contract origContract = new Contract();
        ///		origContract.ContractGUID = contract.ContractGUID; //并发检测时,原对象的主键是必须提供的
        ///		contract.ContractVersion = contractVersion.Int64ToTimeStamp(); //界面中长整型的时间戳字段可以通过Int64ToTimeStamp扩展方法转换为byte[]数组
        ///
        ///		try{
        ///			//根据时间戳字段,进行并发检测
        ///			int count = contract.Update(origContract, ConcurrencyMode.TimeStamp);
        ///			//如果更新成功,则count为1
        ///
        ///			//根据原始值,进行并发检测
        ///			//count = contract.Update(origContract, ConcurrencyMode.OriginalValue);
        ///		}
        ///		catch(OptimisticConcurrencyException ex){
        ///			//并发检测失败,将会抛出OptimisticConcurrencyException异常
        ///		}
        /// }
        /// ]]>
        /// </code>
        /// </example>
        /// <param name="original">用于并发检测的原始对象</param>
        /// <param name="concurrencyMode">并发检测模式</param>
        /// <exception cref="InvalidProgramException">如果数据实体类型的定义不符合规范,就会抛出此异常</exception>
        /// <exception cref="InvalidOperationException">类没有定义主键,即没有任何一个属性被标记为PrimaryKey=true,则抛出此异常</exception>
        /// <exception cref="ArgumentException">用于并发检测的原始对象不能是当前对象</exception>
        /// <exception cref="Mysoft.Map.Extensions.Exception.OptimisticConcurrencyException">并发检测失败时,则会抛出此异常</exception>
        /// <returns>返回ADO.NET的原始结果</returns>
        public virtual int Update(BaseEntity original, ConcurrencyMode concurrencyMode)
        {
            if (original == null)
            {
                throw new ArgumentNullException("original");
            }


            if (concurrencyMode == ConcurrencyMode.OriginalValue && object.ReferenceEquals(this, original))
            {
                throw new ArgumentException("用于并发检测的原始对象不能是当前对象。");
            }

            int     flag  = concurrencyMode == ConcurrencyMode.TimeStamp ? 8 : 9;
            CPQuery query = GetCPQuery(flag, new object[] { this, original, bakObject });

            if (query == null)
            {
                return(0);
            }

            if (_funcBefore != null)
            {
                if (_funcBefore(query) == false)
                {
                    return(-1);
                }
            }

            int effectRows = query.ExecuteNonQuery();

            if (effectRows == 0)
            {
                throw new Mysoft.Map.Extensions.Exception.OptimisticConcurrencyException(
                          "并发操作失败,本次操作没有更新任何记录,请确认当前数据行没有被其他用户更新或删除。");
            }

            return(effectRows);
        }
Пример #5
0
        /// <summary>
        /// 用并发检测的方式,将数据实体对应的记录从数据库表中删除。
        /// </summary>
        /// <example>
        /// <para>下面的代码演示了并发检测模式下,Delete()方法的用法</para>
        /// <code>
        /// <![CDATA[
        /// //Contract类需要继承自BaseEntity,并且需要放在以*.Entity.dll结尾的程序集中
        ///
        /// public void Load(){
        ///
        ///     //在页面加载时,查询数据库信息,时间戳字段需要通过CAST转换为长整型,绑定到界面中
        ///     Contract contract = CPQuery.From("SELECT ContractGUID, CAST(ContractVersion AS BigInt) ContractVersion  .... FROM cb_Contract WHERE ...").ToSingle<Contract>();
        ///
        ///     //其他数据绑定代码
        ///     //...
        /// }
        ///
        /// //删除通道,前端需要传递合同GUID,时间戳字段
        /// public void Delete(Guid contractGUID, long contractVersion){
        ///
        ///		//删除动作,需要构建一个实体对象
        ///		Contract contract = new Contract();
        ///		contract.ContractGUID = contractGUID; //主键必须赋值,这是删除语句的首要条件
        ///		contract.ContractVersion = contractVersion.Int64ToTimeStamp(); //界面中长整型的时间戳字段可以通过Int64ToTimeStamp扩展方法转换为byte[]数组
        ///
        ///		try{
        ///			//根据时间戳字段,进行并发检测
        ///			int count = contract.Delete(ConcurrencyMode.TimeStamp);
        ///			//如果删除成功,则count为1
        ///
        ///			//根据原始值,进行并发检测
        ///			//count = contract.Delete(oldContract, ConcurrencyMode.OriginalValue);
        ///		}
        ///		catch(OptimisticConcurrencyException ex){
        ///			//并发检测失败,将会抛出OptimisticConcurrencyException异常
        ///		}
        /// }
        /// ]]>
        /// </code>
        /// </example>
        /// <exception cref="InvalidProgramException">如果数据实体类型的定义不符合规范,就会抛出此异常</exception>
        /// <exception cref="InvalidOperationException">类没有定义主键,即没有任何一个属性被标记为PrimaryKey=true,则抛出此异常</exception>
        /// <exception cref="Mysoft.Map.Extensions.Exception.OptimisticConcurrencyException">并发检测失败时,则会抛出此异常</exception>
        /// <param name="concurrencyMode">并发检测模式</param>
        /// <returns>返回ADO.NET的原始结果</returns>
        public virtual int Delete(ConcurrencyMode concurrencyMode)
        {
            int     flag  = concurrencyMode == ConcurrencyMode.TimeStamp ? 5 : 6;
            CPQuery query = GetCPQuery(flag, new object[] { this });

            if (_funcBefore != null)
            {
                if (_funcBefore(query) == false)
                {
                    return(-1);
                }
            }

            int effectRows = query.ExecuteNonQuery();

            if (effectRows == 0)
            {
                throw new Mysoft.Map.Extensions.Exception.OptimisticConcurrencyException(
                          "并发操作失败,本次操作没有删除任何记录,请确认当前数据行没有被其他用户更新或删除。");
            }

            return(effectRows);
        }