/// <summary> /// Starts the SQL Server LocalDB instance. /// </summary> /// <exception cref="SqlLocalDbException"> /// The LocalDB instance could not be started. /// </exception> public void Start() { try { Api.StartInstance(Name); UpdateState(); } catch (SqlLocalDbException ex) { throw new SqlLocalDbException( SRHelper.Format(SR.SqlLocalDbInstanceManager_StartFailedFormat, Name), ex.ErrorCode, ex.InstanceName, ex); } }
/// <summary> /// Creates an instance of <see cref="SqlConnectionStringBuilder"/> containing /// the default SQL connection string to connect to the LocalDB instance. /// </summary> /// <param name="instance">The SQL LocalDB instance to create a connection string builder for.</param> /// <returns> /// An instance of <see cref="SqlConnectionStringBuilder"/> containing /// the default SQL connection string to connect to the LocalDB instance. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="instance"/> is <see langword="null"/>. /// </exception> /// <exception cref="InvalidOperationException"> /// The SQL LocalDB instance specified by <paramref name="instance"/> is not running. /// </exception> public static SqlConnectionStringBuilder CreateConnectionStringBuilder(this ISqlLocalDbInstanceInfo instance) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (!instance.IsRunning) { string message = SRHelper.Format(SR.ISqlLocalDbInstanceInfoExtensions_NotRunningFormat, instance.Name); throw new InvalidOperationException(message); } return(new SqlConnectionStringBuilder() { DataSource = instance.NamedPipe, }); }
/// <summary> /// Returns an <see cref="ISqlLocalDbInstanceManager"/> that can be used to manage the instance. /// </summary> /// <param name="instance">The <see cref="ISqlLocalDbInstanceInfo"/> to manage.</param> /// <returns> /// An <see cref="ISqlLocalDbInstanceManager"/> that can be used to manage the SQL LocalDB instance. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="instance"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="instance"/> does not implement <see cref="ISqlLocalDbApiAdapter"/>. /// </exception> public static ISqlLocalDbInstanceManager Manage(this ISqlLocalDbInstanceInfo instance) { if (instance == null) { throw new ArgumentNullException(nameof(instance)); } if (instance is ISqlLocalDbApiAdapter adapter) { return(new SqlLocalDbInstanceManager(instance, adapter.LocalDb)); } string message = SRHelper.Format( SR.ISqlLocalDbInstanceInfoExtensions_DoesNotImplementAdapterFormat, nameof(ISqlLocalDbInstanceInfo), nameof(ISqlLocalDbApiAdapter)); throw new ArgumentException(message, nameof(instance)); }
/// <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 void Share(string sharedName) { if (sharedName == null) { throw new ArgumentNullException(nameof(sharedName)); } try { Api.ShareInstance(Name, sharedName); UpdateState(); } catch (SqlLocalDbException ex) { throw new SqlLocalDbException( SRHelper.Format(SR.SqlLocalDbInstanceManager_ShareFailedFormat, Name), ex.ErrorCode, ex.InstanceName, ex); } }