In the project I'm working to in this period I have to work a lot with stored procedures in SQL Server, actually I use this method (using 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;
}
This method has the following two problems:
- Can return only object of
foo_typetype - Accept only two parameters
I'd like to make this method general, so
- I need to pass all the params of the stored procedure
- I'd like to tell the function the type of the objects that will be returned from the stored procedure
The function declaration will be something like:
public static <T> call_stored_procedure(string stored_procedure_name, string ConnectionString, <T> type, <T> params...)
Is there a way to do something like this in C#?