How can I use Dapper's strongly-typed query parameters with Sybase ASE?

醉酒当歌 提交于 2019-11-30 21:13:05

As far as I can tell, the reason seems to be something to do with Sybase's AseParameter class, and not correctly mapping DbType to AseDbType values.

You can work around the problem by defining a custom Dapper IDynamicParameters set, and populating it with AseParameter objects directly:

public class AseCommandParameters : List<AseParameter>, SqlMapper.IDynamicParameters
{
    public void AddParameters(IDbCommand command, SqlMapper.Identity identity)
    {
        foreach (var parameter in this)
            command.Parameters.Add(parameter);
    }
}

And then passing an instance into the query:

using (var connection = new AseConnection("..."))
{
    connection.Open();

    var parameters = new AseCommandParameters
    {
        new AseParameter("@table_name", "dbo.sysusers")
    };

    var results = connection.Query<Foo>("dbo.sp_columns", parameters, 
        commandType: CommandType.StoredProcedure);
}

This works, and also lets you specify the AseDbType for each parameter.

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