1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > ef 连接localdb_如何连接和使用Microsoft SQL Server Express LocalDB

ef 连接localdb_如何连接和使用Microsoft SQL Server Express LocalDB

时间:2024-01-20 18:32:46

相关推荐

ef 连接localdb_如何连接和使用Microsoft SQL Server Express LocalDB

ef 连接localdb

Microsoft SQL Server Express LocalDB is intended for developers, it is very easy to install and doesn’t require any complex configuration task to create an instance or to use the database. The Introduction on the installation of Local DB be found on “How to install Microsoft SQL Server Express LocalDB” article.

Microsoft SQL Server Express LocalDB适用于开发人员,它非常易于安装,并且不需要任何复杂的配置任务即可创建实例或使用数据库。 在“ 如何安装Microsoft SQL Server Express LocalDB ”一文中可以找到有关Local DB安装的简介。

This article will show different ways of connecting to LocalDB and how to create and use an instance of LocalDB.

本文将展示连接到LocalDB的不同方法以及如何创建和使用LocalDB的实例。

LocalDB runs on demand, which means that the LocalDB processes can start and be stopped automatically when needed. In practice, this will happen when a computer is started and there is no LocalDB process running. So, there are no resources dedicated to servicing a database server during one’s daily work routine. When an application that uses Microsoft SQL Server Express LocalDB is run and a connection is made to the database, the LocalDB process is started on the computer aka on-demand. When the last connection from the application is closed, after a short delay, then the LocalDB process ends.

LocalDB按需运行,这意味着LocalDB进程可以在需要时自动启动和停止。 实际上,这将在计算机启动且没有LocalDB进程运行时发生。 因此,在日常工作中没有专门用于维护数据库服务器的资源。 当运行使用Microsoft SQL Server Express LocalDB的应用程序并建立与数据库的连接时,也按需在计算机上启动LocalDB进程。 当关闭应用程序的最后一个连接时,经过短暂的延迟后,LocalDB进程结束。

Also, the attached database file name property can be used for the database connection string. In this way, the location of the database file can be controlled. The database file doesn’t have to be searched in some centralized SQL Server location, but it can be found in user’s Application Install folder. This means that the work is only being done with a database file, not with configuring and using a database server.

另外,附加的数据库文件名属性可用于数据库连接字符串。 这样,可以控制数据库文件的位置。 不必在某个集中SQL Server位置中搜索数据库文件,但可以在用户的​​“应用程序安装”文件夹中找到该文件。 这意味着工作仅通过数据库文件完成,而不是通过配置和使用数据库服务器完成。

Once Microsoft SQL Server Express LocalDB is installed, using SqlLocalDB Utility can be seen as information about the LocalDB instances installed on the machine. By typing theSqlLocalDb infocommand and pressing the Enter key from the keyboard in the Command Prompt window, the following instances may be found:

一旦安装了Microsoft SQL Server Express LocalDB,就可以将使用SqlLocalDB Utility视为有关计算机上安装的LocalDB实例的信息。 通过键入SqlLocalDb info命令并在“ 命令提示符”窗口中从键盘上按Enter键,可以找到以下实例:

The v11.0 and MSSQLLocalDB instances are automatic Microsoft SQL Server Express LocalDB instances and come with installation of Microsoft SQL Server Express LocalDB.

v11.0和MSSQLLocalDB实例是自动Microsoft SQL Server Express LocalDB实例,并随Microsoft SQL Server Express LocalDB一起安装。

Depending on which version of Microsoft SQL Server Express LocalDB is installed on the machine, the different automatic LocalDB instance will appear. For example, if only Microsoft SQL Server Express LocalDB is installed, when the available LocalDB instances on a machine using theSqlLocalDb infocommand are listed, the v11.0 LocalDB instance will appear. But, if Microsoft SQL Server Express LocalDB or a newer one is installed, the MSSQLLocalDB instance will appear as the automatic instance.

根据计算机上安装的Microsoft SQL Server Express LocalDB版本,将显示不同的自动LocalDB实例。 例如,如果仅安装Microsoft SQL Server Express LocalDB,则列出使用SqlLocalDb info命令的计算机上的可用LocalDB实例时,将显示v11.0 LocalDB实例。 但是,如果安装了Microsoft SQL Server Express LocalDB或较新版本,则MSSQLLocalDB实例将显示为自动实例。

Naming conventions of the automatic v11. 0 LocalDB instance can be taken from this link and naming conventions for the automatic MSSQLLocalDB LocalDB instance can be found on this link.

