Beispiel #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SqlLocalDbInstance"/> class.
        /// </summary>
        /// <param name="instanceName">The name of the SQL Server LocalDB instance.</param>
        /// <param name="localDB">The <see cref="ISqlLocalDbApi"/> instance to use.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="instanceName"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// The LocalDB instance specified by <paramref name="instanceName"/> does not exist.
        /// </exception>
        /// <exception cref="SqlLocalDbException">
        /// The LocalDB instance specified by <paramref name="instanceName"/> could not be obtained.
        /// </exception>
        internal SqlLocalDbInstance(string instanceName, ISqlLocalDbApi localDB)
        {
            if (instanceName == null)
            {
                throw new ArgumentNullException(nameof(instanceName));
            }

            Debug.Assert(localDB != null, "localDB cannot be  null.");

            ISqlLocalDbInstanceInfo info = localDB.GetInstanceInfo(instanceName);

            if (info == null || !info.Exists)
            {
                string message = SRHelper.Format(
                    SR.SqlLocalDbInstance_InstanceNotFoundFormat,
                    instanceName);

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

                throw new InvalidOperationException(message);
            }

            _instanceName = instanceName;
            _namedPipe    = info.NamedPipe;
        }
Beispiel #2
0
        /// <summary>
        /// Sets the physical database file name in the specified <see cref="DbConnectionStringBuilder"/>.
        /// </summary>
        /// <param name="value">The <see cref="DbConnectionStringBuilder"/> to set the physical database file name for.</param>
        /// <param name="fileName">The physical file name to set.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="value"/> is <see langword="null"/> or invalid.
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// <paramref name="fileName"/> references the Data Directory for the current <see cref="AppDomain"/> but
        /// the Data Directory for the current <see cref="AppDomain"/> has no value set.
        /// </exception>
        public static void SetPhysicalFileName(this DbConnectionStringBuilder value, string fileName)
        {
            string fullPath = null;

            if (fileName != null)
            {
                fullPath = ExpandDataDirectoryIfPresent(fileName);

                try
                {
                    // Only full file paths are supported, so ensure that the path is fully qualified before it is set
                    fullPath = Path.GetFullPath(fullPath);
                }
                catch (ArgumentException ex)
                {
                    throw new ArgumentException(SRHelper.Format(SR.Extensions_InvalidPathFormat, ex.Message), nameof(fileName), ex);
                }
                catch (NotSupportedException ex)
                {
                    throw new ArgumentException(SRHelper.Format(SR.Extensions_InvalidPathFormat, ex.Message), nameof(fileName), ex);
                }
            }

            value.SetKeywordValueAsString(AttachDBFilenameKeywordName, fullPath);
        }
        public async Task SRHelper_Format_Formats_Parameters()
        {
            // Arrange
            await Task.Factory.StartNew(
                () =>
            {
                // Use a non-default culture
                var culture = CultureInfo.GetCultureInfo("en-GB");

                Thread.CurrentThread.CurrentCulture   = culture;
                Thread.CurrentThread.CurrentUICulture = culture;

                // Use a date where the result is valid with the day and month either way around
                // i.e. US format dates vs. UK format dates
                DateTime value = new DateTime(2012, 2, 3, 12, 34, 56);

                // Act
                string result = SRHelper.Format(
                    "{0}",
                    value);

                // Assert
                Assert.AreEqual(
                    value.ToString(culture),
                    result,
                    "Format() returned incorrect result.");
            });
        }
