/// <summary> /// Tests the lifecycle of SQL LocalDB instances. /// </summary> /// <param name="localDB">The <see cref="ISqlLocalDbApi"/> to use.</param> /// <param name="provider">The <see cref="ISqlLocalDbProvider"/> to use.</param> private static void TestInstanceLifecycle(ISqlLocalDbApi localDB, ISqlLocalDbProvider provider) { string instanceName = Guid.NewGuid().ToString(); string sharedInstanceName = string.Empty; ISqlLocalDbInstance instance = provider.CreateInstance(instanceName); instance.Start(); try { bool currentUserIsAdmin = Helpers.IsCurrentUserAdmin(); if (currentUserIsAdmin) { sharedInstanceName = Guid.NewGuid().ToString(); instance.Share(sharedInstanceName); // Restart the instance so it listens on the new shared name's pipe instance.Restart(); } try { ISqlLocalDbInstanceInfo info = provider.GetInstances() .Where((p) => string.Equals(p.Name, instanceName, StringComparison.Ordinal)) .FirstOrDefault(); Assert.IsNotNull(info, "GetInstances() did not return the created instance."); Assert.AreEqual(sharedInstanceName, info.SharedName, "ISqlLocalDbInstanceInfo.SharedName is incorrect."); using (SqlConnection connection = instance.CreateConnection()) { Assert.IsNotNull(connection, "CreateConnection() returned null."); TestConnection(connection); } if (currentUserIsAdmin) { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder() { DataSource = $@"(localdb)\.\{sharedInstanceName}", IntegratedSecurity = true }; using (SqlConnection connection = new SqlConnection(builder.ConnectionString)) { TestConnection(connection); } } } finally { if (currentUserIsAdmin) { instance.Unshare(); } } } finally { instance.Stop(); localDB.DeleteInstance(instance.Name); } }
internal static void Main(string[] args) { PrintBanner(); ISqlLocalDbApi localDB = new SqlLocalDbApiWrapper(); if (!localDB.IsLocalDBInstalled()) { Console.WriteLine(SR.SqlLocalDbApi_NotInstalledFormat, Environment.MachineName); return; } if (args?.Length == 1 && string.Equals(args[0], "/deleteuserinstances", StringComparison.OrdinalIgnoreCase)) { SqlLocalDbApi.DeleteUserInstances(deleteFiles: true); } ISqlLocalDbProvider provider = new SqlLocalDbProvider(); IList <ISqlLocalDbVersionInfo> versions = provider.GetVersions(); Console.WriteLine(Strings.Program_VersionsListHeader); Console.WriteLine(); foreach (ISqlLocalDbVersionInfo version in versions) { Console.WriteLine(version.Name); } Console.WriteLine(); IList <ISqlLocalDbInstanceInfo> instances = provider.GetInstances(); Console.WriteLine(Strings.Program_InstancesListHeader); Console.WriteLine(); foreach (ISqlLocalDbInstanceInfo instanceInfo in instances) { Console.WriteLine(instanceInfo.Name); } Console.WriteLine(); string instanceName = Guid.NewGuid().ToString(); ISqlLocalDbInstance instance = provider.CreateInstance(instanceName); instance.Start(); try { if (IsCurrentUserAdmin()) { instance.Share(Guid.NewGuid().ToString()); } try { using (SqlConnection connection = instance.CreateConnection()) { connection.Open(); try { using (SqlCommand command = new SqlCommand("create database [MyDatabase]", connection)) { command.ExecuteNonQuery(); } using (SqlCommand command = new SqlCommand("drop database [MyDatabase]", connection)) { command.ExecuteNonQuery(); } } finally { connection.Close(); } } } finally { if (IsCurrentUserAdmin()) { instance.Unshare(); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { instance.Stop(); localDB.DeleteInstance(instance.Name); } Console.WriteLine(); Console.Write(Strings.Program_ExitPrompt); Console.ReadKey(); }
/// <summary> /// Stops sharing the LocalDB instance. /// </summary> /// <exception cref="ObjectDisposedException"> /// The instance has been disposed. /// </exception> public void Unshare() { EnsureNotDisposed(); _instance.Unshare(); }