自动v11的命名约定。 0从此链接可以获取LocalDB实例,并且可以在此链接上找到自动MSSQLLocalDB LocalDB实例的命名约定。

To see the other details about instances, for example v11.0 instance, type the following command:

SQLLocalDB info v11.0

要查看有关实例的其他详细信息,例如v11.0实例,请键入以下命令:

SQLLocalDB信息v11.0

The result will be as follows:

结果将如下所示:

Namestands for the name of the LocalDB instance,

Name代表LocalDB实例的名称,

Versionshows the full version of the LocalDB instance,

版本显示LocalDB实例的完整版本,

Shared共享nameshares the instance using the specified shared name, when multiple users need to connect to a single instance of Microsoft SQL Server Express LocalDB.的股票使用指定的共享名称,当多个用户需要连接到Microsoft SQL Server Express的的LocalDB的单一实例的实例。

Type the following command:SQLLocalDB share v11.0 Marein the Command Prompt window and execute. In the Command Prompt window will show the following message:

键入以下命令:SQLLocalDB share v11.0 Mare在“命令提示符”窗口中并执行。 在命令提示符窗口中将显示以下消息:

Private LocalDB instance “v11.0” shared with the shared name: “Mare”.

与共享名称“ Mare”共享的私有LocalDB实例“ v11.0”。

Make sure that the Command Prompt window is running under the administrator privileges, otherwise the following message may occur:

确保命令提示符窗口正在管理员特权下运行,否则可能会出现以下消息:

Sharing of LocalDB instance “v11.0” with shared name “Mare” failed because

Administrator privileges are required in order to execute this operation.

共享名称为“ Mare”的LocalDB实例“ v11.0”的共享失败,因为

为了执行此操作,需要管理员特权。

Now, when theSQLLocalDB info v11.0command is executed, the result will be this one:

现在,当执行SQLLocalDB info v11.0命令时,结果将是以下内容:

Ownershows the name of the Windows User (the owner of the LocalDB instance)

所有者显示Windows用户的名称(LocalDB实例的所有者)

Auto-createindicates (shows) if LocalDB instance automatic is created or not. If the LocalDB instance is automatically created in the Auto-create section,Yeswill appear. On the other hand,Nowill be shown.

自动创建指示(显示)是否创建了LocalDB实例自动。 如果LocalDB实例在自动创建章节自动创建,将会出现。 另一方面,将显示“否”

Let’s create a new instance and demonstrate this. In the Command Prompt window, type the following command:SqlLocalDB create NewInstanceand press the Enter key. The message will appear which indicates that the LocalDB instance is created:

让我们创建一个新实例并进行演示。 在“命令提示符”窗口中,键入以下命令:SqlLocalDB create NewInstance,然后按Enter键。 该消息将出现,表明已创建LocalDB实例:

LocalDB instance “NewInstance” created with version 13.0.2186.6.

使用版本13.0.2186.6创建的LocalDB实例“ NewInstance”。

Now, when theSqlLocalDB info NewInstancecommand is executed in theAuto-createsection,Nowill be shown:

现在,在“自动创建”部分中执行SqlLocalDB info NewInstance命令时,将显示“否”

As it may be noticed, theversionof theNewInstanceLocalDB is different from theversionof thev11.0LocaDB instance. This happens, when on a machine, more than one version of Microsoft SQL Server Express LocalDB have been installed.

因为它可能是注意到了,NewInstance方法的LocalDB的版本是从V11.0LocaDB实例的版本不同。 如果在一台计算机上安装了多个版本的Microsoft SQL Server Express LocalDB,则会发生这种情况。

Now, when a new instance of LocalDB is created without specifying a version of Microsoft SQL Server Express LocalDB after a name of the LocalDB instance in the creating strings, by default, the higher version of the LocalDB will be used in creating the process of a new LocalDB instance.

现在,当在创建字符串中的LocalDB实例名称之后没有指定Microsoft SQL Server Express LocalDB版本的情况下创建新的LocalDB实例时,默认情况下,将使用更高版本的LocalDB来创建数据库。新的LocalDB实例。

Let’s create a new instance which will have the same version as the v11.0 LocalDB instance. Type the following command:SqlLocalDB create Test 11.0in the Command Prompt window and press the Enter key. The following message will appear:

让我们创建一个新实例,该实例将具有与v11.0 LocalDB实例相同的版本。 键入以下命令:SqlLocalDB在“命令提示符”窗口中创建Test 11.0,然后按Enter键。 将显示以下消息:

LocalDB instance “Test” created with version 11.0.2318.0.

使用11.0.2318.0版本创建的LocalDB实例“ Test”。

