Class LocalDb
Manages LocalDB databases.
Inheritance
Namespace: Umbraco.Cms.Infrastructure.Persistence
Assembly: Umbraco.Infrastructure.dll
Syntax
public class LocalDb
Remarks
Latest version is SQL Server 2016 Express LocalDB, see https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-2016-express-localdb which can be installed by downloading the Express installer from https://www.microsoft.com/en-us/sql-server/sql-server-downloads (about 5MB) then select 'download media' to download SqlLocalDB.msi (about 44MB), which you can execute. This installs LocalDB only. Though you probably want to install the full Express. You may also want to install SQL Server Management Studio which can be used to connect to LocalDB databases.
See also https://github.com/ritterim/automation-sql which is a somewhat simpler version of this.
Properties
View SourceIsAvailable
Gets a value indicating whether LocalDb is available.
Declaration
public bool IsAvailable { get; }
Property Value
Type | Description |
---|---|
System.Boolean |
Version
Gets the LocalDb installed version.
Declaration
public int Version { get; }
Property Value
Type | Description |
---|---|
System.Int32 |
Remarks
If more than one version is installed, returns the highest available. Returns the major version as an integer e.g. 11, 12...
Methods
View SourceCopyDatabaseFiles(String, String, String, String, String, String, Boolean, Boolean)
Copy database files.
Declaration
public void CopyDatabaseFiles(string databaseName, string filesPath, string targetDatabaseName = null, string targetFilesPath = null, string sourceExtension = null, string targetExtension = null, bool overwrite = false, bool delete = false)
Parameters
Type | Name | Description |
---|---|---|
System.String | databaseName | The name of the source database. |
System.String | filesPath | The directory containing source database files. |
System.String | targetDatabaseName | The name of the target database. |
System.String | targetFilesPath | The directory containing target database files. |
System.String | sourceExtension | The source database files extension. |
System.String | targetExtension | The target database files extension. |
System.Boolean | overwrite | A value indicating whether to overwrite the target files. |
System.Boolean | delete | A value indicating whether to delete the source files. |
Remarks
The targetDatabaseName
, targetFilesPath
,
sourceExtension
and targetExtension
parameters are optional. If they result in target being identical
to source, no copy is performed. If delete
is false, nothing happens, otherwise the source
files are deleted.
If target is not identical to source, files are copied or moved, depending on the value of
delete
.
Extensions are used eg to copy MyDatabase.mdf to MyDatabase.mdf.temp.
CreateInstance(String)
Creates a LocalDb instance.
Declaration
public bool CreateInstance(string instanceName)
Parameters
Type | Name | Description |
---|---|---|
System.String | instanceName | The name of the instance. |
Returns
Type | Description |
---|---|
System.Boolean | A value indicating whether the instance was created without errors. |
DatabaseFilesExist(String, String, String)
Gets a value indicating whether database files exist.
Declaration
public bool DatabaseFilesExist(string databaseName, string filesPath, string extension = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | databaseName | The name of the source database. |
System.String | filesPath | The directory containing source database files. |
System.String | extension | The database files extension. |
Returns
Type | Description |
---|---|
System.Boolean | A value indicating whether the database files exist. |
Remarks
Extensions are used eg to copy MyDatabase.mdf to MyDatabase.mdf.temp.
DropInstance(String)
Drops a LocalDb instance.
Declaration
public bool DropInstance(string instanceName)
Parameters
Type | Name | Description |
---|---|---|
System.String | instanceName | The name of the instance. |
Returns
Type | Description |
---|---|
System.Boolean | A value indicating whether the instance was dropped without errors. |
Remarks
When an instance is dropped all the attached database files are deleted. Successful if the instance does not exist.
GetInstance(String)
Gets a LocalDb instance.
Declaration
public LocalDb.Instance GetInstance(string instanceName)
Parameters
Type | Name | Description |
---|---|---|
System.String | instanceName | The name of the instance. |
Returns
Type | Description |
---|---|
LocalDb.Instance | The instance with the specified name if it exists, otherwise null. |
GetInstances()
Gets the name of existing LocalDb instances.
Declaration
public string[] GetInstances()
Returns
Type | Description |
---|---|
System.String[] | The name of existing LocalDb instances. |
InstanceExists(String)
Gets a value indicating whether a LocalDb instance exists.
Declaration
public bool InstanceExists(string instanceName)
Parameters
Type | Name | Description |
---|---|---|
System.String | instanceName | The name of the instance. |
Returns
Type | Description |
---|---|
System.Boolean | A value indicating whether a LocalDb instance with the specified name exists. |
StartInstance(String)
Stops a LocalDb instance.
Declaration
public bool StartInstance(string instanceName)
Parameters
Type | Name | Description |
---|---|---|
System.String | instanceName | The name of the instance. |
Returns
Type | Description |
---|---|
System.Boolean | A value indicating whether the instance was started without errors. |
Remarks
Failed if the instance does not exist.
StopInstance(String)
Stops a LocalDb instance.
Declaration
public bool StopInstance(string instanceName)
Parameters
Type | Name | Description |
---|---|---|
System.String | instanceName | The name of the instance. |
Returns
Type | Description |
---|---|
System.Boolean | A value indicating whether the instance was stopped without errors. |
Remarks
Successful if the instance does not exist.