SQL Server Express LocalDBSQL Server Express LocalDB

适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions) 适用于:Applies to: 是SQL ServerSQL Server(所有支持的版本)yesSQL ServerSQL Server (all supported versions)

Microsoft SQL Server Express LocalDB 是一种面向开发人员的 SQL Server Express 功能。Microsoft SQL Server Express LocalDB is a feature of SQL Server Express targeted to developers. 它在具有高级服务的 SQL Server Express 上可用。It is available on SQL Server Express with Advanced Services.

LocalDB 安装将复制启动 SQL Server 数据库引擎SQL Server Database Engine 所需的最少的文件集。LocalDB installation copies a minimal set of files necessary to start the SQL Server 数据库引擎SQL Server Database Engine. 安装 LocalDB 后,你可以使用特定连接字符串来启动连接。Once LocalDB is installed, you can initiate a connection using a special connection string. 连接时,将自动创建并启动所需的 SQL ServerSQL Server 基础结构,从而使应用程序无需执行复杂的配置任务即可使用数据库。When connecting, the necessary SQL ServerSQL Server infrastructure is automatically created and started, enabling the application to use the database without complex configuration tasks. 开发人员工具可以向开发人员提供 SQL Server 数据库引擎SQL Server Database Engine ,使其不必管理 Transact-SQLTransact-SQL 的完整服务器实例即可撰写和测试 SQL ServerSQL Server代码。Developer Tools can provide developers with a SQL Server 数据库引擎SQL Server Database Engine that lets them write and test Transact-SQLTransact-SQL code without having to manage a full server instance of SQL ServerSQL Server.

安装媒体Installation media

LocalDB 是在 SQL Server Express 安装过程中选择的功能,可以在下载安装介质后使用。LocalDB is a feature you select during SQL Server Express installation, and is available when you download the media. 如果下载介质,则选择“Express Advanced”或“LocalDB”包。If you download the media, either choose Express Advanced or the LocalDB package.

LocalDB 也可以通过 Visual Studio 安装程序、“数据存储和处理”工作负荷和“ASP.NET 和 Web 开发”工作负荷安装,或作为单独组件安装。Alternatively, you can install LocalDB through the Visual Studio Installer, as part of the Data Storage and Processing workload, the ASP.NET and web development workload, or as an individual component.

安装 LocalDBInstall LocalDB

通过安装向导或使用 SqlLocalDB.msi 程序安装 LocalDB。Install LocalDB through the installation wizard or by using the SqlLocalDB.msi program. LocalDB 是安装 SQL Server Express LocalDB 时的一个选项。LocalDB is an option when installing SQL Server Express LocalDB.

在安装过程中,在“功能选择/共享功能”页上选择 LocalDB。Select LocalDB on the Feature Selection/Shared Features page during installation. 对于每个主要 SQL Server 数据库引擎SQL Server Database Engine 版本,只能存在一个 LocalDB 二进制文件的安装。There can be only one installation of the LocalDB binary files for each major SQL Server 数据库引擎SQL Server Database Engine version. 可以启动多个 数据库引擎Database Engine 进程,并且这些进程都将使用相同的二进制文件。Multiple 数据库引擎Database Engine processes can be started and will all use the same binaries. 作为 LocalDB 启动的 SQL Server 数据库引擎SQL Server Database Engine 实例与 SQL Server ExpressSQL Server Express 具有相同限制。An instance of the SQL Server 数据库引擎SQL Server Database Engine started as the LocalDB has the same limitations as SQL Server ExpressSQL Server Express.

SQL Server ExpressSQL Server Express LocalDB 的实例通过 SqlLocalDB.exe 实用工具进行托管。An instance of SQL Server ExpressSQL Server Express LocalDB is managed by using the SqlLocalDB.exe utility. SQL Server ExpressSQL Server Express LocalDB 应该用于代替已弃用的 SQL Server ExpressSQL Server Express 用户实例功能。LocalDB should be used in place of the SQL Server ExpressSQL Server Express user instance feature, which was deprecated.

