Dapper Contrib Update Discover How to Update Data From a Table
Description
When updating information in a database, you need to specify the name of the table and the name and value of each column in the SQL statement. The Dapper.Contrib library provides the Update method which is a pretty simple way of updating the data.
- With the
Updatemethod, we don't need to write any SQL statements, we just need to pass in the object that needs to be updated as a parameter to theUpdatemethod. - The
Updatemethod updates a given entity in the database.
Parameters
The syntax of the Update<T> method is given below:
public static bool Update<T>(T entityToUpdate, IDbTransaction transaction = null, int? commandTimeout = null)
The following table shows the different parameters of an Update method.
The Update method returns true if updated, and false if not found or not modified. With the Update method, you can update a single or many entities.
Dapper.Contrib's Update method is used to update an existing record in the database. The Update method takes an object as a parameter and updates the records in the database based on the property values of the object.
Example - Update Single
When you need to update a single record, you need to pass the object directly to the Update method that needs to be updated as shown in the below example.
// @nuget: Dapper.Contrib using Dapper.Contrib.Extensions; using (var connection = My.ConnectionFactory()) { connection.Open(); var isSuccess = connection.Update(new Invoice { InvoiceID = 1, Code = "Update_Single_1"}); }
Example - Update Many
If you need to update multiple records in the database then you can use the Update method and pass a list of objects that needs to be updated.
The following example shows how to update multiple objects by passing the list as an argument to the Update method.
// @nuget: Dapper.Contrib using Dapper.Contrib.Extensions; using (var connection = My.ConnectionFactory()) { connection.Open(); var list = new List<Invoice>() { new Invoice {InvoiceID = 1, Code = "Update_Many_1"}, new Invoice {InvoiceID = 2, Code = "Update_Many_2"}, new Invoice {InvoiceID = 3, Code = "Update_Many_3"} }; var isSuccess = connection.Update(list); }
Related Articles
ZZZ Projects
