Skip to content
Sann Lynn Htun edited this page Nov 22, 2024 · 3 revisions

Dapper

Dapper is a simple object mapper for .NET, making it easy to work with databases.

BlogModel.cs

public class BlogModel
{
    public int BlogId { get; set; }
    public string BlogTitle { get; set; }
    public string BlogAuthor { get; set; }
    public string BlogContent { get; set; }
}

1. Constructor

public DapperExample(SqlConnectionStringBuilder sqlConnectionStringBuilder)
{
    _sqlConnectionStringBuilder = sqlConnectionStringBuilder;
}

Summary: The constructor initializes the class with a SqlConnectionStringBuilder object, which is used to manage the database connection string.

2. Run Method

public void Run()
{
    Read();
    Create("title", "author", "content");
    Edit(1);
    Update(1, "title 2", "author 2", "content 2");
    Delete(1);
}

Summary: This method serves as an entry point for testing different operations. It calls the CRUD methods (Create, Read, Update, Delete).

3. Read Method

private void Read()
{
    using IDbConnection db = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString);
    List<BlogDto> blogs = db.Query<BlogDto>("SELECT * FROM tbl_blog").ToList();

    foreach (BlogDto blog in blogs)
    {
        Console.WriteLine($"Blog Id => {blog.BlogId}");
        Console.WriteLine($"Blog Title => {blog.BlogTitle}");
        Console.WriteLine($"Blog Author => {blog.BlogAuthor}");
        Console.WriteLine($"Blog Content => {blog.BlogContent}");
        Console.WriteLine("----------------------------");
    }
}

Summary: This method retrieves all rows from the tbl_blog table and prints each row's data to the console using the Dapper ORM for simplicity and efficiency.

4. Edit Method

private void Edit(int id)
{
    using IDbConnection db = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString);
    BlogDto blog = db.Query<BlogDto>("SELECT * FROM tbl_blog WHERE BlogId = @BlogId", new { BlogId = id }).FirstOrDefault();

    if (blog is null)
    {
        Console.WriteLine("No data found.");
        return;
    }

    Console.WriteLine($"Blog Id => {blog.BlogId}");
    Console.WriteLine($"Blog Title => {blog.BlogTitle}");
    Console.WriteLine($"Blog Author => {blog.BlogAuthor}");
    Console.WriteLine($"Blog Content => {blog.BlogContent}");
}

Summary: This method retrieves a specific blog entry based on the provided id and prints the details if found. If no entry is found, it indicates that no data was found.

5. Create Method

private void Create(string title, string author, string content)
{
    BlogDto blog = new BlogDto
    {
        BlogTitle = title,
        BlogAuthor = author,
        BlogContent = content
    };

    string query = @"INSERT INTO [dbo].[Tbl_Blog]
                     ([BlogTitle], [BlogAuthor], [BlogContent])
                     VALUES (@BlogTitle, @BlogAuthor, @BlogContent)";

    using IDbConnection db = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString);
    int result = db.Execute(query, blog);

    string message = result > 0 ? "Saving Successful." : "Saving Failed.";
    Console.WriteLine(message);
}

Summary: This method inserts a new blog entry into the Tbl_Blog table with the provided title, author, and content. It indicates whether the save operation was successful.

6. Update Method

private void Update(int id, string title, string author, string content)
{
    BlogDto blog = new BlogDto
    {
        BlogId = id,
        BlogTitle = title,
        BlogAuthor = author,
        BlogContent = content
    };

    string query = @"UPDATE [dbo].[Tbl_Blog]
                     SET [BlogTitle] = @BlogTitle,
                         [BlogAuthor] = @BlogAuthor,
                         [BlogContent] = @BlogContent
                     WHERE BlogId = @BlogId";

    using IDbConnection db = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString);
    int result = db.Execute(query, blog);

    string message = result > 0 ? "Updating Successful." : "Updating Failed.";
    Console.WriteLine(message);
}

Summary: This method updates an existing blog entry in the Tbl_Blog table based on the provided id, title, author, and content. It indicates whether the update operation was successful.

7. Delete Method

private void Delete(int id)
{
    string query = @"DELETE FROM [dbo].[Tbl_Blog] WHERE BlogId = @BlogId";

    using IDbConnection db = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString);
    int result = db.Execute(query, new { BlogId = id });

    string message = result > 0 ? "Deleting Successful." : "Deleting Failed.";
    Console.WriteLine(message);
}

Summary: This method deletes a blog entry from the Tbl_Blog table based on the provided id. It indicates whether the delete operation was successful.

C# Basics Wiki

Core Concepts

Object-Oriented Programming (OOP)

Advanced Topics

Miscellaneous

Tools and Resources

Clone this wiki locally