This article is a mirror article of machine translation, please click here to jump to the original article.

View: 107092|Reply: 17

[Source] The ORM framework Dapper is simple to use

[Copy link]
Posted on 12/29/2020 11:58:20 AM | | | |
Although I hate writing SQL statements, sometimes I have to use Dapper to do some simple operations on the database, which is better than just encapsulating dbhelper.

Dapper determines whether the data exists

Dapper performs the update operation



Dapper puts the query results into the DataTable

Dapper insert action

For example:

Dapper query actions







Previous:SQL Server performance-optimized index fragmentation
Next:SQL Server uses comma separating strings
 Landlord| Posted on 1/27/2021 10:23:38 AM |
dapper implements optimistic locks

Add the RowVersion field to the database table, the type is timestamp, and the principle: When updating, use RowVersion as the where condition, as shown in the figure below:



The code is as follows:





 Landlord| Posted on 12/10/2021 2:37:28 PM |
dapper dynamically passes parameters

When dapper executes the command to dynamically pass parameters, I start to pass the object as a parameter through the JSON string, and the error is as follows:

The output error:An enumerable sequence of parameters (arrays, lists, etc) is not allowed in this context.

You can pass dynamic parameters in the following ways:



 Landlord| Posted on 12/4/2025 4:55:34 PM |
Unhandled exception. System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
   at System.Data.DataTable.EnableConstraints()
   at System.Data.DataTable.set_EnforceConstraints(Boolean value)
   at System.Data.DataTable.EndLoadData()
   at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
   at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)

solution
 Landlord| Posted on 1/27/2021 4:59:36 PM |
dapper  select count(*)

Get the total number of data pieces, the code is as follows:



 Landlord| Posted on 1/28/2021 1:10:05 PM |
dapper like fuzzy query



 Landlord| Posted on 3/26/2021 2:46:40 PM |
dapper in update

 Landlord| Posted on 7/14/2021 3:09:24 PM |
Use Dapper to return multiple result sets




Note: When reading the table is retrieved, it must be in the order of returning the table above. For example, the Users table queried above must be returned first, otherwise there is no data returned
 Landlord| Posted on 12/27/2021 10:10:02 PM |
Microsoft introduces Microsoft.Data.SqlClient as an alternative to System.Data.SqlClient
Posted on 12/28/2021 2:26:29 PM |
Being used, learn it
Posted on 1/17/2022 5:33:51 PM |
At my last company, I changed all of my company's data layer to dapper. It feels very good, because almost all my colleagues in the company who used to use ado.net have typed SQL statements, and after that, it was very smooth.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com