Beispiel #4
0
        /// <summary>
        /// Shares the LocalDB instance using the specified name.
        /// </summary>
        /// <param name="sharedName">The name to use to share the instance.</param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="sharedName"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="SqlLocalDbException">
        /// The LocalDB instance could not be shared.
        /// </exception>
        public virtual void Share(string sharedName)
        {
            if (sharedName == null)
            {
                throw new ArgumentNullException(nameof(sharedName));
            }

            try
            {
                SqlLocalDbApi.ShareInstance(_instanceName, sharedName);
            }
            catch (SqlLocalDbException e)
            {
                string message = SRHelper.Format(
                    SR.SqlLocalDbInstance_ShareFailedFormat,
                    _instanceName);

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

                throw new SqlLocalDbException(
                          message,
                          e.ErrorCode,
                          e.InstanceName,
                          e);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Deletes the specified <see cref="ISqlLocalDbInstance"/> instance.
        /// </summary>
        /// <param name="instance">The LocalDB instance to delete.</param>
        /// <param name="throwIfNotFound">
        /// Whether to throw an exception if the SQL LocalDB instance
        /// associated with <paramref name="instance"/> cannot be found.
        /// </param>
        /// <param name="deleteFiles">
        /// Whether to delete the file(s) associated with the SQL LocalDB instance.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="instance"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="SqlLocalDbException">
        /// The SQL Server LocalDB instance specified by <paramref name="instance"/> could not be deleted.
        /// </exception>
        internal static void Delete(ISqlLocalDbInstance instance, bool throwIfNotFound, bool deleteFiles)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            try
            {
                SqlLocalDbApi.DeleteInstanceInternal(instance.Name, throwIfNotFound, deleteFiles);
            }
            catch (SqlLocalDbException ex)
            {
                string message = SRHelper.Format(
                    SR.SqlLocalDbInstance_DeleteFailedFormat,
                    instance.Name);

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

                throw new SqlLocalDbException(
                          message,
                          ex.ErrorCode,
                          ex.InstanceName,
                          ex);
            }
        }
        public void SRHelper_Format_Throws_If_Args_Is_Null()
        {
            // Arrange
            string format = string.Empty;

            object[] args = null;

            throw ErrorAssert.Throws <ArgumentNullException>(
                      () => SRHelper.Format(format, args),
                      "args");
        }
        public void SRHelper_Format_Throws_If_Format_Is_Null()
        {
            // Arrange
            string format = null;

            object[] args = new object[0];

            // Act and Assert
            throw ErrorAssert.Throws <ArgumentNullException>(
                      () => SRHelper.Format(format, args),
                      "format");
        }
Beispiel #8
0
        /// <summary>
        /// Creates an instance of <see cref="SqlConnectionStringBuilder"/> containing
        /// the default SQL connection string to connect to the LocalDB instance.
        /// </summary>
        /// <returns>
        /// An instance of <see cref="SqlConnectionStringBuilder"/> containing
        /// the default SQL connection string to connect to the LocalDB instance.
        /// </returns>
        /// <exception cref="InvalidOperationException">
        /// The value of <see cref="SqlLocalDbInstance.IsRunning"/> is <see langword="false"/>.
        /// </exception>
        public virtual SqlConnectionStringBuilder CreateConnectionStringBuilder()
        {
            if (!this.IsRunning)
            {
                string message = SRHelper.Format(
                    SR.SqlLocalDbInstance_NotRunningFormat,
                    _instanceName);

                throw new InvalidOperationException(message);
            }

            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();

            builder.DataSource = _namedPipe;
            return(builder);
        }
Beispiel #9
0
        /// <summary>
        /// Creates an instance of <see cref="DbConnectionStringBuilder"/> that can be used to connect to the SQL LocalDB instance.
        /// </summary>
        /// <param name="instance">The <see cref="ISqlLocalDbInstance"/> to get the connection string builder for.</param>
        /// <param name="modelConnectionStringName">The name of the connection string for the model in the application configuration file.</param>
        /// <param name="initialCatalog">The optional name to use for the Initial Catalog in the provider connection string to override the default, if present.</param>
        /// <returns>
        /// The created instance of <see cref="DbConnectionStringBuilder"/>.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// No connection string with the name specified by <paramref name="modelConnectionStringName"/> can be found in the application configuration file.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="instance"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// The value of the <see cref="ISqlLocalDbInstance.NamedPipe"/> property of <paramref name="instance"/> is <see langword="null"/> or the empty string.
        /// </exception>
        public static DbConnectionStringBuilder GetConnectionStringForModel(this ISqlLocalDbInstance instance, string modelConnectionStringName, string initialCatalog)
        {
            if (instance == null)
            {
                throw new ArgumentNullException(nameof(instance));
            }

            var connectionStringSettings = ConfigurationManager.ConnectionStrings
                                           .OfType <ConnectionStringSettings>()
                                           .Where((p) => string.Equals(p.Name, modelConnectionStringName, StringComparison.Ordinal))
                                           .FirstOrDefault();

            if (connectionStringSettings == null)
            {
                throw new ArgumentException(SRHelper.Format(SR.Extensions_NoConnectionStringFormat, modelConnectionStringName), nameof(modelConnectionStringName));
            }

            return(CreateBuilder(connectionStringSettings, instance.NamedPipe, initialCatalog));
        }
Beispiel #10
0
        /// <summary>
        /// Stops sharing the LocalDB instance.
        /// </summary>
        /// <exception cref="SqlLocalDbException">
        /// The LocalDB instance could not be unshared.
        /// </exception>
        public virtual void Unshare()
        {
            try
            {
                SqlLocalDbApi.UnshareInstance(_instanceName);
            }
            catch (SqlLocalDbException e)
            {
                string message = SRHelper.Format(
                    SR.SqlLocalDbInstance_UnshareFailedFormat,
                    _instanceName);

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

                throw new SqlLocalDbException(
                          message,
                          e.ErrorCode,
                          e.InstanceName,
                          e);
            }
        }
Beispiel #11
0
        /// <summary>
        /// Stops the SQL Server LocalDB instance.
        /// </summary>
        /// <exception cref="SqlLocalDbException">
        /// The LocalDB instance could not be stopped.
        /// </exception>
        public void Stop()
        {
            try
            {
                SqlLocalDbApi.StopInstance(_instanceName);
                _namedPipe = string.Empty;
            }
            catch (SqlLocalDbException e)
            {
                string message = SRHelper.Format(
                    SR.SqlLocalDbInstance_StopFailedFormat,
                    _instanceName);

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

                throw new SqlLocalDbException(
                          message,
                          e.ErrorCode,
                          e.InstanceName,
                          e);
            }
        }
Beispiel #12
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);
            }
        }
