Exemple #1
0
        /// <summary>
        /// 执行 Sql 操作
        /// </summary>
        /// <param name="sqlProxyMethod">代理方法</param>
        /// <returns></returns>
        private static async Task <T> ExecuteSqlOfTAsync <T>(SqlProxyMethod sqlProxyMethod)
        {
            // 获取 ADO.NET 数据库操作对象
            var database = sqlProxyMethod.Context.Database;

            // 定义多次使用变量
            var returnType     = sqlProxyMethod.ReturnType;
            var sql            = sqlProxyMethod.FinalSql;
            var parameterModel = sqlProxyMethod.ParameterModel;
            var commandType    = sqlProxyMethod.CommandType;

            // 处理 DataSet 返回值
            if (returnType == typeof(DataSet))
            {
                var(dataSet, _) = await database.DataAdapterFillAsync(sql, parameterModel, commandType);

                return((T)(dataSet as object));
            }
            // 处理 元组类型 返回值
            else if (returnType.IsValueTuple())
            {
                var(dataSet, _) = await database.DataAdapterFillAsync(sql, parameterModel, commandType);

                var result = dataSet.ToValueTuple(returnType);

                return((T)result);
            }
            // 处理 基元类型 返回值
            else if (returnType.IsRichPrimitive())
            {
                var(result, _) = await database.ExecuteScalarAsync(sql, parameterModel, commandType);

                return((T)result);
            }
            // 处理 存储过程带输出类型 返回值
            else if (returnType == typeof(ProcedureOutputResult) || (returnType.IsGenericType && typeof(ProcedureOutputResult <>).IsAssignableFrom(returnType.GetGenericTypeDefinition())))
            {
                var(dataSet, dbParameters) = await database.DataAdapterFillAsync(sql, parameterModel, commandType);

                // 处理返回值
                var result = !returnType.IsGenericType
                    ? DbHelpers.WrapperProcedureOutput(dbParameters, dataSet)
                    : DbHelpers.WrapperProcedureOutput(dbParameters, dataSet, returnType.GenericTypeArguments.First());

                return((T)result);
            }
            else
            {
                var(dataTable, _) = await database.ExecuteReaderAsync(sql, parameterModel, commandType);

                // 处理 DataTable 返回值
                if (returnType == typeof(DataTable))
                {
                    return((T)(dataTable as object));
                }
                else
                {
                    var list = await dataTable.ToListAsync(returnType);

                    return((T)list);
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// 执行 Sql 操作
        /// </summary>
        /// <param name="sqlProxyMethod">代理方法</param>
        /// <returns></returns>
        private static object ExecuteSql(SqlProxyMethod sqlProxyMethod)
        {
            // 获取 ADO.NET 数据库操作对象
            var database = sqlProxyMethod.Context.Database;

            // 定义多次使用变量
            var returnType     = sqlProxyMethod.ReturnType;
            var sql            = sqlProxyMethod.FinalSql;
            var parameterModel = sqlProxyMethod.ParameterModel;
            var commandType    = sqlProxyMethod.CommandType;
            var isAsync        = sqlProxyMethod.IsAsync;

            // 处理 DataSet 返回值
            if (returnType == typeof(DataSet))
            {
                var(dataSet, _) = !isAsync
                    ? database.DataAdapterFill(sql, parameterModel, commandType)
                    : database.DataAdapterFillAsync(sql, parameterModel, commandType).GetAwaiter().GetResult();

                return(!isAsync ? dataSet : Task.FromResult(dataSet));
            }
            // 处理 无返回值
            else if (returnType == typeof(void))
            {
                var(rowEffects, _) = !isAsync
                    ? database.ExecuteNonQuery(sql, parameterModel, commandType)
                    : database.ExecuteNonQueryAsync(sql, parameterModel, commandType).GetAwaiter().GetResult();

                return(!isAsync ? rowEffects : Task.FromResult(rowEffects));
            }
            // 处理 元组类型 返回值
            else if (returnType.IsValueTuple())
            {
                var(dataSet, _) = !isAsync
                    ? database.DataAdapterFill(sql, parameterModel, commandType)
                    : database.DataAdapterFillAsync(sql, parameterModel, commandType).GetAwaiter().GetResult();

                var result = dataSet.ToValueTuple(returnType);
                return(!isAsync ? result : result.ToTaskResult(returnType));
            }
            // 处理 基元类型 返回值
            else if (returnType.IsRichPrimitive())
            {
                var(result, _) = !isAsync
                    ? database.ExecuteScalar(sql, parameterModel, commandType)
                    : database.ExecuteScalarAsync(sql, parameterModel, commandType).GetAwaiter().GetResult();

                return(!isAsync ? result : result.ToTaskResult(returnType));
            }
            // 处理 存储过程带输出类型 返回值
            else if (returnType == typeof(ProcedureOutputResult) || (returnType.IsGenericType && typeof(ProcedureOutputResult <>).IsAssignableFrom(returnType.GetGenericTypeDefinition())))
            {
                var(dataSet, dbParameters) = !isAsync
                    ? database.DataAdapterFill(sql, parameterModel, commandType)
                    : database.DataAdapterFillAsync(sql, parameterModel, commandType).GetAwaiter().GetResult();

                // 处理返回值
                var result = !returnType.IsGenericType
                    ? DbHelpers.WrapperProcedureOutput(dbParameters, dataSet) :
                             DbHelpers.WrapperProcedureOutput(dbParameters, dataSet, returnType.GenericTypeArguments.First());

                return(!isAsync ? result : result.ToTaskResult(returnType));
            }
            else
            {
                var(dataTable, _) = !isAsync
                        ? database.ExecuteReader(sql, parameterModel, commandType)
                        : database.ExecuteReaderAsync(sql, parameterModel, commandType).GetAwaiter().GetResult();

                // 处理 DataTable 返回值
                if (returnType == typeof(DataTable))
                {
                    return(!isAsync ? dataTable : Task.FromResult(dataTable));
                }
                else
                {
                    var list = dataTable.ToList(returnType);
                    return(!isAsync ? list : list.ToTaskResult(returnType));
                }
            }
        }