Dapper Extension Ms Access System.Data.OleDb.OleDbException -
i started use dapper. dapper works fine. next step when tried integrate dapper extension. generates exception called system.data.oledb.oledbexception "additional information: characters found after end of sql statement."
why that? dapper extension doesn't support ms access (because of end character) or problem code or missing something. code below
using (var conn = new oledbconnection(@"provider=microsoft.ace.oledb.12.0;data source=myaccessfile.accdb;")) { conn.open(); conn.insert<person>(new person { name = "john stan", age = 20 }); }
according msdn article,
some database engines, such microsoft access jet database engine, not support output parameters , cannot process multiple statements in single batch.
so problem insert method generating statement such as
insert [person] ([person].[personname]) values (@personname); select cast(scope_identity() bigint) [id]
and access can't deal it.
reading around, seems various suggestions how insert-and-get-new-record-key when dealing access (that msdn article suggests second select statement) doesn't if you're using dapperextensions library, since generates query you.
so, basically, think correct in thinking dapperextensions won't work access.
on side note, had nightmare trying find out queries being generated. there various articles talk registry hack set "jetshowplan" value "on" couldn't make of them work. in end, created wrapped database connection , command classes queries captured on way out. in case of use in future, i'm including below..
the database connection initialisation code needs change - eg.
var connectionstring = "provider=microsoft.jet.oledb.4.0;data source=database2.mdb;"; using (var conn = new wrappeddbconnection(new oledbconnection(connectionstring))) { conn.insert<person>(new person { personname = "dan" }); }
and following 2 classes need defined -
public class wrappeddbconnection : idbconnection { private readonly idbconnection _conn; public wrappeddbconnection(idbconnection connection) { if (connection == null) throw new argumentnullexception(nameof(connection)); _conn = connection; } public string connectionstring { { return _conn.connectionstring; } set { _conn.connectionstring = value; } } public int connectiontimeout { { return _conn.connectiontimeout; } } public string database { { return _conn.database; } } public connectionstate state { { return _conn.state; } } public idbtransaction begintransaction() { return _conn.begintransaction(); } public idbtransaction begintransaction(isolationlevel il) { return _conn.begintransaction(il); } public void changedatabase(string databasename) { _conn.changedatabase(databasename); } public void close() { _conn.close(); } public idbcommand createcommand() { return new wrappeddbcommand(_conn.createcommand()); } public void dispose() { _conn.dispose(); } public void open() { _conn.open(); } } public class wrappeddbcommand : idbcommand { private readonly idbcommand _cmd; public wrappeddbcommand(idbcommand command) { if (command == null) throw new argumentnullexception(nameof(command)); _cmd = command; } public string commandtext { { return _cmd.commandtext; } set { _cmd.commandtext = value; } } public int commandtimeout { { return _cmd.commandtimeout; } set { _cmd.commandtimeout = value; } } public commandtype commandtype { { return _cmd.commandtype; } set { _cmd.commandtype = value; } } public idbconnection connection { { return _cmd.connection; } set { _cmd.connection = value; } } public idataparametercollection parameters { { return _cmd.parameters; } } public idbtransaction transaction { { return _cmd.transaction; } set { _cmd.transaction = value; } } public updaterowsource updatedrowsource { { return _cmd.updatedrowsource; } set { _cmd.updatedrowsource = value; } } public void cancel() { _cmd.cancel(); } public idbdataparameter createparameter() { return _cmd.createparameter(); } public void dispose() { _cmd.dispose(); } public int executenonquery() { console.writeline($"[executenonquery] {_cmd.commandtext}"); return _cmd.executenonquery(); } public idatareader executereader() { console.writeline($"[executereader] {_cmd.commandtext}"); return _cmd.executereader(); } public idatareader executereader(commandbehavior behavior) { console.writeline($"[executereader({behavior})] {_cmd.commandtext}"); return _cmd.executereader(); } public object executescalar() { console.writeline($"[executescalar] {_cmd.commandtext}"); return _cmd.executescalar(); } public void prepare() { _cmd.prepare(); } }
now, queries written console before being sent database.
Comments
Post a Comment