Beispiel #13
0
        /// <summary>
        /// Creates a new instance of <see cref="ISqlLocalDbInstance"/>.
        /// </summary>
        /// <param name="instanceName">The name of the SQL Server LocalDB instance to create.</param>
        /// <returns>
        /// The created instance of <see cref="ISqlLocalDbInstance"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="instanceName"/> is <see langword="null"/>.
        /// </exception>
        /// <exception cref="InvalidOperationException">
        /// The LocalDB instance specified by <paramref name="instanceName"/> already exists or
        /// no LocalDB instance information was returned by the underlying SQL LocalDB API.
        /// </exception>
        /// <exception cref="SqlLocalDbException">
        /// The LocalDB instance specified by <paramref name="instanceName"/> could not be created.
        /// </exception>
        public virtual SqlLocalDbInstance CreateInstance(string instanceName)
        {
            ISqlLocalDbInstanceInfo info = _localDB.GetInstanceInfo(instanceName);

            if (info == null)
            {
                throw new InvalidOperationException(SR.SqlLocalDbProvider_NoInstance);
            }

            if (info.Exists)
            {
                string message = SRHelper.Format(
                    SR.SqlLocalDbFactory_InstanceExistsFormat,
                    instanceName);

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

                throw new InvalidOperationException(message);
            }

            string version;

            // If creating the default instance, the version number must not be specified
            if (SqlLocalDbApi.IsDefaultInstanceName(instanceName))
            {
                version = string.Empty;
            }
            else
            {
                version = _version ?? _localDB.LatestVersion;
            }

            _localDB.CreateInstance(instanceName, version);

            return(GetInstance(instanceName));
        }