Alternative way to get output parameter from stored procedure

后端 未结 1 1421
栀梦
栀梦 2021-01-02 02:33

I love using Dapper for my ORM needs but I know there must be a better way to insert/update my sql server database using a stored procedure and strongly typed Lists.

相关标签:
1条回答
  • 2021-01-02 03:18

    As I stated originally, I didn't want to have to manually convert each class property to a Dapper dynamic parameter. This is critical because if I create a generic method, I may not know which class is being passed into the method and thus which properties to pass to convert to dynamic parameters. Taking @Metro Smurfs advice (when all else fails read the directions), I looked into the Dapper test class and found a solution that works for me:

    DynamicParameters p = new DynamicParameters(song);
    

    by adding the song object to the DynamicParameters constructor, a DynamicParameters template is created which will automatically convert all properties to parameters. Then I can simply add my two output parameters and execute the sproc:

    p.Add("@success_updated", dbType: DbType.Boolean, direction: ParameterDirection.Output);
    p.Add("@success_added", dbType: DbType.Boolean, direction: ParameterDirection.Output);
    dbConn.Execute("Test_UpdateSong", p, commandType: CommandType.StoredProcedure);
    // get my output parameters...
    var success_added = p.Get<bool>("@success_Added");
    var success_added = p.Get<bool>("@success_Updated");
    

    and I'm good to go! Thanks to @Nick and @Metro Smurf for the suggestions!

    0 讨论(0)
提交回复
热议问题