Example #1
0
        public void Can_Test_Whether_Instances_Exist()
        {
            // Arrange
            using (var api = new SqlLocalDbApi(_loggerFactory))
            {
                // Start the default instance to ensure it exists
                api.StartInstance(api.DefaultInstanceName);

                try
                {
                    // Arrange
                    string instanceName = api.DefaultInstanceName;

                    // Act
                    bool actual = api.InstanceExists(instanceName);

                    // Assert
                    actual.ShouldBeTrue();

                    // Arrange
                    instanceName = Guid.NewGuid().ToString();

                    // Act
                    actual = api.InstanceExists(instanceName);

                    // Assert
                    actual.ShouldBeFalse();
                }
                finally
                {
                    api.StopInstance(api.DefaultInstanceName);
                }
            }
        }
Example #2
0
 public void Throws_PlatformNotSupportedException()
 {
     // Arrange
     using (var actual = new SqlLocalDbApi(_loggerFactory))
     {
         // Act and Assert
         Assert.Throws <PlatformNotSupportedException>(() => actual.CreateInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.DeleteInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.DeleteUserInstances());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetDefaultInstance());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetInstanceInfo("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetInstanceNames());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetInstances());
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetOrCreateInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.GetVersionInfo("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.InstanceExists("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.LatestVersion);
         Assert.Throws <PlatformNotSupportedException>(() => actual.ShareInstance("name", "sharedName"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.ShareInstance("sid", "name", "sharedName"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.StartInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.StartTracing());
         Assert.Throws <PlatformNotSupportedException>(() => actual.StopInstance("name"));
         Assert.Throws <PlatformNotSupportedException>(() => actual.StopTracing());
         Assert.Throws <PlatformNotSupportedException>(() => actual.UnshareInstance("name"));
     }
 }
Example #3
0
        public void Throws_InvalidOperationException_If_SQL_LocalDB_Not_Installed()
        {
            // Arrange
            var options  = new SqlLocalDbOptions();
            var registry = Mock.Of <Interop.IRegistry>();

            using (var actual = new SqlLocalDbApi(options, registry, _loggerFactory))
            {
                // Act and Assert
                Assert.Throws <InvalidOperationException>(() => actual.CreateInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.DeleteInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.DeleteUserInstances());
                Assert.Throws <InvalidOperationException>(() => actual.GetDefaultInstance());
                Assert.Throws <InvalidOperationException>(() => actual.GetInstanceInfo("name"));
                Assert.Throws <InvalidOperationException>(() => actual.GetInstanceNames());
                Assert.Throws <InvalidOperationException>(() => actual.GetInstances());
                Assert.Throws <InvalidOperationException>(() => actual.GetOrCreateInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.GetVersionInfo("name"));
                Assert.Throws <InvalidOperationException>(() => actual.InstanceExists("name"));
                Assert.Throws <InvalidOperationException>(() => actual.LatestVersion);
                Assert.Throws <InvalidOperationException>(() => actual.ShareInstance("name", "sharedName"));
                Assert.Throws <InvalidOperationException>(() => actual.StartInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.StartTracing());
                Assert.Throws <InvalidOperationException>(() => actual.StopInstance("name"));
                Assert.Throws <InvalidOperationException>(() => actual.StopTracing());
                Assert.Throws <InvalidOperationException>(() => actual.UnshareInstance("name"));
            }
        }
Example #4
0
        public void SqlLocalDbInstance_Constructor_If_Instance_Is_Started()
        {
            // Arrange
            Helpers.EnsureLocalDBInstalled();

            string instanceName = Guid.NewGuid().ToString();

            try
            {
                SqlLocalDbApi.CreateInstance(instanceName);

                try
                {
                    string namedPipe = SqlLocalDbApi.StartInstance(instanceName);

                    // Act
                    SqlLocalDbInstance target = new SqlLocalDbInstance(instanceName);

                    // Assert
                    Assert.IsTrue(target.IsRunning, "SqlLocalDbInstance.IsRunning is incorrect.");
                    Assert.AreEqual(instanceName, target.Name, "SqlLocalDbInstance.Name is incorrect.");
                    Assert.AreEqual(namedPipe, target.NamedPipe, "SqlLocalDbInstance.NamedPipe is incorrect.");
                }
                finally
                {
                    SqlLocalDbApi.StopInstance(instanceName);
                }
            }
            finally
            {
                SqlLocalDbApi.DeleteInstance(instanceName);
            }
        }
Example #5
0
        private void StartClick(object sender, EventArgs e)
        {
            Button obj = (Button)sender;

            WaitLabel();
            SqlLocalDbApi.StartInstance(obj.Parent.Name);
            RefreshInstances();
        }
        private void CreateLocalDb()
        {
            string connection    = System.Configuration.ConfigurationManager.ConnectionStrings["BVCareManager.Properties.Settings.BVCareManagerConnectionString"].ConnectionString;
            string localDbstring = @"(LocalDB)\";

            int pFrom = connection.IndexOf(localDbstring) + localDbstring.Length;
            int pTo   = connection.LastIndexOf(";AttachDbFilename");

            string resultLocalDb = connection.Substring(pFrom, pTo - pFrom);

            #region SqlLocalDb Handle
            var localDb = new SqlLocalDbApi();

            ISqlLocalDbInstanceInfo localDbInstance = localDb.GetInstanceInfo(resultLocalDb);
            string localDbVersion = localDbInstance.LocalDbVersion.ToString().Substring(0, 2);

            if (localDbVersion == "0.")
            {
                try
                {
                    localDb.CreateInstance(resultLocalDb, "12.0");
                }
                catch
                {
                    MessageBox.Show("Phiên bản SQL Server LocalDB hiện tại không phù hợp. Hãy cài đặt phiên bản 12.0 (SQL Server 2014 Express LocalDb)");
                    Environment.Exit(1);
                }
            }
            else if (localDbVersion != "0." && localDbVersion != "12")
            {
                localDb.StopInstance(resultLocalDb);
                localDb.DeleteInstance(resultLocalDb);

                try
                {
                    localDb.CreateInstance(resultLocalDb, "12.0");
                }
                catch
                {
                    MessageBox.Show("Phiên bản SQL Server LocalDB hiện tại không phù hợp. Hãy cài đặt phiên bản 12.0 (SQL Server 2014 Express LocalDb)");
                    Environment.Exit(1);
                }
            }


            localDb.StartInstance(resultLocalDb);
            #endregion
        }
        public void Manager_Shares_And_Unshares_Instance()
        {
            // Act
            string instanceName = Guid.NewGuid().ToString();
            string sharedName   = Guid.NewGuid().ToString();

            using (var api = new SqlLocalDbApi(_loggerFactory))
            {
                api.CreateInstance(instanceName);

                try
                {
                    api.StartInstance(instanceName);

                    try
                    {
                        var instance = api.GetInstanceInfo(instanceName);

                        var manager = new SqlLocalDbInstanceManager(instance, api);

                        // Act
                        manager.Share(sharedName);

                        // Assert
                        instance.IsShared.ShouldBeTrue();

                        // Act
                        manager.Unshare();

                        // Assert
                        instance.IsShared.ShouldBeFalse();
                    }
                    catch (Exception)
                    {
                        api.StopInstance(instanceName);
                        throw;
                    }
                }
                catch (Exception)
                {
                    api.DeleteInstance(instanceName, deleteFiles: true);
                    throw;
                }
            }
        }
Example #8
0
        /// <summary>
        /// Starts the SQL Server LocalDB instance.
        /// </summary>
        /// <exception cref="SqlLocalDbException">
        /// The LocalDB instance could not be started.
        /// </exception>
        public void Start()
        {
            try
            {
                // The pipe name changes per instance lifetime
                _namedPipe = SqlLocalDbApi.StartInstance(_instanceName);
            }
            catch (SqlLocalDbException e)
            {
                string message = SRHelper.Format(
                    SR.SqlLocalDbInstance_StartFailedFormat,
                    _instanceName);

                Logger.Error(Logger.TraceEvent.StartInstance, message);

                throw new SqlLocalDbException(
                          message,
                          e.ErrorCode,
                          e.InstanceName,
                          e);
            }
        }
Example #9
0
        public void Methods_Validate_Parameters()
        {
            // Arrange
            TimeSpan timeout = TimeSpan.Zero.Add(TimeSpan.FromTicks(-1));

            using (var actual = new SqlLocalDbApi(_loggerFactory))
            {
                // Act and Assert
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.CreateInstance(null, "version"));
                Assert.Throws <ArgumentNullException>("version", () => actual.CreateInstance("instanceName", null));
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.DeleteInstance(null));
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.GetInstanceInfo(null));
                Assert.Throws <ArgumentNullException>("version", () => actual.GetVersionInfo(null));
                Assert.Throws <ArgumentNullException>("ownerSid", () => actual.ShareInstance(null, "instanceName", "sharedInstanceName"));
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.ShareInstance("ownerSid", null, "sharedInstanceName"));
                Assert.Throws <ArgumentNullException>("sharedInstanceName", () => actual.ShareInstance("ownerSid", "instanceName", null));
                Assert.Throws <ArgumentException>("instanceName", () => actual.ShareInstance("sid", string.Empty, "sharedInstanceName"));
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.StartInstance(null));
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.StopInstance(null, TimeSpan.Zero));
                Assert.Throws <ArgumentOutOfRangeException>("timeout", () => actual.StopInstance("instanceName", timeout)).ActualValue.ShouldBe(timeout);
                Assert.Throws <ArgumentNullException>("instanceName", () => actual.UnshareInstance(null));
            }
        }
