using Microsoft.Data.SqlClient; namespace SwSL.EndPoints.ConnectionValidator { public class DbServerConnectionValidator : IDbServerConnectionValidator { public async Task ValidateConnectionAsync(string hostName,int port,string username,string password,CancellationToken ct) { if (string.IsNullOrWhiteSpace(hostName)) throw new ArgumentException("Server name is required."); if (string.IsNullOrWhiteSpace(username)) throw new ArgumentException("Database username is required."); if (string.IsNullOrWhiteSpace(password)) throw new ArgumentException("Database password is required."); string server = hostName; // SQL Server instance format if (port > 0) { server = $"{hostName},{port}"; } var builder = new SqlConnectionStringBuilder { DataSource = server, UserID = username, Password = password, // Same as SSMS IntegratedSecurity = false, TrustServerCertificate = true, Encrypt = false, ConnectTimeout = 30 }; try { await using var connection = new SqlConnection(builder.ConnectionString); await connection.OpenAsync(ct); } catch (SqlException ex) { throw new InvalidOperationException( $"Unable to connect SQL Server '{server}'. Please check ServerName, Username and Password.", ex); } } } }