/// <summary> /// Opens an SQL connection with an instance of the created <see cref="ThrowawayDatabase"/>. /// </summary> /// <param name="this">An instance of <see cref="ThrowawayDatabase"/></param> /// <returns>A new instance of the SQL connection</returns> public static SqlConnection OpenConnection(this ThrowawayDatabase @this) { var connection = new SqlConnection(@this.ConnectionString); connection.Open(); return(connection); }
/// <summary> /// Opens an SQL connection with an instance of the created <see cref="ThrowawayDatabase"/>. /// </summary> /// <param name="this">An instance of <see cref="ThrowawayDatabase"/></param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> for opening the SQL connection task</param> /// <returns>Task representing an asynchronous operation</returns> public static async Task <SqlConnection> OpenConnectionAsync(this ThrowawayDatabase @this, CancellationToken cancellationToken = default) { var connection = new SqlConnection(@this.ConnectionString); await connection.OpenAsync(cancellationToken) .ConfigureAwait(false); return(connection); }
/// <summary> /// Creates a throwaway database using the connection string provided. No need to set the Initial Catalog as it will get replaced by the name of the database that will be created. /// </summary> public static ThrowawayDatabase Create(string connectionString, ThrowawayDatabaseOptions options) { if (!TryPingDatabase(connectionString)) { throw new Exception("Could not connect to the database"); } var database = new ThrowawayDatabase(connectionString ?? string.Empty, options); if (!database.CreateDatabaseIfDoesNotExist(options)) { throw new Exception("Could not create the throwaway database"); } return(database); }
/// <summary> /// Creates a throwaway database using the connection string provided. /// No need to set the Initial Catalog as it will get replaced by the name of the database that will be created. /// </summary> public static ThrowawayDatabase Create(string connectionString, string databaseNamePrefix = null) { if (!TryPingDatabase(connectionString)) { throw new Exception("Could not connect to the database"); } var database = new ThrowawayDatabase(connectionString ?? "", databaseNamePrefix); if (!database.CreateDatabaseIfDoesNotExist()) { throw new Exception("Could not create the throwaway database"); } return(database); }
/// <summary> /// Creates a database through SQL server authentication using the given username, password and the datasource/instance. /// </summary> public static ThrowawayDatabase Create(string username, string password, string datasource, string databaseNamePrefix = null) { var connectionString = $"Password={password};Persist Security Info=True;User ID={username};Initial Catalog=master;Data Source={datasource}"; if (!TryPingDatabase(connectionString)) { throw new Exception("Could not connect to the database"); } var database = new ThrowawayDatabase(connectionString, databaseNamePrefix); if (!database.CreateDatabaseIfDoesNotExist()) { throw new Exception("Could not create the throwaway database"); } return(database); }
/// <summary> /// Uses the given instance as the Data Source of the connection string along with integration security /// assuming that the current user has direct access to his or her Sql server instance. /// </summary> public static ThrowawayDatabase FromLocalInstance(string instance, string databaseNamePrefix = null) { var connectionString = $"Data Source={instance};Initial Catalog=master;Integrated Security=True;"; if (!TryPingDatabase(connectionString)) { throw new Exception("Could not connect to the database"); } var database = new ThrowawayDatabase(connectionString, databaseNamePrefix); if (!database.CreateDatabaseIfDoesNotExist()) { throw new Exception("Could not create the throwaway database"); } return(database); }
public void Dispose() { Db?.RestoreSnapshot(); Db = null; }
public SnapshotScope(ThrowawayDatabase db) { Db = db; Db.CreateSnapshot(); }
/// <summary> /// Creates a snapshot at the beginning of the scope, and restores the snapshot at the end of the scope. /// </summary> /// <param name="this">An instance of <see cref="ThrowawayDatabase"/></param> /// <returns>The <see cref="SnapshotScope"/></returns> public static SnapshotScope CreateSnapshotScope(this ThrowawayDatabase @this) { return(new SnapshotScope(@this)); }