The CASE statement in SQL allows you to implement conditional logic within queries, making it a powerful tool for handling complex data transformations and classifications.
CASE
The CASE statement works like an IF-ELSE structure, evaluating conditions and returning corresponding values:
IF-ELSE
SELECT column_name, CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 ELSE default_result END AS alias_name FROM table_name;
Assume we have an Employees table with an Age column, and we want to categorize employees based on their age groups:
Employees
Age
SELECT Name, Age, CASE WHEN Age < 25 THEN 'Young' WHEN Age BETWEEN 25 AND 40 THEN 'Mid-Age' ELSE 'Senior' END AS AgeCategory FROM Employees;
CASE is often used in aggregate functions to perform conditional counting or summing:
SELECT SUM(CASE WHEN Age < 25 THEN 1 ELSE 0 END) AS YoungCount, SUM(CASE WHEN Age BETWEEN 25 AND 40 THEN 1 ELSE 0 END) AS MidAgeCount, SUM(CASE WHEN Age > 40 THEN 1 ELSE 0 END) AS SeniorCount FROM Employees;
You can use CASE to customize sorting order dynamically:
SELECT Name, Age FROM Employees ORDER BY CASE WHEN Age < 25 THEN 1 WHEN Age BETWEEN 25 AND 40 THEN 2 ELSE 3 END;
The CASE statement is a versatile tool in SQL for implementing conditional logic in SELECT, WHERE, ORDER BY, and aggregate functions. It enhances query flexibility, making data classification and transformation more efficient.
SELECT
WHERE
ORDER BY
Closing a SqlDataReader correctly prevents memory leaks, connection issues, and unclosed resources. Here’s the best way to do it.
Using using statements ensures SqlDataReader and SqlConnection are closed even if an exception occurs.
Example
using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); using (SqlCommand cmd = new SqlCommand("SELECT * FROM Users", conn)) using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Console.WriteLine(reader["Username"]); } } // ✅ Auto-closes reader here } // ✅ Auto-closes connection here
This approach auto-closes resources when done and it is cleaner and less error-prone than manual closing.
If you need explicit control, you can manually close it inside a finally block.
SqlDataReader? reader = null; try { using SqlConnection conn = new SqlConnection(connectionString); conn.Open(); using SqlCommand cmd = new SqlCommand("SELECT * FROM Users", conn); reader = cmd.ExecuteReader(); while (reader.Read()) { Console.WriteLine(reader["Username"]); } } finally { reader?.Close(); // ✅ Closes reader if it was opened }
This is slightly more error prone if you forget to add a finally block. But might make sense when you need to handle the reader separately from the command or connection.
Measuring the execution time of C# methods is essential for performance optimization and identifying bottlenecks in your application.
The most straightforward approach uses the Stopwatch class from the System.Diagnostics namespace, which provides high-precision timing capabilities.
Stopwatch
System.Diagnostics
This approach is perfect for quick performance checks during development or when troubleshooting specific methods in production code.
Here's a practical example: Imagine you have a method that processes a large dataset and you want to measure its performance.
First, add using System.Diagnostics; to your imports. Then implement timing as shown below:
using System.Diagnostics;
public void MeasurePerformance() { Stopwatch stopwatch = new Stopwatch(); // Start timing stopwatch.Start(); // Call the method you want to measure ProcessLargeDataset(); // Stop timing stopwatch.Stop(); // Get the elapsed time Console.WriteLine($"Processing time: {stopwatch.ElapsedMilliseconds} ms"); // Or use ElapsedTicks for higher precision Console.WriteLine($"Processing ticks: {stopwatch.ElapsedTicks}"); }
For more advanced scenarios, consider using the BenchmarkDotNet library, which offers comprehensive benchmarking with statistical analysis.
BenchmarkDotNet
Simply install the NuGet package, decorate methods with the [Benchmark] attribute, and run BenchmarkRunner.Run<YourBenchmarkClass>() to generate detailed reports comparing different implementation strategies.
[Benchmark]
BenchmarkRunner.Run<YourBenchmarkClass>()
Storing passwords as plain text is dangerous. Instead, you should hash them using a strong, slow hashing algorithm like BCrypt, which includes built-in salting and resistance to brute-force attacks.
Step 1: Install BCrypt NuGet Package
Before using BCrypt, install the BCrypt.Net-Next package:
dotnet add package BCrypt.Net-Next
or via NuGet Package Manager:
Install-Package BCrypt.Net-Next
Step 2: Hash a Password
Use BCrypt.HashPassword() to securely hash a password before storing it:
using BCrypt.Net; string password = "mySecurePassword123"; string hashedPassword = BCrypt.HashPassword(password); Console.WriteLine(hashedPassword); // Output: $2a$12$...
Step 3: Verify a Password
To check a user's login attempt, use BCrypt.Verify():
bool isMatch = BCrypt.Verify("mySecurePassword123", hashedPassword); Console.WriteLine(isMatch); // Output: True
Ensuring proper hashing should be at the top of your list when it comes to building authentication systems.
Register for my free weekly newsletter.