【问题标题】:Dapper using ODBC store procedure Input parm always promt @XXX was not suppliedDapper 使用 ODBC 存储过程输入参数始终提示 @XXX 未提供
【发布时间】:2023-03-21 05:25:01
【问题描述】:

我需要 All Dapper master 的帮助。

我从一个月前开始学习使用 Dapper,但是在使用 ODBC SP 执行查询时出现错误。

代码最初是由某人(DapperExample)编写的,但没有使用 ODBC,感谢作者我忘记了你的名字。

我的 SP:

创建过程 SP_GET_FIND_EMPLOYEES (@EmpID INT) 作为 开始 设置无计数; SELECT * FROM tblEmployee WHERE EmpID = @EmpID

结束 去吧


我的代码

公共类 EmployeeDashBoard : IEmployeeDashBoard {

    private IDbConnection _db;
    string connStr2 = WebConfigurationManager.ConnectionStrings["DapperExample"].ConnectionString;

    public EmployeeDashBoard()
    {

    }

    public Employee Find(int id)
    {

        //type b, by sp 
        using (IDbConnection connection = new OdbcConnection(connStr2))
        {

            var p = new DynamicParameters();

                p.Add("@EmpID", id);



            Employee result = this._db.Query<Employee>("dbo.SP_GET_FIND_EMPLOYEES", new { @EmpID = id }, commandType: CommandType.StoredProcedure).Single();
            return result;

        }



    }


}

错误:

错误 [42000] [Microsoft][SQL Server Native Client 11.0][SQL Server]过程或函数“SP_GET_FIND_EMPLOYEES”需要参数“@EmpID”,但未提供。

提前致谢。

马萨西赫

【问题讨论】:

    标签: odbc dapper


    【解决方案1】:

    我自己解决了,我正在使用 Sybase ODBC SP,(God Job Sam),现在我可以避免功能中的实体框架。 这里的技巧:

    已解决:SP_GET_FIND_EMPLOYEES ?


            using (IDbConnection connection = new OdbcConnection(connStr2))
            {
    
                var p = new DynamicParameters();
    
                p.Add("?EmpID?", id.ToString()); 
    
                Employee result = this._db.Query<Employee>("dbo.SP_GET_FIND_EMPLOYEES ?", p, commandType: CommandType.StoredProcedure).Single();
                return result;
            }
    

    【讨论】:

      【解决方案2】:

      您对 ODBC 命名参数的实现不正确。在语句中用问号将命名参数括起来,并创建不带问号的命名参数。 Dapper 使用问号来解析语句以查找名称。

      p.Add("EmpID", id.ToString());
      Employee result = this._db.Query<Employee>("dbo.SP_GET_FIND_EMPLOYEES ?EmpID?", p, commandType: CommandType.StoredProcedure).Single();
      

      有关更多信息,请参阅此答案:https://stackoverflow.com/a/26484944/6490042

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-07-04
        • 2021-02-27
        • 1970-01-01
        • 1970-01-01
        • 2021-03-15
        • 1970-01-01
        • 2021-07-14
        相关资源
        最近更新 更多