After upgrading some of our class libraries to .NET Standard 2.0, System.Data.SqlClient went away and we now need to use Microsoft.Data.SqlClient in those projects.
However, we are now seeing this error when calling a stored procedure:
The SqlParameterCollection only accepts non-null SqlParameter type objects, not SqlParameter objects
This worked fine on System.Data.SqlClient.
I've looked online and lot of people talk about this error being fixed after simply using Microsoft.Data.SqlClient, but since that's what creates my issue I'm not sure where to go from here.
public int GetStuff(long memberId, DateTime start, DateTime end)
{
var idParameter = new SqlParameter("@intID", memberId);
var startMonthParameter = new SqlParameter("@intMonth1", start.Month);
var endMonthParameter = new SqlParameter("@intMonth2", end.Month);
var startYearParameter = new SqlParameter("@intYear", start.Year);
return ExecuteStoreProcedure<int>("sp_GetStuff", idParameter, startMonthParameter, endMonthParameter, startYearParameter);
}
private T ExecuteStoreProcedure<T>(string procedureName, params SqlParameter[] sqlParamaters)
{
var procedure = $"{procedureName} {string.Join(", ", sqlParamaters.Select(p => p.ParameterName))}";
var parameters = sqlParamaters.ToArray<object>();
return Database.SqlQuery<T>(procedure, parameters).First();
}
Based on @madreflection comment I added the System.Data.SqlClient package instead of Microsoft.Data.SqlClient and it fixed my issues for .NET Standard 2.0. Thanks everyone for the help!