Created
June 7, 2024 16:56
-
-
Save JerryNixon/49fefce6728db3f0208a1df32fac0ad0 to your computer and use it in GitHub Desktop.
Validate a SQL Connection String
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SqlConnectionStringValidator | |
{ | |
public enum ErrorReason { None, ServerNetwork, Database, UserPassword, Other } | |
public static bool TryValidateConnectionString(string server, string database, string user, string password, out string connectionString, out ErrorReason reason, out string message) | |
{ | |
connectionString = default; | |
if (!TryValidateValues(server, database, user, password, out reason, out message)) | |
{ | |
return false; | |
} | |
if (!TryBuildConnectionString(server, database, user, password, out connectionString, out reason, out message)) | |
{ | |
return false; | |
} | |
if (!TryValidateConnectionString(connectionString, out reason, out message)) | |
{ | |
return false; | |
} | |
return reason == ErrorReason.None; | |
} | |
private static bool TryValidateConnectionString(string connectionString, out ErrorReason reason, out string message) | |
{ | |
reason = ErrorReason.None; | |
message = default; | |
try | |
{ | |
using var connection = new SqlConnection(connectionString); | |
connection.Open(); | |
} | |
catch (SqlException ex) when (new[] { 53, 10060, 11001 }.Contains(ex.Number)) | |
{ | |
reason = ErrorReason.ServerNetwork; | |
message = ex.Message; | |
} | |
catch (SqlException ex) when (ex.Number == 4060) | |
{ | |
reason = ErrorReason.Database; | |
message = ex.Message; | |
} | |
catch (SqlException ex) when (ex.Number == 18456) | |
{ | |
reason = ErrorReason.UserPassword; | |
message = ex.Message; | |
} | |
catch (Exception ex) | |
{ | |
reason = ErrorReason.Other; | |
message = ex.Message; | |
} | |
return reason == ErrorReason.None; | |
} | |
private static bool TryBuildConnectionString(string server, string database, string user, string password, out string connectionString, out ErrorReason reason, out string message) | |
{ | |
connectionString = default; | |
reason = ErrorReason.None; | |
message = default; | |
try | |
{ | |
connectionString = new SqlConnectionStringBuilder | |
{ | |
DataSource = server, | |
InitialCatalog = database, | |
UserID = user, | |
Password = password | |
}.ConnectionString; | |
} | |
catch (Exception ex) | |
{ | |
reason = ErrorReason.Other; | |
message = ex.Message; | |
} | |
return reason == ErrorReason.None; | |
} | |
private static bool TryValidateValues(string server, string database, string user, string password, out ErrorReason reason, out string message) | |
{ | |
reason = ErrorReason.None; | |
message = default; | |
if (string.IsNullOrWhiteSpace(server)) | |
{ | |
reason = ErrorReason.ServerNetwork; | |
message = "Server name is required"; | |
} | |
else if (string.IsNullOrWhiteSpace(database)) | |
{ | |
reason = ErrorReason.Database; | |
message = "Database name is required"; | |
} | |
else if (string.IsNullOrWhiteSpace(user)) | |
{ | |
reason = ErrorReason.UserPassword; | |
message = "User name is required"; | |
} | |
else if (string.IsNullOrWhiteSpace(password)) | |
{ | |
reason = ErrorReason.UserPassword; | |
message = "Password is required"; | |
} | |
return reason == ErrorReason.None; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment