/// <summary> /// 处理存储过程带输出结果 /// </summary> /// <param name="returnType">返回值类型</param> /// <param name="parameters">命令参数</param> /// <param name="dataSet">数据集</param> /// <returns></returns> private static object ConvertProcedureOutputResult(Type returnType, DbParameter[] parameters, DataSet dataSet) { // 是否是泛型 if (!returnType.IsGenericType) { return(DbHelpers.WrapperProcedureOutput(parameters, dataSet)); } else { var result = DbHelpers.WrapperProcedureOutput(parameters, dataSet, returnType.GenericTypeArguments.First()); return(result?.Adapt(result.GetType(), returnType)); } }
/// <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; // 处理 DataSet 返回值 if (returnType == typeof(DataSet)) { var(dataSet, _) = database.DataAdapterFill(sql, parameterModel, commandType); return(dataSet); } // 处理 无返回值 else if (returnType == typeof(void)) { var(rowEffects, _) = database.ExecuteNonQuery(sql, parameterModel, commandType); return(rowEffects); } // 处理 元组类型 返回值 else if (returnType.IsValueTuple()) { var(dataSet, _) = database.DataAdapterFill(sql, parameterModel, commandType); var result = dataSet.ToValueTuple(returnType); return(result); } // 处理 基元类型 返回值 else if (returnType.IsRichPrimitive()) { var(result, _) = database.ExecuteScalar(sql, parameterModel, commandType); return(result.ChangeType(returnType)); } // 处理 存储过程带输出类型 返回值 else if (returnType == typeof(ProcedureOutputResult) || (returnType.IsGenericType && typeof(ProcedureOutputResult <>).IsAssignableFrom(returnType.GetGenericTypeDefinition()))) { var(dataSet, dbParameters) = database.DataAdapterFill(sql, parameterModel, commandType); // 处理返回值 var result = !returnType.IsGenericType ? DbHelpers.WrapperProcedureOutput(database.ProviderName, dbParameters, dataSet) : DbHelpers.WrapperProcedureOutput(database.ProviderName, dbParameters, dataSet, returnType.GenericTypeArguments.First()); return(result); } else { var(dataTable, _) = database.ExecuteReader(sql, parameterModel, commandType); // 处理 DataTable 返回值 if (returnType == typeof(DataTable)) { return(dataTable); } else { var list = dataTable.ToList(returnType); return(list); } } }
/// <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); } } }