Dapper Column number rather than column name?

对着背影说爱祢 提交于 2019-12-18 05:11:22

问题


I have a very similar question to Dapper-dot-net "no column name", but the answer there is not getting me where I need.

I'm writing a web interface and using dapper to get data from Stored Procedures from my client's ERP system. The SP returns 4 columns of data without column names. That being said the SP's are locked and I can't change them. I've tried to work around this by using a temp table in my query as Sam suggested.

var grid = QueryMultiple(@"set nocount on 
declare @t table(Id int, Name nvarchar(max), AnotherId int)

insert @t
exec proc

set nocount off 
select Id, Name from @t
select Id, AnotherId from @t
");

However, I've now discovered the original SP also contains an insert for logging and therefore SQL will not allow me to insert my sp into a temp table because of this.

There is mention of adding support for:

class Foo { [ColumnNumber(1)] public string Name {get;set;} }

How can I do this? Can someone point me in the right direction to modify Dapper source to not require column names and allow me to map by column number?


回答1:


The issue of non-trivial bindings keeps coming up; it came up most recently here: http://code.google.com/p/dapper-dot-net/issues/detail?id=108

My suggestion there stands, although I haven't had time to code it yet. I propose a static event that you can choose to handle, and apply whatever esoteric mappings you want, i.e.

SqlMapper.ColumnBind += (sender, args) {
    int colIndex = args.ColumnIndex;
    Type type = args.TargetType;
    MemberInfo member = // ... Entirely up to you to define logic
    args.Member = member;
};

The above is entirely theoretical, but would it meet your needs?

Note: if ColumnBind was not subscribed, it would just do normal basic name mapping as normal. Thoughts?

(note: it could also be a single event invoke per type, rather than per-column; that might be more efficient, as the subscriber will call GetProperties etc less often)



来源:https://stackoverflow.com/questions/11703600/dapper-column-number-rather-than-column-name

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