SQL Server 2014 Express LocalDBSQL Server 2014 Express LocalDB

MicrosoftMicrosoft SQL Server 2014 ExpressSQL Server 2014 Express LocalDB 執行模式SQL Server ExpressSQL Server Express專供程式開發人員。SQL Server 2014 ExpressSQL Server 2014 Express LocalDB is an execution mode of SQL Server ExpressSQL Server Express targeted to program developers. LocalDB 安裝會複製檔案啟動所需的最小集合SQL Server Database EngineSQL Server Database EngineLocalDB installation copies a minimal set of files necessary to start the SQL Server Database EngineSQL Server Database Engine. 一次LocalDB是安裝,開發人員起始的連線使用特殊連接字串。Once LocalDB is installed, developers initiate a connection by using a special connection string. 連接時,就會自動建立及啟動必要的 [SQL Server]SQL Server 基礎結構,應用程式不需複雜或耗時的組態工作即可開始使用資料庫。When connecting, the necessary [SQL Server]SQL Server infrastructure is automatically created and started, enabling the application to use the database without complex or time consuming configuration tasks. Developer Tools 為開發人員提供 SQL Server Database EngineSQL Server Database Engine ,讓他們撰寫和測試 Transact-SQLTransact-SQL 程式碼,而不需要管理 [SQL Server]SQL Server的完整伺服器執行個體。Developer Tools can provide developers with a SQL Server Database EngineSQL Server Database Engine that lets them write and test Transact-SQLTransact-SQL code without having to manage a full server instance of [SQL Server]SQL Server. 執行個體SQL Server ExpressSQL Server ExpressLocalDBSqlLocalDB.exe公用程式。An instance of SQL Server ExpressSQL Server ExpressLocalDB is managed by using the SqlLocalDB.exe utility. SQL Server ExpressSQL Server ExpressLocalDB 應該用於取代SQL Server ExpressSQL Server Express使用者執行個體功能已被取代。LocalDB should be used in place of the SQL Server ExpressSQL Server Express user instance feature which is deprecated.

安裝 LocalDBInstalling LocalDB

安裝的主要方法LocalDB是使用 SqlLocalDB.msi 程式。The primary method of installing LocalDB is by using the SqlLocalDB.msi program. LocalDB 安裝的任何 SKU 時SQL Server 2014 ExpressSQL Server 2014 ExpressLocalDB is an option when installing any SKU of SQL Server 2014 ExpressSQL Server 2014 Express. 選取 LocalDB特徵選取頁面上,安裝期間SQL Server ExpressSQL Server ExpressSelect LocalDB on the Feature Selection page during installation of SQL Server ExpressSQL Server Express. 可以有一份LocalDB每個主要的二進位檔SQL Server Database EngineSQL Server Database Engine版本。There can be only one installation of the LocalDB binary files for each major SQL Server Database EngineSQL Server Database Engine version. 多個 Database EngineDatabase Engine 處理序可以啟動,而且全部都會使用相同的二進位檔案。Multiple Database EngineDatabase Engine processes can be started and will all use the same binaries. 執行個體SQL Server Database EngineSQL Server Database Engine啟動為LocalDB具有相同的限制 SQL Server ExpressSQL Server ExpressAn instance of the SQL Server Database EngineSQL Server Database Engine started as the LocalDB has the same limitations as SQL Server ExpressSQL Server Express

描述Description

LocalDB安裝程式使用 SqlLocalDB.msi 程式來安裝必要的檔案,在電腦上。The LocalDB setup program uses the SqlLocalDB.msi program to install the necessary files on the computer. 安裝之後,LocalDB的執行個體SQL Server ExpressSQL Server Express可建立及開啟[SQL Server]SQL Server資料庫。Once installed, LocalDB is an instance of SQL Server ExpressSQL Server Express that can create and open [SQL Server]SQL Server databases. 資料庫的系統資料庫檔案儲存在使用者本機上通常處於隱藏狀態的 AppData 路徑。The system database files for the database are stored in the users' local AppData path which is normally hidden. 例如 C:\Users\<使用者>\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\<使用者>\Documents\ 資料夾中的某個位置。User database files are stored where the user designates, typically somewhere in the C:\Users\<user>\Documents\ folder.

如需詳細資訊,包括LocalDB應用程式,請參閱Visual StudioVisual Studio文件本機資料概觀逐步解說:建立 SQL Server LocalDB 資料庫,和逐步解說:連接到 SQL Server LocalDB 資料庫 (Windows Form) 中的資料For more information about including LocalDB in an application, see the Visual StudioVisual Studio documentation Local Data Overview, Walkthrough: Creating a SQL Server LocalDB Database, and Walkthrough: Connecting to Data in a SQL Server LocalDB Database (Windows Forms).

如需詳細資訊LocalDBAPI,請參閱 < SQL Server Express LocalDB 執行個體 API 參考LocalDBStartInstance 函數For more information about the LocalDB API, see SQL Server Express LocalDB Instance API Reference and LocalDBStartInstance Function.

SqlLocalDb 公用程式可以建立的新執行個體LocalDB、 啟動及停止的執行個體LocalDB,並包含選項,可協助您管理LocalDBThe SqlLocalDb utility can create new instances of LocalDB, start and stop an instance of LocalDB, and includes options to help you manage LocalDB. 如需 SqlLocalDb 公用程式的詳細資訊,請參閱 SqlLocalDB 公用程式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. 自主資料庫遵循 Contained Database Collations所定義的中繼資料和 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.