说明Description

LocalDB 安装程序使用 SqlLocalDB.msi 程序在计算机上安装所需文件。The LocalDB setup program uses the SqlLocalDB.msi program to install the necessary files on the computer. 安装后,LocalDB 是可以创建和打开 SQL ServerSQL Server 数据库的 SQL Server ExpressSQL Server Express 的实例。Once installed, LocalDB is an instance of SQL Server ExpressSQL Server Express that can create and open SQL ServerSQL Server databases. 数据库的系统数据库文件存储于本地 AppData 路径中,这个路径通常是隐藏的。The system database files for the database are stored in the local AppData path, which is normally hidden. 例如,C:\Users\<user>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\LocalDBApp1\For example, C:\Users\<user>\AppData\Local\Microsoft\Microsoft SQL Server Local DB\Instances\LocalDBApp1\. 用户数据库文件存储在用户指定的位置,通常为 C:\Users\<user>\Documents\ 文件夹中的某个位置。User database files are stored where the user designates, typically somewhere in the C:\Users\<user>\Documents\ folder.

有关将 LocalDB 包括在应用程序中的详细信息,请参阅 Visual StudioVisual Studio 本地数据概述在 Visual Studio 中创建一个数据库并添加表For more information about including LocalDB in an application, see Visual StudioVisual Studio Local Data Overview, Create a database and add tables in Visual Studio.

有关 LocalDB API 的详细信息,请参阅 SQL Server Express LocalDB 参考For more information about the LocalDB API, see SQL Server Express LocalDB Reference.

SqlLocalDb 实用工具可以创建 LocalDB 的新实例,启动和停止 LocalDB 的实例,并且包含可帮助你管理 LocalDB 的选项。有关 SqlLocalDb 实用工具的详细信息,请参阅 SqlLocalDB 实用工具The SqlLocalDb utility can create new instances of LocalDB, start and stop an instance of LocalDB, and includes options to help you manage LocalDB.For more information about the SqlLocalDb utility, see SqlLocalDB Utility.

将 LocalDB 的实例排序规则设置为 SQL_Latin1_General_CP1_CI_AS,并且不能更改。The instance collation for LocalDB is set to SQL_Latin1_General_CP1_CI_AS and cannot be changed. 一般支持数据库级、列级和表达式级排序规则。Database-level, column-level, and expression-level collations are supported normally. 包含的数据库遵循包含数据库的排序规则所定义的元数据和 tempdb 排序规则。Contained databases follow the metadata and tempdb collations rules defined by Contained Database Collations.

限制Restrictions

  • LocalDB 不能是合并复制订阅服务器。LocalDB cannot be a merge replication subscriber.

  • LocalDB 不支持 FILESTREAM。LocalDB does not support FILESTREAM.

  • LocalDB 仅允许 Service Broker 的本地队列。LocalDB only allows local queues for Service Broker.

  • 由于 Windows 文件系统重定向,NT AUTHORITY\SYSTEM 等内置帐户拥有的 LocalDB 的一个实例可能具有管理性问题。An instance of LocalDB owned by the built-in accounts such as NT AUTHORITY\SYSTEM can have manageability issues due to windows file system redirection. 请改用常规 Windows 帐户作为所有者。Instead use a normal windows account as the owner.

自动实例和命名实例Automatic and named instances

