Execute stored procedure w/parameters in Dapper

前端 未结 5 909
被撕碎了的回忆
被撕碎了的回忆 2020-12-31 05:02

I\'m using Dapper (thanks Sam, great project.) a micro ORM with a DAL and by some reason I\'m not able to execute stored procedures with input parameters.

In a exam

5条回答
  •  感动是毒
    2020-12-31 05:28

    You'll need to extend it to support outbound parameters and returning results, but it contains the portion for creating the Dapper dynamic parameters.

    internal static bool ExecuteProc(string sql, List paramList = null)
    {
        try
        {
            using (SqlConnection conn = new SqlConnection (GetConnectionString()))
            {                    
               DynamicParameters dp = new DynamicParameters();
               if(paramList != null)
                   foreach (SqlParameter sp in paramList)
                       dp.Add(sp.ParameterName, sp.SqlValue, sp.DbType);
               conn.Open();
               return conn.Execute(sql, dp, commandType: CommandType.StoredProcedure) > 0;
            }
        }
        catch (Exception e)
        {
            //do logging
            return false;
        }
    

    }

提交回复
热议问题