I know how to pass one parameter to an sql query but i want to create a function to pass multiple params that will have differents type and here im stuck.
public List<T> RawSql<T>(string query, params object[] parameters)
{
var command = context.Database.GetDbConnection().CreateCommand();
command.CommandText = query;
command.CommandType = CommandType.Text;
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@bookId";
parameter.SqlDbType = SqlDbType.Int;
parameter.Value = parameters[0];
command.Parameters.Add(parameter);
var result = command.ExecuteReader())
return result;
}
Usage :
var rows = helper.RawSql("myStoreProc @bookId", x=> new Book { Id = (bool)x[0] }, bookId);
But how i can change the RawSql function to pass multiple parameters like this :
var rows = helper.RawSql("myStoreProc @bookId, @authorName", x=> new Book { Id = (bool)x[0] }, bookId, authorName);
var data = connection.Query<Book>("myStoreProc", new { bookId, authorName }, commandType: CommandType.StoredProcedure).AsList();- which will do everything you would expect, including column-name based data binding ofBook(optimized, etc) (note: methods exist for all the usual scenarios)cmd.Parameters.Add(string, dbtype).Value = dataThe collection creates the parameter, names it, tells it the type and sets the value in one easy line of code. ORMs likeDapperallow that syntax you have at the end if that is critical