コード例 #1
0
ファイル: MsSqlDatabase4Dapper.cs プロジェクト: wyh0395/Berry
        /// <summary>
        /// 根据主键查询一个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection"></param>
        /// <param name="keyValue"></param>
        /// <param name="transaction"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public T FindEntity <T>(IDbConnection connection, object keyValue, IDbTransaction transaction = null, int?timeout = Timeout) where T : class
        {
            T res = null;

            this.Logger(this.GetType(), "根据主键查询一个实体-FindEntity", () =>
            {
                Type type       = keyValue.GetType();
                string key      = EntityAttributeHelper.GetEntityKey <T>();
                string whereStr = " WHERE 1 = 1 ";
                whereStr        = string.Format(type == typeof(int) ? " WHERE {0} = {1}" : " WHERE {0} = '{1}'", key, keyValue);

                string sql = DatabaseCommon.SelectSql <T>(whereStr, true).ToString();

                res = connection.Query <T>(sql, null, transaction, true, timeout).FirstOrDefault();
            }, e =>
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }

                connection.Close();
                connection.Dispose();
            }, () =>
            {
            });
            return(res);
        }
コード例 #2
0
        /// <summary>
        /// 根据主键获取一条数据
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns></returns>
        public T FindEntity <T>(object keyValue) where T : class, new()
        {
            T res = default(T);

            string keyField = EntityAttributeHelper.GetEntityKey <T>();

            string where = $"Where [{keyField}] = ";
            if (keyValue is int)
            {
                where += $"{keyValue}";
            }
            else if (keyValue is string)
            {
                where += $"'{keyValue}'";
            }
            string sql = DatabaseCommon.SelectSql <T>(where).ToString();

            using (var conn = Connection)
            {
                DataSet data = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql);
                if (data.Tables[0].IsExistRows())
                {
                    DataTable table = data.Tables[0];
                    res = table.DataTableToObject <T>();
                }
            }

            return(res);
        }
コード例 #3
0
ファイル: MsSqlDatabase4Dapper.cs プロジェクト: wyh0395/Berry
        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection"></param>
        /// <param name="keyValue"></param>
        /// <param name="transaction"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public int Delete <T>(IDbConnection connection, object keyValue, IDbTransaction transaction = null, int?timeout = Timeout) where T : class
        {
            int res = 0;

            this.Logger(this.GetType(), "根据主键删除-Delete", () =>
            {
                Type type       = keyValue.GetType();
                string key      = EntityAttributeHelper.GetEntityKey <T>();
                string whereStr = " WHERE 1 = 1 ";
                whereStr        = string.Format(type == typeof(int) ? " WHERE {0} = {1}" : " WHERE {0} = '{1}'", key, keyValue);

                string sql = DatabaseCommon.DeleteSql <T>(whereStr).ToString();
                res        = connection.Execute(sql, null, transaction, timeout);
            }, e =>
            {
                if (transaction != null)
                {
                    transaction.Rollback();
                }

                connection.Close();
                connection.Dispose();
            }, () =>
            {
            });
            return(res);
        }
コード例 #4
0
        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public int Delete <T>(object keyValue) where T : class
        {
            int res = 0;

            Type   type = keyValue.GetType();
            string key  = EntityAttributeHelper.GetEntityKey <T>();

            string where = " WHERE 1 = 1";
            if (type == typeof(int))
            {
                where = $" WHERE {key} = {keyValue}";
            }
            else
            {
                where = $" WHERE {key} = '{keyValue}'";
            }

            string sql = DatabaseCommon.DeleteSql <T>(where).ToString();

            using (var connection = Connection)
            {
                res = connection.Execute(sql);
            }
            return(res);
        }
コード例 #5
0
        /// <summary>
        /// 根据主键删除一条数据
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        public int Delete <T>(object keyValue) where T : class
        {
            int res = 0;

            string keyField = EntityAttributeHelper.GetEntityKey <T>();

            string where = $"Where [{keyField}] = ";
            if (keyValue is int)
            {
                where += $"{keyValue}";
            }
            else if (keyValue is string)
            {
                where += $"'{keyValue}'";
            }
            string sql = DatabaseCommon.DeleteSql <T>(where).ToString();

            if (SqlTransaction == null)
            {
                using (var conn = Connection)
                {
                    res = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql);
                }
            }
            else
            {
                res = SqlHelper.ExecuteNonQuery(SqlTransaction, CommandType.Text, sql);

                //res = this.Commit();
            }
            return(res);
        }
