https://github.com/aidrecabrera/dapper-example
An example program of the communication between C# and MySQL using Dapper
https://github.com/aidrecabrera/dapper-example
Last synced: 10 months ago
JSON representation
An example program of the communication between C# and MySQL using Dapper
- Host: GitHub
- URL: https://github.com/aidrecabrera/dapper-example
- Owner: aidrecabrera
- Created: 2023-10-07T14:39:29.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2023-10-09T14:29:14.000Z (about 2 years ago)
- Last Synced: 2025-01-26T05:14:31.914Z (11 months ago)
- Language: C#
- Size: 19.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Employees - Dapper Example Program
This is a simple C# program that demonstrates the usage of [Dapper](https://github.com/DapperLib/Dapper), a micro ORM (Object-Relational Mapping) for .NET, by performing basic database operations with a MySQL database. In this program, we create an `Employee` class and use Dapper to perform CRUD (Create, Read, Update, Delete) operations on a MySQL database table named `employees`.
## Prerequisites
Before running this program, make sure you have the following prerequisites:
- [.NET SDK](https://dotnet.microsoft.com/download)
- [MySQL Server](https://dev.mysql.com/downloads/installer/) with a database named `dapper`.
- MySQL user credentials (`Uid` and `Pwd`) with appropriate permissions to access the database.
## Installation and Setup
1. Clone or download this repository to your local machine.
2. Open the project in your preferred C# IDE or text editor.
## Configuration
In the `Program.cs` file, you will find a `connectionString` variable. Modify this variable to specify the connection details for your MySQL database:
```csharp
string connectionString = "Server=127.0.0.1;Port=3306;Database=dapper;Uid=your_username;Pwd=your_password;";
```
Replace `your_username` and `your_password` with your MySQL user credentials.
## Database Schema
The MySQL database `dapper` used in this program contains a single table named `employees`. Here's the schema for the `employees` table:
```sql
CREATE TABLE employees (
Id INT AUTO_INCREMENT PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
Age INT NOT NULL,
Salary DECIMAL(10, 2) NOT NULL
);
```
- `Id`: A unique identifier for each employee, automatically incremented.
- `Name`: The name of the employee (a string).
- `Age`: The age of the employee (an integer).
- `Salary`: The salary of the employee (a decimal number).
## Usage
This program performs the following CRUD operations on the `employees` table:
1. **Creating Data (C - Create)**: It inserts an employee record into the `employees` table with a name, age, and salary. You can customize the data in the `Employee` object:
```csharp
var employee = new Employee
{
Name = "Aidre",
Age = 30,
Salary = 130000
};
string insertQuery = "INSERT INTO employees (Name, Age, Salary) VALUES (@Name, @Age, @Salary)";
connection.Execute(insertQuery, employee);
```
2. **Reading Data (R - Read)**: It retrieves all employee records from the `employees` table and stores them in a list of `Employee` objects.
```csharp
string selectQuery = "SELECT * FROM employees";
List employees = connection.Query(selectQuery).ToList();
```
3. **Updating Data (U - Update)**: It updates the details of an existing employee in the `employees` table. Modify the properties of the `Employee` object and execute an update query to apply changes.
```csharp
// Example: Updating the salary of an employee
employee.Salary = 140000;
string updateQuery = "UPDATE employees SET Salary = @Salary WHERE Id = @Id";
connection.Execute(updateQuery, employee);
```
4. **Deleting Data (D - Delete)**: It deletes an employee record from the `employees` table based on the employee's `Id`.
```csharp
// Example: Deleting an employee by Id
int employeeIdToDelete = 1;
string deleteQuery = "DELETE FROM employees WHERE Id = @Id";
connection.Execute(deleteQuery, new { Id = employeeIdToDelete });
```
```csharp
using MySql.Data.MySqlClient;
using Dapper;
namespace Dapper_Introduction
{
internal class Program
{
static void Main(string[] args)
{
string connectionString = "Server=127.0.0.1;Port=3306;Database=dapper;Uid=aidrecabrera;Pwd=aidrecabrera;";
MySqlConnection connection = new MySqlConnection(connectionString);
var employee = new Employee
{
Name = "Aidre",
Age = 30,
Salary = 130000
};
string insertQuery = "INSERT INTO employees (Name, Age, Salary) VALUES (@Name, @Age, @Salary)";
connection.Execute(insertQuery, employee);
string selectQuery = "SELECT * FROM employees";
List employees = connection.Query(selectQuery).ToList();
employee.Salary = 140000;
string updateQuery = "UPDATE employees SET Salary = @Salary WHERE Id = @Id";
connection.Execute(updateQuery, employee);
int employeeIdToDelete = 1;
string deleteQuery = "DELETE FROM employees WHERE Id = @Id";
connection.Execute(deleteQuery, new { Id = employeeIdToDelete });
}
}
}
```
## Running the Program
1. Build the project using your IDE or by running `dotnet build` in the project directory.
2. Run the program using your IDE or by executing `dotnet run` in the project directory.
That's all! :)