Esempio n. 1
0
        /// <summary>
        /// 插入类型为T的对象到表中,如果表中包含自增量主键,则返回主键编号, 否则返回1
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public int Insert <T>(T value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("Insert操作不能传入空值");
            }

            IDbConnection connection = CreateConnection();

            try
            {
                using (IDbCommand cmd = CreateCommand(connection, MyTransaction))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = InsertCache.SQL;

                    AddParametersClone2Cmd(cmd, InsertCache.Parameters, null);

                    EntityTools.ChangeType(value, cmd.Parameters);

                    int iRet = 0;

                    if (InsertCache.IsHaveAutoIncrement)
                    {
                        iRet = Convert.ToInt32(cmd.ExecuteScalar());
                    }
                    else
                    {
                        iRet = cmd.ExecuteNonQuery();
                    }

                    EntityTools.ChangeType(cmd.Parameters, value);

                    return(iRet);
                }
            }
            catch (Exception ex)
            {
                throw new DatabaseException(ex.Message, ex, DeleteCache.SQL);
            }
            finally
            {
                if (MyConnection != connection && MyTransaction == null)
                {
                    if (!KeepAlive)
                    {
                        connection.Close();
                        connection.Dispose();
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 从表中根据主键检查一条记录是否存在, values顺序必须和主键顺序一致
        /// </summary>
        /// <typeparam name="T">类型T</typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool IsExist <T>(T value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("IsExist操作不能传入空值");
            }

            IDbConnection connection = CreateConnection();

            try
            {
                using (IDbCommand cmd = CreateCommand(connection, MyTransaction))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = IsExistCache.SQL;

                    AddParametersClone2Cmd(cmd, IsExistCache.Parameters, null);

                    EntityTools.ChangeType(value, cmd.Parameters);

                    return(Convert.ToInt32(cmd.ExecuteScalar()) > 0);
                }
            }
            catch (Exception ex)
            {
                throw new DatabaseException(ex.Message, ex, IsExistCache.SQL);
            }
            finally
            {
                if (MyConnection != connection && MyTransaction == null)
                {
                    if (!KeepAlive)
                    {
                        connection.Close();
                        connection.Dispose();
                    }
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 从表中根据主键更新一条记录, 从已知对象中转换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public virtual bool Update <T>(T value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("Update操作不能传入空值");
            }

            IDbConnection connection = CreateConnection();

            try
            {
                using (IDbCommand cmd = CreateCommand(connection, MyTransaction))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = UpdateCache.SQL;

                    AddParametersClone2Cmd(cmd, UpdateCache.Parameters, null);

                    EntityTools.ChangeType(value, cmd.Parameters);

                    return(cmd.ExecuteNonQuery() > 0);
                }
            }
            catch (Exception ex)
            {
                throw new DatabaseException(ex.Message, ex, UpdateCache.SQL);
            }
            finally
            {
                if (MyConnection != connection && MyTransaction == null)
                {
                    if (!KeepAlive)
                    {
                        connection.Close();
                        connection.Dispose();
                    }
                }
            }
        }