-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseConnection.cs
More file actions
122 lines (110 loc) · 4.58 KB
/
DatabaseConnection.cs
File metadata and controls
122 lines (110 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// C# Database Connection Template for Workshop Exercises
// This file demonstrates secure database connection patterns for the workshop
using System;
using System.Data.SqlClient;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
namespace WorkshopExercises
{
/// <summary>
/// Secure database connection class for workshop exercises
/// Demonstrates best practices for SQL Server connectivity
/// </summary>
public class DatabaseConnection
{
private readonly IConfiguration _configuration;
private readonly ILogger<DatabaseConnection> _logger;
private readonly string _connectionString;
public DatabaseConnection(IConfiguration configuration, ILogger<DatabaseConnection> logger)
{
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
// Get connection string from configuration
_connectionString = _configuration.GetConnectionString("DefaultConnection")
?? throw new InvalidOperationException("Connection string 'DefaultConnection' not found");
}
/// <summary>
/// Gets a secure SQL connection for workshop exercises
/// </summary>
/// <returns>Configured SqlConnection</returns>
public SqlConnection GetConnection()
{
try
{
var connection = new SqlConnection(_connectionString);
_logger.LogInformation("Database connection created successfully");
return connection;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to create database connection");
throw;
}
}
/// <summary>
/// Tests database connectivity for workshop setup
/// </summary>
/// <returns>True if connection successful</returns>
public async Task<bool> TestConnectionAsync()
{
try
{
using var connection = GetConnection();
await connection.OpenAsync();
_logger.LogInformation("Database connection test successful");
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Database connection test failed");
return false;
}
}
/// <summary>
/// Executes the workshop database setup script
/// </summary>
public async Task<bool> InitializeWorkshopDatabaseAsync()
{
const string initScript = @"
-- Check if tables already exist
IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='users' AND xtype='U')
BEGIN
-- Create Users table for workshop exercises
CREATE TABLE users (
id INT IDENTITY(1,1) PRIMARY KEY,
username NVARCHAR(50) NOT NULL UNIQUE,
email NVARCHAR(100) NOT NULL,
password_hash NVARCHAR(255) NOT NULL,
created_date DATETIME2 DEFAULT GETDATE(),
last_login DATETIME2 NULL,
is_active BIT DEFAULT 1
);
-- Insert sample data for exercises
INSERT INTO users (username, email, password_hash) VALUES
('alice.johnson', 'alice.johnson@example.com', 'hashed_password_1'),
('bob.smith', 'bob.smith@example.com', 'hashed_password_2'),
('charlie.brown', 'charlie.brown@example.com', 'hashed_password_3');
PRINT 'Workshop database initialized successfully';
END
ELSE
BEGIN
PRINT 'Workshop database already initialized';
END";
try
{
using var connection = GetConnection();
await connection.OpenAsync();
using var command = new SqlCommand(initScript, connection);
await command.ExecuteNonQueryAsync();
_logger.LogInformation("Workshop database initialization completed");
return true;
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to initialize workshop database");
return false;
}
}
}
}