dapper

What causes “extension methods cannot be dynamically dispatched” here?

混江龙づ霸主 提交于 2019-11-26 17:49:46
问题 Compile Error 'System.Data.SqlClient.SqlConnection' has no applicable method named 'Query' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. Now, I know how to work around the problem, but I'm trying to get a better understanding of the error itself. I have class that I'm building to leverage Dapper. In the end I'm going to provide

Passing query parameters in Dapper using OleDb

旧街凉风 提交于 2019-11-26 17:12:14
问题 This query produces an error No value given for one or more required parameters : using (var conn = new OleDbConnection("Provider=...")) { conn.Open(); var result = conn.Query( "select code, name from mytable where id = ? order by name", new { id = 1 }); } If I change the query string to: ... where id = @id ... , I will get an error: Must declare the scalar variable "@id". How do I construct the query string and how do I pass the parameter? 回答1: The following should work: var result = conn

C# MySQL,Dapper Trans,list

大憨熊 提交于 2019-11-26 16:59:25
static async Task MySQLTransDemo() { try { using(dbConnection) { dbConnection.Open(); //The object matched type such as class must have the column fields in table List<object> objList = new List<object>(); //assign values to objList; //to-do //objList=GetObjList(); using(IDbTransaction trans=dbConnection.BeginTransaction()) { //match columns and values respectively. string transSQL = "insert into table_Name(column1,column2) values(@column1,@column2)"; //In this example,this objList collection's item object must have column1 and column2 properties. var transNum = await dbConnection.ExecuteAsync

SELECT * FROM X WHERE id IN (…) with Dapper ORM

百般思念 提交于 2019-11-26 15:05:39
What is the best way to write a query with IN clause using Dapper ORM when the list of values for the IN clause is coming from business logic? For example let's say I have a query: SELECT * FROM SomeTable WHERE id IN (commaSeparatedListOfIDs) The commaSeparatedListOfIDs is being passed in from business logic and it can be any type of IEnumerable(of Integer) . How would I construct a query in this case? Do I have to do what I've been doing so far which is basically string concatenation or is there some sort of advanced parameter mapping technique that I'm not aware of? LukeH Dapper supports

How to log/get a SQL query auto-generated by Dapper Extensions?

纵然是瞬间 提交于 2019-11-26 14:34:51
问题 I am using Dapper Extensions (DE) as ORM. It is consumed in Data Access Layer which is implemented using Repository pattern. SQL Express is back-end RDBMS. DE automatically generates most of the queries for me. I want to log those auto-generated queries for debugging purpose. There are two ways I can see to achieve this: - Get the SQL query generated by DE (before or after it is executed) and write it to log. This is preferred way for me as I already have my logging module (using log4net) in

Does Dapper support SQL 2008 Table-Valued Parameters?

妖精的绣舞 提交于 2019-11-26 14:21:19
Does anyone know if is possible to pass table-valued parameter data to a stored procedure with Dapper? There is now (n Dapper 1.26 and higher) direct support for table-valued parameters baked into dapper. In the case of stored procedures, since the data type is built into the sproc API, all you need to do is supply a DataTable : var data = connection.Query<SomeType>(..., new { id=123, name="abc", values = someTable }, ...); For direct command-text you have two other options: use a helper method to tell it the custom data type: var data = connection.Query<SomeType>(..., new { id=123, name="abc"

How do I map lists of nested objects with Dapper

自闭症网瘾萝莉.ら 提交于 2019-11-26 14:05:25
I'm currently using Entity Framework for my db access but want to have a look at Dapper. I have classes like this: public class Course{ public string Title{get;set;} public IList<Location> Locations {get;set;} ... } public class Location{ public string Name {get;set;} ... } So one course can be taught at several locations. Entity Framework does the mapping for me so my Course object is populated with a list of locations. How would I go about this with Dapper, is it even possible or do I have to do it in several query steps? Sam Saffron Dapper is not a full blown ORM it does not handle magic

Multi-Mapper to create object hierarchy

谁说胖子不能爱 提交于 2019-11-26 13:58:53
I've been playing around with this for a bit, because it seems like it feels a lot like the documented posts/users example , but its slightly different and isn't working for me. Assuming the following simplified setup (a contact has multiple phone numbers): public class Contact { public int ContactID { get; set; } public string ContactName { get; set; } public IEnumerable<Phone> Phones { get; set; } } public class Phone { public int PhoneId { get; set; } public int ContactID { get; set; } // foreign key public string Number { get; set; } public string Type { get; set; } public bool IsActive {

Closing connection when using Dapper

六眼飞鱼酱① 提交于 2019-11-26 13:53:24
问题 Is it necessary to close connection once query is executed explicitly calling Close method or putting the connection within Using statement? Would leaving connection open lead to connection reuse and improve SQL performance for future queries? 回答1: I am assuming that you are using latest version of Dapper. With Dapper, there are two ways to manage connection: Fully manage yourself: Here, you are fully responsible for opening and closing connection. This is just like how you treat connection

Dapper.NET and stored proc with multiple result sets

不想你离开。 提交于 2019-11-26 13:04:49
Is there any way to use Dapper.NET with stored procs that return multiple result sets? In my case, the first result set is a single row with a single column; if it's 0 then the call was successful, and the second result set will contain that actual rows/columns of data. (and if it was non-zero, an error occured and no second result set will be provided) Any chance to handle this with Dapper.NET? So far, I'm only ever getting back that single 0 - but nothing more. Update: OK, it works fine - as long as the result set no. 2 is a single entity: Dapper.SqlMapper.GridReader reader = _conn