Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

.net - Dapper Column number rather than column name?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...