コード例 #6
0
        /// <summary>
        /// 根据主键查询一个实体
        /// </summary>
        /// <typeparam name="T">动态对象</typeparam>
        /// <param name="keyValue">主键</param>
        /// <param name="timeout">超时时间</param>
        /// <returns></returns>
        public T FindEntity <T>(object keyValue, int?timeout = Timeout) where T : IEntity
        {
            T res = default(T);

            this.Logger(this.GetType(), "根据主键查询一个实体-FindEntity", () =>
            {
                Type type       = keyValue.GetType();
                string key      = EntityAttributeHelper.GetEntityKey <T>();
                string whereStr = string.Format(type == typeof(int) ? " WHERE {0} = {1}" : " WHERE {0} = '{1}'", key, keyValue);

                string sql = DatabaseCommon.SelectSql <T>(whereStr, true).ToString();

                if (this.BaseDbTransaction == null)
                {
                    using (IDbConnection connection = this.BaseDbConnection)
                    {
                        res = connection.Query <T>(sql, null, null, true, timeout, CommandType.Text).FirstOrDefault();
                    }
                }
                else
                {
                    res = this.BaseDbTransaction.Connection.Query <T>(sql, null, this.BaseDbTransaction, true, timeout, CommandType.Text).FirstOrDefault();
                }
            }, e =>
            {
                this.Rollback();
            });
            return(res);
        }
コード例 #7
0
        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <typeparam name="T">动态对象</typeparam>
        /// <param name="keyValue">主键</param>
        /// <param name="timeout">超时时间</param>
        /// <returns></returns>
        public int Delete <T>(object keyValue, int?timeout = Timeout)
        {
            int res = 0;

            this.Logger(this.GetType(), "根据主键删除-Delete", () =>
            {
                Type type       = keyValue.GetType();
                string key      = EntityAttributeHelper.GetEntityKey <T>();
                string whereStr = string.Format(type == typeof(int) ? " WHERE {0} = {1}" : " WHERE {0} = '{1}'", key, keyValue);

                string sql = DatabaseCommon.DeleteSql <T>(whereStr).ToString();

                if (this.BaseDbTransaction == null)
                {
                    using (IDbConnection connection = this.BaseDbConnection)
                    {
                        res = connection.Execute(sql, null, null, timeout, CommandType.Text);
                    }
                }
                else
                {
                    res = this.BaseDbTransaction.Connection.Execute(sql, null, this.BaseDbTransaction, timeout, CommandType.Text);
                }
            }, e =>
            {
                this.Rollback();
            });
            return(res);
        }
コード例 #8
0
        /// <summary>
        /// 根据主键查询一个实体
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public T FindEntity <T>(object keyValue) where T : class, new()
        {
            Type   type = keyValue.GetType();
            string key  = EntityAttributeHelper.GetEntityKey <T>();

            string where = " WHERE 1 = 1";
            if (type == typeof(int))
            {
                where = $" WHERE {key} = {keyValue}";
            }
            else
            {
                where = $" WHERE {key} = '{keyValue}'";
            }
            string sql = DatabaseCommon.SelectSql <T>(where).ToString();

            using (var dbConnection = Connection)
            {
                var data = dbConnection.Query <T>(sql);
                return(data.FirstOrDefault());
            }
        }
コード例 #9
0
 /// <summary>
 /// 根据主键查询一个实体
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="keyValue"></param>
 /// <returns></returns>
 public T FindEntity <T>(object keyValue) where T : class, new()
 {
     using (var dbConnection = Connection)
     {
         var data = dbConnection.Query <T>(string.Format("select * from {0} where {1}=?key", EntityAttributeHelper.GetEntityTable <T>(), EntityAttributeHelper.GetEntityKey <T>()), new { key = keyValue });
         return(data.FirstOrDefault());
     }
 }
コード例 #10
0
        /// <summary>
        /// 根据主键删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public int Delete <T>(object keyValue) where T : class
        {
            T entity = DbTransaction.Connection.Query <T>(string.Format("select * from {0} where {1}=?primarykey", EntityAttributeHelper.GetEntityTable <T>(), EntityAttributeHelper.GetEntityKey <T>()), new { primarykey = keyValue }).FirstOrDefault();

            return(Delete <T>(entity));
        }