dapper

How to map to a Dictionary object from database results using Dapper Dot Net?

这一生的挚爱 提交于 2019-11-28 17:10:06
If I have a simple query such as: string sql = "SELECT UniqueString, ID FROM Table"; and I want to map it to a dictionary object such as: Dictionary<string, int> myDictionary = new Dictionary<string, int>(); How would I do this with Dapper? I assume it is something like: myDictionary = conn.Query<string, int>(sql, new { }).ToDictionary(); But can't figure out the proper syntax. There's various ways already shown; personally I'd just use the non-generic api: var dict = conn.Query(sql, args).ToDictionary( row => (string)row.UniqueString, row => (int)row.Id); Tim Schmelter Works also without an

Inserting an IEnumerable<T> collection with Dapper errors out with “class is not supported by Dapper.”

五迷三道 提交于 2019-11-28 17:01:50
Yep, there are questions here and here about how to insert records with dapper-dot-net. However, the answers, while informative, didn't seem to point me in the right direction. Here is the situation: moving data from SqlServer to MySql. Reading the records into an IEnumerable<WTUser> is easy, but I am just not getting something on the insert. First, the 'moving records code': // moving data Dim session As New Session(DataProvider.MSSql, "server", _ "database") Dim resources As List(Of WTUser) = session.QueryReader(Of WTUser)("select * from tbl_resource") session = New Session(DataProvider

Does Dapper support the like operator?

二次信任 提交于 2019-11-28 16:36:48
问题 Using Dapper-dot-net... The following yields no results in the data object: var data = conn.Query(@" select top 25 Term as Label, Type, ID from SearchTerms WHERE Term like '%@T%'", new { T = (string)term }); However, when I just use a regular String Format like: string QueryString = String.Format("select top 25 Term as Label, Type, ID from SearchTerms WHERE Term like '%{0}%'", term); var data = conn.Query(QueryString); I get 25 rows back in the collection. Is Dapper not correctly parsing the

How to use transactions with dapper.net?

孤街醉人 提交于 2019-11-28 16:16:47
I would like to run multiple insert statements on multiple tables. I am using dapper.net. I don't see any way to handle transactions with dapper.net. Please share your ideas on how to use transactions with dapper.net. Here the code snippet: using System.Transactions; .... using (var transactionScope = new TransactionScope()) { DoYourDapperWork(); transactionScope.Complete(); } Note that you need to add reference to System.Transactions assembly because it is not referenced by default. I preferred to use a more intuitive approach by getting the transaction directly from the connection: // This

Problems connecting to local MySQL server

隐身守侯 提交于 2019-11-28 13:14:27
I have problems connecting to a local MySQL database. I made sure the server is running and i can connect to the server using the user and password from the connection string and create databases with the terminal. I'm using visual studio community on a mac and MySQL 8.0.16. Example: Program.cs using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace ReadSQL { class Program { static void Main(string[] args) { DataAccess dataAccess = new DataAccess(); dataAccess.GetTask(); } } } DataAccess.cs using System; using System.Collections.Generic; using

.net core dapper (2)

扶醉桌前 提交于 2019-11-28 12:51:24
0.0 今天还是简单的更一篇( 在 睡梦中被叫醒强迫更新的一篇 )   人生得意须尽欢,莫使金樽空对月 1. 简单的插入数据,然后查询整个表 1 public static void Main(string[] args) 2 { 3 var connectionString = "Server=.;Database=Demo;User Id=sa;Password = keno;"; 4 5 //02 insert 6 var customerModel = new Customer { CustomerName = "张三", Address = "北京", City = "北京", Country = "中国", PostalCode = "121212" }; 7 using (var connection = new SqlConnection(connectionString)) 8 { 9 string sql = "insert into [dbo].[Customer]([CustomerName],[Address],[City],[PostalCode],[Country]) values(@CustomerName,@Address,@City,@PostalCode,@Country)"; 10 var addRow = connection

Generic QueryMultiple with Dapper

孤街醉人 提交于 2019-11-28 11:50:04
问题 I have a base repository with a Generic Get method to return Data using Dapper like public T Get<T>(Func<IDbConnection, T> query) { using (IDbConnection db = new SqlConnection(ConfigurationManager.ConnectionStrings["myDB"].ConnectionString)) { return query.Invoke(db); } } However I now have the need to return multiple Data. The DAL query is as below: var multi = db.QueryMultiple(getCarDataSp , new { CustomerID = customerId, Year = year }, commandType: CommandType.StoredProcedure)); var cars =

Is there any way to trace\\log the sql using Dapper?

狂风中的少年 提交于 2019-11-28 08:54:16
Is there a way to dump the generated sql to the Debug log or something? I'm using it in a winforms solution so the mini-profiler idea won't work for me. I got the same issue and implemented some code after doing some search but having no ready-to-use stuff. There is a package on nuget MiniProfiler.Integrations I would like to share. Update V2 : it supports to work with other database servers, for MySQL it requires to have MiniProfiler.Integrations.MySql Below are steps to work with SQL Server: 1.Instantiate the connection var factory = new SqlServerDbConnectionFactory(_connectionString); using

Using Dapper with SQL Spatial Types as a parameter

做~自己de王妃 提交于 2019-11-28 07:46:51
I've got a system which basically has to do a query like this: SELECT * FROM MyTable WHERE @parameter.STIntersects(MyGeometryColumn) This is quite simple to do when using vanilla SQL parameters, you just have to create your parameter in a non-typical way (where the builder variable is a SqlGeometryBuilder which I use to create a rectangle): command.Parameters.Add(new SqlParameter { UdtTypeName = "geometry", Value = builder.ConstructedGeometry, ParameterName = "@paremeter" }); Now, When I try to do this using dapper, I get an error that it can't figure out how to use this as a parameter. Anyone

Ignore property on model property

流过昼夜 提交于 2019-11-28 06:58:40
问题 How can I ignore a property on my model using dapper/dapper extensions/dapper rainbow or any of those dapper libraries? 回答1: Dapper creator Sam Saffron has addressed this requirement in response to another SO user's questions here. Check it out. Also, if you want to use the Dapper Extensions library that Sam has mentioned in his answer, you can get it from Github or via Nuget. Here's an example of ignoring properties from the Library's Test Project. using System; using System.Collections