static void Main(string[] args) { var arguments = String.Join(", ", args); Console.WriteLine($"Arguments [{arguments}]"); using (var database = ThrowawayDatabase.FromLocalInstance("localhost\\SQLEXPRESS")) { using (var connection = new SqlConnection(database.ConnectionString)) { connection.Open(); using (var cmd = new SqlCommand("SELECT 1", connection)) { var result = Convert.ToInt32(cmd.ExecuteScalar()); Console.WriteLine(result); } } } }
public void CreateDatabase() { string databaseName; using (var fixture = ThrowawayDatabase.FromLocalInstance(LocalInstanceName)) { databaseName = fixture.Name; DatabaseExists(databaseName) .Should() .BeTrue(); CheckCommandExecution(fixture) .Should() .BeTrue(); } DatabaseExists(databaseName) .Should() .BeFalse();
static void Main(string[] args) { var arguments = String.Join(", ", args); Console.WriteLine($"Arguments [{arguments}]"); using (var database = ThrowawayDatabase.FromLocalInstance("localhost\\SQLEXPRESS")) { Console.WriteLine($"Created database {database.Name}"); // Apply migrations / seed data if necessary // execute code against this newly generated database using (var connection = new SqlConnection(database.ConnectionString)) { connection.Open(); using (var cmd = new SqlCommand("SELECT 1", connection)) { var result = Convert.ToInt32(cmd.ExecuteScalar()); Console.WriteLine(result); // prints 1 } } } }
public void CreateAndRestoreSnapshot() { const string tblTest = nameof(tblTest); using var fixture = ThrowawayDatabase.FromLocalInstance(LocalInstanceName); var connection = fixture.OpenConnection(); using (var cmd = new SqlCommand($"CREATE TABLE {tblTest} (test int)", connection)) cmd.ExecuteNonQuery(); var items = GetItems(fixture).ToArray(); items.Should().BeEmpty(); // Create a snapshot and populate the table fixture.CreateSnapshot(); using (var cmd = new SqlCommand($"INSERT {tblTest} VALUES (@i)", connection)) { cmd.Parameters.Add("i", SqlDbType.Int); foreach (var i in Enumerable.Range(1, 3)) { cmd.Parameters[0].Value = i; cmd.ExecuteNonQuery(); } } items = GetItems(fixture).ToArray(); items.Should().BeEquivalentTo(new[] { 1, 2, 3 }); // Restore the snapshot fixture.RestoreSnapshot(); items = GetItems(fixture).ToArray(); items.Should().BeEmpty();