Beispiel #1
0
        /// <summary>
        /// 将数据实体对应的记录从数据库表中虚拟删除,将删除标记标识为1。
        /// 实体
        /// </summary>
        /// <example>
        /// <para>下面的代码演示了VirtualDelete()方法的用法</para>
        /// <code>
        /// //Contract类需要继承自BaseEntity,并且需要放在以*.Entity.dll结尾的程序集中
        /// Contract contract = new Contract();
        ///
        /// //对主键字段进行赋值
        /// contract.ContractGUID = Request.QueryString["ContractGUID"];
        ///
        /// int count = contract.VirtualDelete();
        /// 虚拟删除成功后,count等于1,VirtualDelete字段更新为1
        /// </code>
        /// </example>
        /// <exception cref="InvalidOperationException">类没有定义主键或者虚拟删除字段,即没有任何一个属性被标记为PrimaryKey=true,VirtualDelete=true则抛出此异常</exception>
        /// <exception cref="InvalidProgramException">如果数据实体类型的定义不符合规范,就会抛出此异常</exception>
        /// <param name="connectionString">数据库连接字符串,默认值为null 即表示使用默认连接字符串</param>
        /// <returns>返回ADO.NET的原始结果</returns>
        public virtual int VirtualDelete(string connectionString = null)
        {
            CPQuery query = GetCPQuery(16, new object[] { this, connectionString });

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

            return(query.ExecuteNonQuery());
        }
Beispiel #2
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>
        /// <param name="connectionString">数据库连接字符串,默认值为null 即表示使用默认连接字符串</param>
        /// <returns>返回ADO.NET的原始结果</returns>
        public virtual int Update(string connectionString = null)
        {
            CPQuery query = GetCPQuery(7, new object[] { this, bakObject, connectionString });

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

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

            return(query.ExecuteNonQuery());
        }
Beispiel #3
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>
        /// <param name="connectionString">数据库连接字符串,默认值为null 即表示使用默认连接字符串</param>
        /// <returns>返回ADO.NET的原始结果</returns>
        public virtual int Insert(string connectionString = null)
        {
            CPQuery query = GetCPQuery(3, new object[] { this, connectionString });

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

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

            return(query.ExecuteNonQuery());
        }
Beispiel #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="Citms.Data.Extensions.Exception.OptimisticConcurrencyException">并发检测失败时,则会抛出此异常</exception>
        /// <param name="connectionString">数据库连接字符串,默认值为null 即表示使用默认连接字符串</param>
        /// <returns>返回ADO.NET的原始结果</returns>
        public virtual int Update(BaseEntity original, ConcurrencyMode concurrencyMode, string connectionString = null)
        {
            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, connectionString });

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

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

            int effectRows = query.ExecuteNonQuery();

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

            return(effectRows);
        }
Beispiel #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="Citms.Data.Extensions.Exception.OptimisticConcurrencyException">并发检测失败时,则会抛出此异常</exception>
        /// <param name="concurrencyMode">并发检测模式</param>
        /// <param name="connectionString">数据库连接字符串,默认值为null 即表示使用默认连接字符串</param>
        /// <returns>返回ADO.NET的原始结果</returns>
        public virtual int Delete(ConcurrencyMode concurrencyMode, string connectionString = null)
        {
            int     flag  = concurrencyMode == ConcurrencyMode.TimeStamp ? 5 : 6;
            CPQuery query = GetCPQuery(flag, new object[] { this, connectionString });

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

            int effectRows = query.ExecuteNonQuery();

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

            return(effectRows);
        }