C# Using Parameters in SqlHelper.DB

北慕城南 提交于 2020-01-06 08:11:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!