執行個體LocalDB擁有的內建帳戶,例如 NT AUTHORITY\SYSTEM 可以有管理能力問題,因為 windows 檔案系統重新導向;改為使用一般 windows 帳戶的擁有者。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; 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 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. 這可防止名稱衝突的具名執行個體LocalDBThis 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. 執行個體名稱LocalDBsysname資料類型,可以包含最多可有 128 個字元。The instance name of a LocalDB is sysname data type and can have up to 128 characters. (這不同於 [SQL Server]SQL Server 的一般具名執行個體,其名稱限制為 16 個 ASCII 字元的一般 NetBIOS 名稱)。執行個體的名稱LocalDB可以包含任何合法的檔案名稱內的 Unicode 字元。(This differs from regular named instances of [SQL Server]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.

電腦的不同使用者可有同名的執行個體。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

若要支援多個使用者的電腦需要連線到的單一執行個體的案例LocalDBLocalDB支援執行個體共用。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 to his 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具有相同格式的具名執行個體LocalDBThe shared name for an instance of LocalDB has the same format as the named instance of LocalDB.

只有在電腦上的系統管理員可以建立的共用執行個體LocalDBOnly an administrator on the computer can create a shared instance of LocalDB. 共用執行個體LocalDB可以是由系統管理員,或共用執行個體的擁有者取消共用LocalDBA shared instance of LocalDB can be unshared by an administrator or by the owner of the shared instance of LocalDB. 共用及取消共用的執行個體LocalDB,使用LocalDBShareInstanceLocalDBUnShareInstance方法LocalDBAPI,或共用及取消共用的選項 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 以及連接到 LocalDBStarting LocalDB and Connecting to LocalDB

連接到自動執行個體Connecting to the Automatic Instance

若要使用的最簡單方式LocalDB是連接到自動執行個體所使用的連接字串中目前使用者所擁有 "Server = (localdb) \MSSQLLocalDB;Integrated 安全性 = 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".

注意

第一次在電腦上的使用者嘗試連線到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.

建立及連接到具名執行個體Creating and Connecting to a Named Instances

除了自動執行個體,LocalDB也支援具名執行個體。In addition to the automatic instance, LocalDB also supports named instances. 使用 SqlLocalDB.exe 程式來建立、 啟動及停止的具名執行個體LocalDBUse the SqlLocalDB.exe program to create, start, and stop an 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\120\Tools\Binn\SqlLocalDB.exe" create LocalDBApp1  
REM Start the instance of LocalDB  
"C:\Program Files\Microsoft SQL Server\120\Tools\Binn\SqlLocalDB.exe" start LocalDBApp1  
REM Gather information about the instance of LocalDB  
"C:\Program Files\Microsoft SQL Server\120\Tools\Binn\SqlLocalDB.exe" info LocalDBApp1  

以上最後一行傳回的資訊如下所示。The last line above, returns information similar to the following.

名稱Name "LocalDBApp1""LocalDBApp1"
VersionVersion <目前版本><Current Version>
共用名稱Shared name """"
[擁有者]Owner "<您的 Windows 使用者>""<Your Windows User>"
自動建立Auto create No
StateState 執行中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 之前的版本您必須直接連接到的具名管道LocalDBIf 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. 若要連接到的執行個體LocalDB利用SQL Server Management StudioSQL Server Management Studio,輸入執行個體管道名稱伺服器名稱方塊連接到Database EngineDatabase 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 EngineDatabase Engine dialog box. 從您的自訂程式中,您可以建立執行個體的連線LocalDB使用類似的連接字串 SqlConnection conn = new SqlConnection(@"Server=np:\\.\pipe\LOCALDB#F365A78E\tsql\query");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 的共用執行個體Connecting to a Shared Instance of LocalDB

若要連接到的共用執行個體LocalDB新增 。\ (點 + 反斜線) 來參考保留給共用執行個體的命名空間的連接字串。To connect to a shared instance of LocalDB add .\ (dot + backslash) to the connection string to reference the namespace reserved for shared instances. 例如,若要連接到的共用執行個體LocalDB名為AppData使用的連接字串,例如(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 Server]SQL Server驗證登入。A user connecting to a shared instance of LocalDB that they do not own must have a Windows Authentication or [SQL Server]SQL Server Authentication login.

疑難排解Troubleshooting

如需疑難排解資訊LocalDB,請參閱 < 疑難排解 SQL Server 2012 Express LocalDBFor information about troubleshooting LocalDB, see Troubleshooting SQL Server 2012 Express LocalDB.

PermissionsPermissions

執行個體SQL Server 2014 ExpressSQL Server 2014 ExpressLocalDB是供其使用使用者所建立的執行個體。An instance of SQL Server 2014 ExpressSQL Server 2014 ExpressLocalDB 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, storing files under their user profile and running 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絕對不會執行使用的認證從本機系統管理員群組。LocalDB always runs under the users security context; that is, LocalDB never runs with credentials from the local Administrator's group. 這表示所有資料庫所使用的檔案LocalDB執行個體必須能夠使用擁有使用者的 Windows 帳戶,而不必考量本機系統管理員群組的成員資格。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