【发布时间】:2015-08-13 12:50:47
【问题描述】:
在这段时间我正在做的项目中,我必须在 SQL Server 中使用存储过程进行大量工作,实际上我使用这种方法(使用 Dapper):
public static foo_type call_stored_procedure(string stored_procedure_name, string ConnectionString, type_param_1 param1, type_param_2 param2)
{
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
var server_return = connection.Query<foo_type>(stored_procedure_name,
new { param1 = param1, param2 = param2 }, commandType: CommandType.StoredProcedure);
if (server_return != null)
return server_return;
}
return null;
}
这种方法存在以下两个问题:
- 只能返回
foo_type类型的对象 - 只接受两个参数
我想让这个方法通用,所以
- 我需要传递存储过程的所有参数
- 我想告诉函数将从存储过程返回的对象的类型
函数声明将类似于:
public static <T> call_stored_procedure(string stored_procedure_name, string ConnectionString, <T> type, <T> params...)
有没有办法在 C# 中做这样的事情?
【问题讨论】:
-
发送SqlParameters的数组[].......
-
了解方法重载。当然这不是您问题的答案,但方法调用者感觉就像调用相同的方法
-
你可以使用Dapper。
标签: c# asp.net sql-server stored-procedures refactoring