SqlClient Support for LocalDB
Beginning in SQL Server code name Denali, a lightweight version of SQL Server, called LocalDB, will be available. This topic discusses how to connect to a LocalDB database.
Remarks
For more information about LocalDB, including how to install LocalDB and configure your LocalDB instance, see SQL Server Books Online.
To summarize what you can do with LocalDB:
-
Create and start LocalDB instances with sqllocaldb.exe or your app.config file.
-
Use sqlcmd.exe to add and modify databases in a LocalDB instance. For example,
sqlcmd -S (localdb)\myinst.
-
Use the AttachDBFilename connection string keyword to add a database to your LocalDB instance. When using AttachDBFilename, if you do not specify the name of the database with the Database connection string keyword, the database will be removed from the LocalDB instance when the application closes.
-
Specify a LocalDB instance in your connection string. For example, your instance name is myInstance, the connection string would include:
server=(localdb)\\myInstance
User Instance=True is not allowed when connecting to a LocalDB database.
You can download LocalDB from Microsoft SQL Server code name 'Denali' Express Core Community Technology Preview 3 (CTP 3). If you will use sqlcmd.exe to modify data in your LocalDB instance, you will need sqlcmd from Denali CTP 3, which you can get from: Microsoft SQL Server code name 'Denali', Community Technology Preview 3 (CTP 3).
Programmatically Create a Named Instance
An application can create a named instance and specify a database as follows:
-
Specify the LocalDB instances to create in the app.config file, as follows. The version number of the instance should be the same as the version number of your LocalDB installation.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="system.data.localdb" type="System.Data.LocalDBConfigurationSection,System.Data,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089"/> </configSections> <system.data.localdb> <localdbinstances> <add name="myInstance" version="11.0" /> </localdbinstances> </system.data.localdb> </configuration> -
Specify the name of the instance using the server connection string keyword. The instance name specified in the server connection string keyword must match the name specified in the app.config file (discussed next).
-
Use the AttachDBFilename connection string keyword to specify the .MDF file.