c# - Dapper & MS Access - Read works, Write doesn't -
let's start getting out of way: i'm stuck using ms access db , can't change it.
this works fine:
using (oledbconnection conn = connectionhelper.getconnection()) { conn.open(); var results = conn.query<string>( "select firstname students lastname = @lastname", new { lastname= "smith" } ); conn.close(); }
this works fine:
using (oledbconnection conn = connectionhelper.getconnection()) { oledbcommand cmd = new oledbcommand( "update students set firstname = @firstname, city = @city lastname = @lastname", conn ); cmd.parameters.addwithvalue("firstname", "john"); cmd.parameters.addwithvalue("city", "somecity"); cmd.parameters.addwithvalue("lastname", "smith"); conn.open(); var result = cmd.executenonquery(); conn.close(); }
this doesn't... executes without error sets firstname "somecity" in db , city "john":
using (oledbconnection conn = connectionhelper.getconnection()) { conn.open(); var results = conn.query<string>( "update students set firstname = @firstname, city = @city lastname = @lastname", new { firstname = "john", city = "somecity", lastname = "smith" } ); conn.close(); }
any ideas?
edit below
dapper works if use dynamicparameters:
using (oledbconnection conn = connectionhelper.getconnection()) { dynamicparameters parameters = new dynamicparameters(); parameters.add("firstname", "john"); parameters.add("city", "somecity"); parameters.add("lastname", "smith"); conn.open(); var result = conn.query<string>( "update students set firstname = @firstname, city = @city lastname = @lastname", parameters ); conn.close(); }
after digging, able find cause:
below createparaminfogenerator delegate dapper's sqlmapper:
public static action<idbcommand, object> createparaminfogenerator(identity identity) { // code above here ienumerable<propertyinfo> props = type.getproperties().orderby(p => p.name);
the props unanimous param gets re-ordered orderby(p => p.name), moves city upfront.
new { firstname = "john", city = "somecity", lastname = "smith" }
props being added idbcommand parameters order important.
if comment out orderby() clause, works.
i tested dynamicparameters , intentionally re-ordered attributes move city upfront:
var parameters = new dynamicparameters(); parameters.add("city", "somecity"); parameters.add("firstname", "john"); parameters.add("lastname", "smith"); var result = dbconnection.query<string>( "update students set firstname = @firstname, city = @city lastname = @lastname", parameters );
the above did not work well, order of attributes reason!
i guess can modify local copy of sqlmapper , remove orderby() , wait official verdict marc...
hope helps.
Comments
Post a Comment