8

I am executing a stored procedure with Dapper like this:

var sprocResult = conn.Query("TestSproc", new {clientID = 2}, commandType: CommandType.StoredProcedure).ToList();

I can enumerate the results and list the values. What I need to be able to do though is also enumerate the field names that come back from the sproc. I will not know these field names at design time.

Thanks in advance.

4

2 回答 2

16

使用dynamicAPI 时(根据您的示例,Query(...)代替Query<T>(...)),每一行还实现IDictionary<string,object>. 所以基本上:

foreach(IDictionary<string,object> row in sprocResult)
{
    var colNames = row.Keys;
    //...
}
于 2013-10-01T09:29:52.560 回答
3

如果查询可能不返回任何行,则Query().First().Keys不起作用。在这种情况下,您可以使用ExecuteReader().GetName(i).

于 2017-10-20T11:46:42.930 回答