-
Notifications
You must be signed in to change notification settings - Fork 5
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; }
}
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.
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).
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.
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.
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.
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.
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.
-
Introduction to C#
What is C#? -
Variables and Data Types
Understand how to declare and use variables. -
Operators
Arithmetic, relational, and logical operators in C#. -
Control Statements
If-else, switch, and looping constructs.
-
Classes and Objects
Basics of OOP in C#. -
Inheritance
How to use inheritance effectively. -
Polymorphism
Method overriding and overloading. -
Encapsulation
Understanding access modifiers.
-
LINQ Basics
Working with Language Integrated Query. -
Asynchronous Programming
Introduction to async/await. -
File Handling
Reading and writing files.
-
Number Formatting
Formatting numbers in C#. -
Exception Handling
Handling runtime errors effectively. -
Working with Dates
DateTime and time zone handling. -
Using Keyword in C#
Different usages of theusing
keyword in C#.
-
Setting Up Development Environment
Installing Visual Studio and .NET SDK. - Useful Libraries Libraries and tools for C# development.