问题
I installed SqlHelper.DB NuGet package (https://www.nuget.org/packages/SQLHelper.DB/) and I am having issues passing parameters to the AddQuery method. I am passing a List of SqlParameter to my function and I receive an error: 'Failed to convert parameter value from a List`1 to a Int32.'
Here is the code to call my method:
SqlProcessor sqlproc = new SqlProcessor("Data Source = myserver; Initial Catalog = mydb; User ID = me; Password = $uper$ecret;");
List<SqlParameter> p = new List<SqlParameter>
{
new SqlParameter("@MyParam1", "param1"),
new SqlParameter("@MyParam2", "param2"),
};
List<dynamic> l = sqlproc.ExecuteNonQuery(new Models.SqlCommandHelper("dbo.spGoGadgetGo", p));
Here is the method:
public List<dynamic> ExecuteNonQuery(SqlCommandHelper sch)
{
var results = SQLHelper.CreateBatch()
.AddQuery(sch.CommandType, sch.StoredProcedureName, sch.StoredProcedureParameters)
.Execute();
return results[0];
}
SqlCommandHelper is a small class I wrote:
class SqlCommandHelper
{
public SqlCommandHelper(string spName, List<SqlParameter> parameters)
{
StoredProcedureName = spName;
StoredProcedureParameters = parameters;
CommandType = CommandType.StoredProcedure;
}
public SqlCommandHelper(string queryString)
{
QueryString = queryString;
CommandType = CommandType.Text;
}
public CommandType CommandType { get; set; }
public string StoredProcedureName { get; set; }
public string QueryString { get; set; }
public List<SqlParameter> StoredProcedureParameters { get; set; }
}
Any help greatly appreciated!
来源:https://stackoverflow.com/questions/58771178/c-sharp-using-parameters-in-sqlhelper-db