Example #10
0
        public void Can_Manage_SqlLocalDB_Instances()
        {
            // Arrange
            using (var actual = new SqlLocalDbApi(_loggerFactory))
            {
                string instanceName = Guid.NewGuid().ToString();

                // Act
                ISqlLocalDbInstanceInfo instance = actual.GetInstanceInfo(instanceName);

                // Assert
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeFalse();
                instance.IsRunning.ShouldBeFalse();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeFalse();

                // Act
                actual.CreateInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeFalse();

                // Act
                actual.StartInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeTrue();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeTrue();

                // Act
                actual.StopInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeFalse();

                // Act
                actual.DeleteInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeFalse();
                instance.IsRunning.ShouldBeFalse();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeFalse();

                // Act (no Assert)
                actual.DeleteInstanceFiles(instanceName);
            }
        }
Example #11
0
        public async Task Can_Manage_SqlLocalDB_Instances()
        {
            // Arrange
            using (var actual = new SqlLocalDbApi(_loggerFactory))
            {
                string instanceName = Guid.NewGuid().ToString();

                // Act
                ISqlLocalDbInstanceInfo instance = actual.GetInstanceInfo(instanceName);

                // Assert
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeFalse();
                instance.IsRunning.ShouldBeFalse();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeFalse();

                // Act
                instance = actual.CreateInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeFalse();

                // Act
                string namedPipe = actual.StartInstance(instanceName);

                // Assert
                namedPipe.ShouldNotBeNullOrWhiteSpace();

                var builder = new SqlConnectionStringBuilder()
                {
                    DataSource = namedPipe
                };

                using (var connection = new SqlConnection(builder.ConnectionString))
                {
                    await connection.OpenAsync();
                }

                // Act
                instance = actual.GetInstanceInfo(instanceName);

                // Assert
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeTrue();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeTrue();

                // Act
                actual.StopInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeTrue();
                instance.IsRunning.ShouldBeFalse();

                // Act
                actual.DeleteInstance(instanceName);

                // Assert
                instance = actual.GetInstanceInfo(instanceName);
                instance.ShouldNotBeNull();
                instance.Name.ShouldBe(instanceName);
                instance.Exists.ShouldBeFalse();
                instance.IsRunning.ShouldBeFalse();

                // Act and Assert
                actual.InstanceExists(instanceName).ShouldBeFalse();

                // Act (no Assert)
                actual.DeleteInstanceFiles(instanceName);
            }
        }
Example #12
0
 /// <summary>
 /// Starts the specified instance of SQL Server LocalDB.
 /// </summary>
 /// <param name="instanceName">The name of the LocalDB instance to start.</param>
 /// <returns>
 /// The named pipe to use to connect to the LocalDB instance.
 /// </returns>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="instanceName"/> is <see langword="null"/>.
 /// </exception>
 /// <exception cref="InvalidOperationException">
 /// SQL Server LocalDB is not installed on the local machine.
 /// </exception>
 /// <exception cref="SqlLocalDbException">
 /// The SQL Server LocalDB instance specified by <paramref name="instanceName"/> could not be started.
 /// </exception>
 public virtual string StartInstance(string instanceName) => SqlLocalDbApi.StartInstance(instanceName);