Tuesday, June 14, 2016

SqlLocalDB command line tool

A few years ago, Microsoft created SQL Server LocalDB,  an execution mode of SQL Server Express targeted to developers. LocalDB installation copies a minimal set of files necessary to start the SQL Server Database Engine making it much easier to install and manage. Once LocalDB is installed, developers initiate a connection by using a special connection string:
Data Source=(LocalDB)\MyInstance;Initial Catalog=foo;Integrated Security=True;
When connecting, the necessary SQL Server infrastructure is automatically created and started, enabling the application to use the database without complex or time consuming configuration tasks. An instance of SQL Server Express LocalDB is managed by using the SqlLocalDB.exe utility.
LocalDB supports two kinds of instances: Automatic instances and named instances.
From MSDN:
  • Automatic instances of LocalDB are public. They are created and managed automatically for the user and can be used by any application. One automatic instance of LocalDB exists for every version of LocalDB installed on the user’s computer. Automatic instances of LocalDB provide seamless instance management. There is no need to create the instance; it just works. This allows for easy application installation and migration to a different computer. If the target machine has the specified version of LocalDB installed, the automatic instance of LocalDB for that version is available on the target machine as well. Automatic instances of LocalDB have a special pattern for the instance name that belongs to a reserved namespace. This prevents name conflicts with named instances of LocalDB. The name for the automatic instance is MSSQLLocalDB.
  • Named instances of LocalDB are private. They are owned by a single application that is responsible for creating and managing the instance. Named instances provide isolation from other instances and can improve performance by reducing resource contention with other database users. Named instances must be created explicitly by the user through the LocalDB management API or implicitly via the app.config file for a managed application (although managed application may also use the API, if desired). Each named instance of LocalDB has an associated LocalDB version that points to the respective set of LocalDB binaries. The instance name of a LocalDB is sysname data type and can have up to 128 characters. (This differs from regular named instances of SQL Server, which limits names to regular NetBIOS names of 16 ASCII chars.) The name of an instance of LocalDB can contain any Unicode characters that are legal within a filename. A named instance that uses an automatic instance name becomes an automatic instance.

To connect to an automatic instance, you can use the following connection string:
Server=(localdb)\MSSQLLocalDB;Integrated Security=true
To connect to a namend instance, you first need to use the SqlLocalDB.exe command line tool to create and start a named instance of LocalDB:
REM Create an instance of LocalDB  
SqlLocalDB create MyInstance
REM Start the instance of LocalDB  
SqlLocalDB start MyInstance
REM Gather information about the instance of LocalDB  
SqlLocalDB info MyInstance
Once created you can use the following connection string:
Data Source=(localdb)\MyInstance;Initial Catalog=foo;Integrated Security=True;
UPDATE: If you are using a SQL Server version below 2014, the connectionstring for the automatic instance is/was:

Server=(localdb)\v11.0;Integrated Security=true

No comments: