1. Install Dapper
First, you need to install Dapper through the NuGet package manager. You can use the NuGet Package Manager console to execute the following commands:
Install-Package Dapper
Dapper also supports these platforms if you are using newer versions such as .NET Core or .NET 5/6/7.
2. Configure database connections
Next, you need to configure the database connection. This usually involves setting up a database connection string and creating a connection object to the database. Dapper itself does not handle connection management, but can use(For SQL Server) or other database providers to create a connection.
string connectionString = "Database Link"; using (var connection = new SqlConnection(connectionString)) { // Perform database operations here}
3. Execute SQL query
With Dapper, SQL queries can be executed and results mapped to objects. For example, suppose there is a nameUsers
database table, want to query all users:
string sql = "SELECT * FROM Users"; using (var connection = new SqlConnection(connectionString)) { var users = <User>(sql); // User is a class that matches the database table structure foreach (var user in users) { (); } }
In the above code,Query
Methods execute SQL query and map each row result to oneUser
Object.User
The class should have an attribute that matches the columns in the database table.
4. Perform parameterized query
To prevent SQL injection attacks, parameterized queries should always be used. Dapper supports named parameters and anonymous objects as parameters:
string sql = "SELECT * FROM Users WHERE Age > @Age"; int ageThreshold = 30; using (var connection = new SqlConnection(connectionString)) { var users = <User>(sql, new { Age = ageThreshold }); foreach (var user in users) { (); } }
Alternatively, you can use named parameters:
string sql = "SELECT * FROM Users WHERE Age > @age"; using (var connection = new SqlConnection(connectionString)) { var users = <User>(sql, new { age = 30 }); foreach (var user in users) { (); } }
Note that Dapper is case-insensitive to parameter names, but it is best to maintain consistency.
5. Perform insert, update and delete operations
In addition to query, Dapper also supports insert, update and delete operations. AvailableExecute
Methods to do these operations:
string insertSql = "INSERT INTO Users (Name, Age) VALUES (@Name, @Age)"; using (var connection = new SqlConnection(connectionString)) { var rowsAffected = (insertSql, new { Name = "John Doe", Age = 25 }); ($"Rows affected: {rowsAffected}"); }
6. Use transactions
Dapper also supports transactions. AvailableBeginTransaction
Method to start a transaction and commit or rollback after completion:
using (var connection = new SqlConnection(connectionString)) { using (var transaction = ()) { try { // Perform a series of database operations // ... // Submit transaction (); } catch { // Roll back the transaction (); throw; } } }
This is the article about the process steps of using Dapper for database access in C#. For more related content on C# Dapper database access, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!