-
Notifications
You must be signed in to change notification settings - Fork 5
Microsoft.Data.SqlClient
Microsoft.Data.SqlClient provides data access for Microsoft SQL Server and Azure SQL Database. Sure, let's walk through each method in the
AdoDotNetExample
class to summarize its functionality:
public AdoDotNetExample(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 Read()
{
using (SqlConnection connection = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString))
{
connection.Open();
Console.WriteLine("Connection open.");
string query = "SELECT * FROM tbl_blog";
using (SqlCommand cmd = new SqlCommand(query, connection))
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sqlDataAdapter.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Console.WriteLine($"Blog Id => {dr["BlogId"]}");
Console.WriteLine($"Blog Title => {dr["BlogTitle"]}");
Console.WriteLine($"Blog Author => {dr["BlogAuthor"]}");
Console.WriteLine($"Blog Content => {dr["BlogContent"]}");
Console.WriteLine("--------------------------------");
}
}
Console.WriteLine("Connection close.");
}
}
Summary: This method connects to the database, executes a SELECT
query to retrieve all rows from the tbl_blog
table, and prints each row's data to the console.
public void Edit(string id)
{
using (SqlConnection connection = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString))
{
connection.Open();
string query = "SELECT * FROM tbl_blog WHERE BlogId = @BlogId";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@BlogId", id);
using (SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sqlDataAdapter.Fill(dt);
if (dt.Rows.Count == 0)
{
Console.WriteLine("No data found.");
return;
}
DataRow dr = dt.Rows[0];
Console.WriteLine($"Blog Id => {dr["BlogId"]}");
Console.WriteLine($"Blog Title => {dr["BlogTitle"]}");
Console.WriteLine($"Blog Author => {dr["BlogAuthor"]}");
Console.WriteLine($"Blog Content => {dr["BlogContent"]}");
Console.WriteLine("--------------------------------");
}
}
}
}
Summary: This method retrieves a specific blog entry based on the provided id
, prints the details if found, or indicates that no data was found.
public void Create(string title, string author, string content)
{
using (SqlConnection connection = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString))
{
connection.Open();
string query = @"INSERT INTO [dbo].[Tbl_Blog]
([BlogTitle], [BlogAuthor], [BlogContent])
VALUES (@BlogTitle, @BlogAuthor, @BlogContent)";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@BlogTitle", title);
cmd.Parameters.AddWithValue("@BlogAuthor", author);
cmd.Parameters.AddWithValue("@BlogContent", content);
int result = cmd.ExecuteNonQuery();
Console.WriteLine(result > 0 ? "Saving Successful." : "Saving Failed.");
}
}
}
Summary: This method inserts a new blog entry into the Tbl_Blog
table with the provided title
, author
, and content
, and indicates whether the save operation was successful.
public void Update(int id, string title, string author, string content)
{
using (SqlConnection connection = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString))
{
connection.Open();
string query = @"UPDATE [dbo].[Tbl_Blog]
SET [BlogTitle] = @BlogTitle,
[BlogAuthor] = @BlogAuthor,
[BlogContent] = @BlogContent
WHERE BlogId = @BlogId";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@BlogId", id);
cmd.Parameters.AddWithValue("@BlogTitle", title);
cmd.Parameters.AddWithValue("@BlogAuthor", author);
cmd.Parameters.AddWithValue("@BlogContent", content);
int result = cmd.ExecuteNonQuery();
Console.WriteLine(result > 0 ? "Updating Successful." : "Updating Failed.");
}
}
}
Summary: This method updates an existing blog entry in the Tbl_Blog
table based on the provided id
, title
, author
, and content
, and indicates whether the update operation was successful.
public void Delete(int id)
{
using (SqlConnection connection = new SqlConnection(_sqlConnectionStringBuilder.ConnectionString))
{
connection.Open();
string query = @"DELETE FROM Tbl_Blog WHERE BlogId = @BlogId";
using (SqlCommand cmd = new SqlCommand(query, connection))
{
cmd.Parameters.AddWithValue("@BlogId", id);
int result = cmd.ExecuteNonQuery();
Console.WriteLine(result > 0 ? "Deleting Successful." : "Deleting Failed.");
}
}
}
Summary: This method deletes a blog entry from the Tbl_Blog
table based on the provided id
, and 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.