Beispiel #1
0
 /// <summary>
 /// 数据是否存在
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="condition"></param>
 /// <returns></returns>
 public bool Exists <T>(Expression <Func <T, bool> > condition) where T : class, new()
 {
     using (ExpressionCondition expression = db.GetExpressionCondition(condition))
     {
         string sql   = $"SELECT 0 WHERE EXISTS(SELECT 0 FROM [{typeof(T).GetTableName()}] { expression.ToCondition(out DynamicParameters parameters)} )";
         object value = db.ExecuteScalar(CommandType.Text, sql, parameters);
         return(value != null);
     }
 }
Beispiel #2
0
 public int Count <T>(Expression <Func <T, bool> > condition) where T : class, new()
 {
     using (IExpressionCondition expression = db.GetExpressionCondition(condition))
     {
         string sql   = $"SELECT COUNT(0) FROM [{typeof(T).GetTableName()}] { expression.ToCondition(out DynamicParameters parameters)} ";
         object value = db.ExecuteScalar(CommandType.Text, sql, parameters);
         return(value == null ? 0 : (int)value);
     }
 }
Beispiel #3
0
        public static TValue ExecuteScalar <T, TValue>(this DbExecutor db, T obj) where T : IProcedureModel
        {
            object result = db.ExecuteScalar(CommandType.StoredProcedure, typeof(T).Name, obj.ToParameters());

            if (result == null)
            {
                return(default);
Beispiel #4
0
        public void ExecuteScalar()
        {
            using (var exec = new DbExecutor(connectionFactory()))
            {
                exec.ExecuteScalar <long>("select @TypeId", new { TypeId = 2 })
                .Is(2);

                exec.ExecuteScalar <object>("select null").Is(DBNull.Value);
            }

            DbExecutor.ExecuteScalar <string>(connectionFactory(), "select date('now')")
            .Is(DateTime.Now.ToString("yyyy-MM-dd"));

            DbExecutor.ExecuteScalar <object>(connectionFactory(), "select null")
            .Is(DBNull.Value);
        }
Beispiel #5
0
        public void ExecuteScalar()
        {
            using (var exec = new DbExecutor(connectionFactory()))
            {
                exec.ExecuteScalar <int>("select max(TypeId) from Types where TypeId <= @TypeId", new { TypeId = 2 })
                .Is(2);
                exec.ExecuteScalar <int?>("select TypeId from Types where TypeId = -1000")
                .IsNull();
            }

            DbExecutor.ExecuteScalar <DateTime>(connectionFactory(), "select GETDATE()")
            .Day.Is(DateTime.Now.Day);

            DbExecutor.ExecuteScalar <int?>(connectionFactory(), "select TypeId from Types where TypeId = -1000")
            .IsNull();
        }
Beispiel #6
0
 /// <summary>
 /// 查询一个值
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <typeparam name="TValue"></typeparam>
 /// <param name="field"></param>
 /// <param name="condition"></param>
 /// <returns></returns>
 public static TValue ExecuteScalar <T, TValue>(this DbExecutor db, Expression <Func <T, TValue> > field, Expression <Func <T, bool> > condition) where T : class
 {
     using (ExpressionCondition expression = new ExpressionCondition())
     {
         expression.Visit(field);
         string sql = $"SELECT TOP 1 {expression.ToString()} FROM [{typeof(T).GetTableName()}]";
         expression.Visit(condition);
         sql += expression.ToCondition(out DynamicParameters parameters);
         object value = db.ExecuteScalar(CommandType.Text, sql, parameters);
         if (value == null)
         {
             return(default);