Now, when theSqlLocalDB info Testcommand is executed, the result will be this one:

现在,当执行SqlLocalDB info Test命令时,结果将是以下内容:

When creating a new LocalDB instance with the name which have two or more words speared by a space, for exampleTest instance, without putting them in the double quotation mark, the following message will appear:

当创建一个名称新的LocalDB实例时,如果名称中有两个或多个用空格隔开的单词,例如Test instance,而没有将它们放在双引号中,则会出现以下消息:

Creation of LocalDB instance “Test” with version instance failed because of the following error

The parameter for the LocalDB Instance API method is incorrect. Consult the API documentation.

由于以下错误,使用版本实例创建LocalDB实例“ Test”失败

LocalDB Instance API方法的参数不正确。 请查阅API文档。

If executed theSQLLocalDB create “Test instance”command, the following message will appear in the Command Prompt window:

如果执行了SQLLocalDB create“测试实例”命令,则命令提示符窗口中将显示以下消息:

LocalDB instance “Test instance” created with version 11.0.

使用版本11.0创建的LocalDB实例“测试实例”。

Stateshows a current state of a chosen LocalDB instance, if it isRunningor itStopped.To run, for example, the Test LocalDB instance, in the Command Prompt window, the following command should be typed:SqlLocalDB start Test.The result after starting the Test LocalDB instance is:

状态显示所选LocalDB实例的当前状态(如果正在运行或已停止)。若要运行例如Test LocalDB实例,请在“命令提示符”窗口中键入以下命令:SqlLocalDB start Test。启动Test LocalDB实例后的结果是:

Now, theStateis changed to “Running” and theInstance pipe namesection has a value. This value (string) is used as the connection string to the Test LocalDB instance from another application.

现在,状态更改为“正在运行”,并且“实例管道名称”部分具有值。 该值(字符串)用作从另一个应用程序到Test LocalDB实例的连接字符串。

Now, when an instance is created and started, it is not possible to do other things like creating databases and running queries since SqlLocalDb does not provide an interface to the engine by itself as it does not provide a means to interact with databases. Still, there are several other options to connect to and interact with SqlLocalDb instances.

现在,创建和启动实例后,将无法执行其他操作,例如创建数据库和运行查询,因为SqlLocalDb本身不提供与引擎交互的方式,因此它本身并未提供与引擎的接口。 尽管如此,还有其他几个选项可以连接到SqlLocalDb实例并与之交互。

For example, if the string from theInstance pipe nameis pasted in the Server name field under the Connect to Server window of SQL Server Management Studio:

例如,如果实例管道名称中的字符串粘贴在SQL Server Management Studio的“连接到服务器”窗口下的“服务器名称”字段中:

And theConnectbutton is pressed, the connection to the Test LocalDB instance will be established:

然后按下“连接”按钮,将建立到Test LocalDB实例的连接:

Now, when the connection is established, the same T-SQL code can be used in LocalDB as it is in SQL Server Express when creating databases, tables, stored procedures etc.

现在,建立连接后,在创建数据库,表,存储过程等时,可以在LocalDB中使用与在SQL Server Express中相同的T-SQL代码。

There are some restrictions related to LocalDB comparing to SQL Server Express, which can be seen here.

与SQL Server Express相比,LocalDB有一些限制,可以在此处查看 。

Another way to connect to the LocalDB instance is to type the (LocalDB)\ followed by the name of the LocalDB instance (v11.0) in the Server name combo box of the Connect to Server window and to click theConnectbutton:

连接到LocalDB实例的另一种方法是在“连接到服务器”窗口的“服务器名称”组合框中键入(LocalDB)\,然后输入LocalDB实例的名称(v11.0),然后单击“连接”按钮:

In the Object Explorer window, the result will be like this:

在“对象资源管理器”窗口中,结果将如下所示:

If in the Server name combo box of the Connect to Server window is entered (LocalDB)\ followed by a dot ((LocalDB)\.):

如果在“连接到服务器”窗口的“服务器名称”组合框中输入(LocalDB)\,然后输入点号((LocalDB)\。):

The connection will be established with the automatic (default) LocalDB instance:

将使用自动(默认)LocalDB实例建立连接:

This type of connection to the automatic LocalDB instance refers to Microsoft SQL Server Express LocalDB or newer.

与自动LocalDB实例的这种连接是指Microsoft SQL Server Express LocalDB或更高版本。

As it is mentioned in the text above, if more than one LocalDB versions are installed on the machine, the connection will be established with the automatic LocalDB instance with higher (latest) version.

如上文所述,如果计算机上安装了多个LocalDB版本,则将使用具有较高(最新)版本的自动LocalDB实例建立连接。