LocalDB 支持两种类型的实例:自动实例和命名实例。LocalDB supports two kinds of instances: Automatic instances and named instances.

  • LocalDB 的自动实例是公共的。Automatic instances of LocalDB are public. 系统自动为用户创建和管理此类实例,并可由任何应用程序使用。They are created and managed automatically for the user and can be used by any application. 安装在用户计算机上的每个 LocalDB 版本都存在一个自动 LocalDB 实例。One automatic instance of LocalDB exists for every version of LocalDB installed on the user's computer. 自动 LocalDB 实例提供无缝的实例管理。Automatic instances of LocalDB provide seamless instance management. 无需创建实例;它可以自动执行工作。There is no need to create the instance; it just works. 此功能使得应用程序可以轻松地安装和迁移到另一台计算机。This feature allows for easy application installation and migration to a different computer. 如果目标计算机已安装指定版本的 LocalDB,则目标计算机也提供此版本的自动 LocalDB 实例。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. 自动 LocalDB 实例具有属于保留命名空间的特殊实例名称模式。Automatic instances of LocalDB have a special pattern for the instance name that belongs to a reserved namespace. 自动实例可以防止名称与 LocalDB 的命名实例发生冲突。Automatic instances prevents name conflicts with named instances of LocalDB. 自动实例的名称为 MSSQLLocalDBThe name for the automatic instance is MSSQLLocalDB.

  • LocalDB 的命名实例是专用的。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. 命名实例必须由用户通过 LocalDB 管理 API 显式创建,或者通过托管应用程序的 app.config 文件隐式创建(尽管托管应用程序也会在需要时使用 API)。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). LocalDB 的每个命名实例都具有关联的 LocalDB 版本,指向相应的 LocalDB 二进制文件集。Each named instance of LocalDB has an associated LocalDB version that points to the respective set of LocalDB binaries. LocalDB 的命名实例为 sysname 数据类型并且可具有最多 128 个字符。The instance name of a LocalDB is sysname data type and can have up to 128 characters. (这不同于常规的 SQL ServerSQL Server 命名实例,此类命名实例将名称限制为 16 个 ASCII 字符的常规 NetBIOS 名称。)LocalDB 实例名称可包含任何在文件名内合法的 Unicode 字符。使用自动实例名称的命名实例将成为自动实例。(This differs from regular named instances of SQL ServerSQL 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.

不同的计算机用户可具有同名的实例。Different users of a computer can have instances with the same name. 每个实例都是以不同的用户身份运行的不同的进程。Each instance is a different processes running as a different user.

LocalDB 的共享实例Shared instances of LocalDB

为了支持多个计算机用户需要连接到单个 LocalDB 实例的方案,LocalDB 支持实例共享。To support scenarios where multiple users of the computer need to connect to a single instance of LocalDB, LocalDB supports instance sharing. 实例所有者可以选择允许计算机上的其他用户连接实例。An instance owner can choose to allow the other users on the computer to connect the instance. LocalDB 的自动实例和命名实例都可以共享。Both automatic and named instances of LocalDB can be shared. 若要共享 LocalDB 的某个实例,用户需要为其选择一个共享名称(别名)。To share an instance of LocalDB, a user selects a shared name (alias) for it. 因为该共享名称对于该计算机的所有用户都是可见的,则此共享名称在计算机上必须唯一。Because the shared name is visible to all users of the computer, this shared name must be unique on the computer. LocalDB 实例的共享名称具有与 LocalDB 的命名实例相同的格式。The shared name for an instance of LocalDB has the same format as the named instance of LocalDB.

只有计算机上的管理员才能创建 LocalDB 的共享实例。Only an administrator on the computer can create a shared instance of LocalDB. LocalDB 的共享实例可由管理员或 LocalDB 共享实例的所有者取消共享。A shared instance of LocalDB can be unshared by an administrator or by the owner of the shared instance of LocalDB. 若要共享和取消共享某一 LocalDB 实例,请使用 LocalDB API 的 LocalDBShareInstanceLocalDBUnShareInstance 方法,或者使用 SqlLocalDb 实用工具的共享和取消共享选项。To share and unshared an instance of LocalDB, use the LocalDBShareInstance and LocalDBUnShareInstance methods of the LocalDB API, or the share and unshared options of the SqlLocalDb utility.

启动 LocalDB 并连接到 LocalDBStart LocalDB and connect to LocalDB

连接到自动实例Connect to the automatic instance

使用 LocalDB 的最简单方法是通过使用连接字符串 Server=(localdb)\MSSQLLocalDB;Integrated Security=true 连接到当前用户拥有的自动实例。The easiest way to use LocalDB is to connect to the automatic instance owned by the current user by using the connection string Server=(localdb)\MSSQLLocalDB;Integrated Security=true. 若要通过使用文件名连接到特定数据库,请使用类似于 Server=(LocalDB)\MSSQLLocalDB; Integrated Security=true ;AttachDbFileName=D:\Data\MyDB1.mdf 的连接字符串进行连接。To connect to a specific database by using the file name, connect using a connection string similar to Server=(LocalDB)\MSSQLLocalDB; Integrated Security=true ;AttachDbFileName=D:\Data\MyDB1.mdf.

在 SQL Server 2014 中,LocalDB 格式的命名约定和连接字符串发生了变化。The naming convention and connection string for LocalDB format changed in SQL Server 2014. 以前,实例名称是一个 v 字符,后跟 LocalDB 和版本号。Previously, the instance name was a single v character followed by LocalDB and the version number. 自 SQL Server 2014 起,不再支持这种实例名称格式,而应改用前面提到的连接字符串。Starting with SQL Server 2014, this instance name format is no longer supported, and the connection string mentioned previously should be used instead.

备注

  • 在计算机上的用户首次尝试连接到 LocalDB 时,必须创建并启动该自动实例。The first time a user on a computer tries to connect to LocalDB, the automatic instance must be both created and started. 创建实例所用的额外时间可能会导致连接尝试失败并且具有超时消息。The extra time for the instance to be created can cause the connection attempt to fail with a timeout message. 在发生此情况时,等待几秒钟以便让创建过程完成,然后再次连接。When this happens, wait a few seconds to let the creation process complete, and then connect again.

创建并连接到命名实例Create and connect to a named instance

除了自动实例之外,LocalDB 还支持命名实例。In addition to the automatic instance, LocalDB also supports named instances. 使用 SqlLocalDB.exe 程序可以创建、启动和停止 LocalDB 的命名实例。Use the SqlLocalDB.exe program to create, start, and stop a named instance of LocalDB. 有关 SqlLocalDB.exe 的详细信息,请参阅 SqlLocalDB 实用工具For more information about SqlLocalDB.exe, see SqlLocalDB Utility.

REM Create an instance of LocalDB
"C:\Program Files\Microsoft SQL Server\130\Tools\Binn\SqlLocalDB.exe" create LocalDBApp1
REM Start the instance of LocalDB
"C:\Program Files\Microsoft SQL Server\130\Tools\Binn\SqlLocalDB.exe" start LocalDBApp1
REM Gather information about the instance of LocalDB
"C:\Program Files\Microsoft SQL Server\130\Tools\Binn\SqlLocalDB.exe" info LocalDBApp1

上面的最后一行将返回如下信息。The last line above, returns information similar to the following.

类别Category Value
名称Name LocalDBApp1
版本Version <Current Version>
共享名称Shared name """"
所有者Owner "<Your Windows User>""<Your Windows User>"
自动创建Auto create No
状态State “正在运行”running
上次启动时间Last start time <Date and Time>
实例管道名称Instance pipe name np:\\.\pipe\LOCALDB#F365A78E\tsql\querynp:\\.\pipe\LOCALDB#F365A78E\tsql\query

备注

如果应用程序使用早于 .NET 4.0.2 的版本,则必须直接连接到 LocalDB 的命名管道。If your application uses a version of .NET before 4.0.2 you must connect directly to the named pipe of the LocalDB. 实例管道名称值为 LocalDB 的实例正在侦听的命名管道。The Instance pipe name value is the named pipe that the instance of LocalDB is listening on. LOCALDB# 之后的实例管道名称部分将在每次启动 LocalDB 实例时更改。The portion of the Instance pipe name after LOCALDB# will change each time the instance of LocalDB is started. 若要通过使用 SQL Server Management StudioSQL Server Management Studio 连接到 LocalDB 实例,请在“连接到 数据库引擎Database Engine”对话框的“服务器名称”框中键入实例管道名称。To connect to the instance of LocalDB by using SQL Server Management StudioSQL Server Management Studio, type the instance pipe name in the Server name box of the Connect to 数据库引擎Database Engine dialog box. 在自定义程序中,可以通过使用类似于 SqlConnection conn = new SqlConnection(@"Server=np:\\.\pipe\LOCALDB#F365A78E\tsql\query"); 的连接字符串建立与 LocalDB 实例的连接From your custom program you can establish connection to the instance of LocalDB using a connection string similar to SqlConnection conn = new SqlConnection(@"Server=np:\\.\pipe\LOCALDB#F365A78E\tsql\query");

连接到 LocalDB 的共享实例Connect to a shared instance of LocalDB

若要连接到某一 LocalDB 共享实例,请将 \.\(反斜杠 + 句点 + 反斜杠)添加到连接字符串以便引用为共享实例保留的命名空间。To connect to a shared instance of LocalDB add \.\ (backslash + dot + backslash) to the connection string to reference the namespace reserved for shared instances. 例如,若要连接到名为 AppData 的 LocalDB 的共享实例,可使用连接字符串(例如 (localdb)\.\AppData)作为连接字符串的一部分。For example, to connect to a shared instance of LocalDB named AppData use a connection string such as (localdb)\.\AppData as part of the connection string. 连接到用户不拥有的 LocalDB 共享实例的用户必须具有 Windows 身份验证或 SQL ServerSQL Server 身份验证登录名。A user connecting to a shared instance of LocalDB that they do not own must have a Windows Authentication or SQL ServerSQL Server Authentication login.

疑难解答Troubleshooting

有关排除 LocalDB 问题的信息,请参阅排除 SQL Server 2012 Express LocalDB 问题For information about troubleshooting LocalDB, see Troubleshooting SQL Server 2012 Express LocalDB.

权限Permissions

SQL Server Express LocalDB 的实例是用户根据需要创建的实例。An instance of SQL Server Express LocalDB is an instance created by a user for their use. 计算机上的任何用户都可以使用 LocalDB 实例创建数据库,在用户配置文件下存储文件并使用凭据来运行进程。Any user on the computer can create a database using an instance of LocalDB, store files under their user profile, and run the process under their credentials. 默认情况下,对 LocalDB 实例的访问仅限于其所有者。By default, access to the instance of LocalDB is limited to its owner. LocalDB 中包含的数据受到对数据库文件的文件系统访问的保护。The data contained in the LocalDB is protected by file system access to the database files. 如果用户数据库文件存储于某一共享位置,则通过使用他们自己拥有的 LocalDB 实例对该位置具有文件系统访问权限的任何人都可以打开该数据库。If user database files are stored in a shared location, the database can be opened by anyone with file system access to that location by using an instance of LocalDB that they own. 如果数据库文件处于某一受保护的位置,例如用户数据文件夹,则只有该用户以及有权访问该文件夹的任何管理员才能打开该数据库。If the database files are in a protected location, such as the users data folder, only that user, and any administrators with access to that folder, can open the database. LocalDB 文件只能一次通过一个 LocalDB 实例来打开。The LocalDB files can only be opened by one instance of LocalDB at a time.

备注

LocalDB 始终在用户安全上下文中运行;即 LocalDB 从不使用本地 Administrator 组的凭据来运行。LocalDB always runs under the users security context; that is, LocalDB never runs with credentials from the local Administrator's group. 这意味着 LocalDB 实例使用的所有数据库文件必须可以通过拥有的用户的 Windows 帐户来访问,而不必考虑本地 Administrators 组中的成员资格。This means that all database files used by a LocalDB instance must be accessible using the owning user's Windows account, without considering membership in the local Administrators group.

另请参阅See Also

SqlLocalDB 实用工具SqlLocalDB Utility