Exemple #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;
        }
        /// <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;
        }
Exemple #3
0
        /// <summary>
        /// Gets a SQL Local DB instance with the specified name if it exists, otherwise a new instance with the specified name is created.
        /// </summary>
        /// <param name="api">The <see cref="ISqlLocalDbApi"/> to use to get or create the instance.</param>
        /// <param name="instanceName">The name of the SQL Server LocalDB instance to get or create.</param>
        /// <returns>
        /// A SQL Local DB instance with the name specified by <paramref name="instanceName"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="api"/> or <paramref name="instanceName"/> is <see langword="null"/>.
        /// </exception>
        public static ISqlLocalDbInstanceInfo GetOrCreateInstance(this ISqlLocalDbApi api, string instanceName)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            if (instanceName == null)
            {
                throw new ArgumentNullException(nameof(instanceName));
            }

            // Instance names in SQL Local DB are case-insensitive
            if (string.Equals(api.DefaultInstanceName, instanceName, StringComparison.OrdinalIgnoreCase) ||
                SqlLocalDbApi.IsDefaultInstanceName(instanceName))
            {
                // The default instance is always listed, even if it does not exist,
                // so need to query that separately to verify whether to get or create.
                ISqlLocalDbInstanceInfo info = api.GetInstanceInfo(instanceName);

                if (info != null)
                {
                    // If it exists (or it's a default instance), we can't create
                    // it so just return the information about it immediately.
                    if (info.Exists || info.IsAutomatic)
                    {
                        return(info);
                    }
                }
            }

            if (api.InstanceExists(instanceName))
            {
                return(api.GetInstanceInfo(instanceName));
            }
            else
            {
                return(api.CreateInstance(instanceName, api.LatestVersion));
            }
        }
Exemple #4
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));
        }
Exemple #5
0
        /// <summary>
        /// Gets the instance with the existing name.
        /// </summary>
        /// <param name="api"></param>
        /// <param name="instanceName"></param>
        /// <returns></returns>
        ISqlLocalDbInstanceInfo GetLocalDbInstance(ISqlLocalDbApi api, string instanceName)
        {
            try
            {
                if (api.GetInstanceInfo(instanceName) is ISqlLocalDbInstanceInfo info && info.Exists)
                {
                    return(info);
                }
            }
            catch (InvalidOperationException)
            {
                // ignore
            }

            return(null);
        }
Exemple #6
0
        /// <summary>
        /// Returns information about the available SQL Server LocalDB instances.
        /// </summary>
        /// <param name="api">The <see cref="ISqlLocalDbApi"/> to use to enumerate the instances.</param>
        /// <returns>
        /// An <see cref="IReadOnlyList{ISqlLocalDbInstanceInfo}"/> containing information
        /// about the available SQL Server LocalDB instances on the current machine.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="api"/> is <see langword="null"/>.
        /// </exception>
        public static IReadOnlyList <ISqlLocalDbInstanceInfo> GetInstances(this ISqlLocalDbApi api)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            IReadOnlyList <string> instanceNames = api.GetInstanceNames();

            var instances = new List <ISqlLocalDbInstanceInfo>(instanceNames?.Count ?? 0);

            if (instanceNames != null)
            {
                foreach (string name in instanceNames)
                {
                    ISqlLocalDbInstanceInfo info = api.GetInstanceInfo(name);
                    instances.Add(info);
                }
            }

            return(instances);
        }