Let’s delete that automatic LocalDB instance. In this situation, theMSSQLLocalDBinstance is the one that should be deleted. In the Command Prompt window, the following command should be typed:SqlLocalDB delete MSSQLLocalDB. When the Enter key is pressed from the keyboard, the following message may occur:

让我们删除该自动LocalDB实例。 在这种情况下,MSSQLLocalDB实例是应删除的实例。 在命令提示符窗口中,应键入以下命令:SqlLocalDB delete MSSQLLocalDB。 当从键盘上按Enter键时,可能会出现以下消息:

Delete of LocalDB instance “MSSQLLocalDB” failed because of the following error:

Requested operation on LocalDB instance cannot be performed because specified instance is currently in use. Stop the instance and try again.

由于以下错误,删除LocalDB实例“ MSSQLLocalDB”失败:

由于当前正在使用指定的实例,因此无法对LocalDB实例执行请求的操作。 停止实例,然后重试。

As the message says, the automatic MSSQLLocalDB instance is in use. In order to be deleted, first it should be stopped. Type theSqlLocalDB stop MSSQLLocaDBcommand in the Command Prompt window:

如消息所示,正在使用自动MSSQLLocalDB实例。 为了将其删除,首先应将其停止。 在“命令提示符”窗口中键入SqlLocalDB stop MSSQLLocaDB命令:

LocalDB instance “MSSQLLocalDB” stopped.

LocalDB实例“ MSSQLLocalDB”已停止。

Now, repeat the SqlLocalDB delete MSSQLLocalDB command. This will successfully remove LocalDB instance from the machine:

现在,重复SqlLocalDB delete MSSQLLocalDB命令。 这将成功从计算机中删除LocalDB实例:

LocalDB instance “MSSQLLocalDB” deleted.

LocalDB实例“ MSSQLLocalDB”已删除。

Using sqlcmd, the connection with the LocalDB instance, can be established. From there, T-SQL command and various T-SQL queries can be used. In the Command Prompt window, navigate to the SqlLocalDB.exe:cd C:\Program Files\Microsoft SQL Server\110\Tools\Binn\

使用sqlcmd ,可以建立与LocalDB实例的连接。 从那里,可以使用T-SQL命令和各种T-SQL查询。 在“命令提示符”窗口中,导航到SqlLocalDB.exe:cd C:\ Program Files \ Microsoft SQL Server \ 110 \ Tools \ Binn \

Also, type the following command:sqlcmd-S (localDB)\v11.0in order to connect to v11.0 LocaDB instance.

另外,键入以下命令:sqlcmd-S(localDB)\ v11.0以连接到v11.0 LocaDB实例。

When there is the connection to the v11.0 LocalDB instance, a database, a table, etc. can be created. Past the following query in the Command Prompt window:

当存在到v11.0 LocalDB实例的连接时,可以创建数据库,表等。 在“命令提示符”窗口中通过以下查询:

CREATE DATABASE TestSQlCMD;GOUSE TestSQlCMD;GOCREATE TABLE dbo.Pesron(ID INT,Name VARCHAR(50));GO

Now, let’s connect to the v11.0 LocaDB instance via SSMS:

现在,让我们通过SSMS连接到v11.0 LocaDB实例:

As it can be seen, in the Object Explorer window, theTestSQlCMDdatabase with thePesrontable was created in the v11.0 LocalDB instance:

可以看到,在“对象资源管理器”窗口中,带有Pesron表的TestSQlCMD数据库是在v11.0 LocalDB实例中创建的:

连接到LocalDB的共享实例 (Connecting to a shared Instance of LocalDB)

To connect to a shared instance of LocalDB add.\between (LocalDB)\ and a name of a shared instance. For example(LocalDB)\.\Mare:

要连接到LocalDB的共享实例,请在(LocalDB)\和共享实例的名称之间添加。\。 例如(LocalDB)\。\ Mare

If the .\ is omitted the following message will appear:

如果省略。\,将出现以下消息:

Previous article in this series

本系列的上一篇文章

How to install Microsoft SQL Server Express LocalDB如何安装Microsoft SQL Server Express LocalDB

参考 (Reference)

SQL Server Express LocalDB SQL Server Express本地数据库 SQL Server Express LocalDB SQL Server Express本地数据库 LocalDB: Where is My Database? LocalDB:我的数据库在哪里? SQL Server Express LocalDB Instance API Reference SQL Server Express LocalDB实例API参考

翻译自: /how-to-connect-and-use-microsoft-sql-server-express-localdb/

ef 连接localdb

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。