Skip to content

Commit

Permalink
Added default MS SQL connection construction
Browse files Browse the repository at this point in the history
  • Loading branch information
samadh90 committed Jul 21, 2022
1 parent 07321f6 commit 9ca9c8c
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions Connection/Connection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,41 @@ public class Connection
/// </summary>
private readonly DbType _dbType;

/// <summary>
/// Default connection MS SQL
/// </summary>
/// <param name="connectionString"></param>
/// <param name="providerFactory"></param>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="InvalidOperationException"></exception>
public Connection(string connectionString, DbProviderFactory providerFactory)
{
if (providerFactory is null)
{
throw new ArgumentException("providerFactory is not valid !");
}

if (string.IsNullOrWhiteSpace(connectionString))
{
throw new ArgumentException("connectionString is not valid !");
}

_connectionString = connectionString;
_providerFactory = providerFactory;

try
{
using (DbConnection dbConnection = CreateConnection())
{
dbConnection.Open();
}
}
catch (Exception)
{
throw new InvalidOperationException("the connection string is not valid or the server is down...");
}
}

/// <summary>
/// Create an instance of the <see cref="Connection"/> taking the <paramref name="connectionString"/>,
/// <paramref name="providerFactory"/> and <paramref name="dbType"/>
Expand Down Expand Up @@ -146,5 +181,13 @@ public DataSet GetDataSet(Command command)
throw new InvalidOperationException($"Provided database type is not valid!");
}
}

private DbConnection CreateConnection()
{
DbConnection dbConnection = _providerFactory.CreateConnection();
dbConnection.ConnectionString = _connectionString;

return dbConnection;
}
}
}

0 comments on commit 9ca9c8c

Please sign in to comment.