对于程序员来说,编程过程中或多或少会和数据库打交道。如果采用Visual Studio进行程序开发,则微软的Sql Server数据库是最好的选择。但是问题来了,Sql Server数据库动辄几个G,安装后占用的空间也相当大,是不是每个开发人员在开发时都需要安装Sql Server呢?其实,对于小型项目、测试型项目、学习型项目的开发,完全没必要使用Sql Server那么高大上的数据库。微软自己也深知这点,因此,推出了Sql Server数据库的超级简化版本:Sql Server LocalDB。这个小型的数据库完全可以满足普通项目的开发和调试,关键是它只有几十M,可以大大减轻PC的运行压力。本文将简要介绍在Visual Studio 2015中LocalDB数据库的使用方法。

一、LocalDB的安装

    在安装VS2015时会自动安装LocalDB,所以只要正确安装VS2015那么localDB是肯定有的。

二、LocalDB的连接和管理

    进入VS2015,在“视图”中选择“Sql Server对象资源管理器”,可以看到如下的界面。

VS自带的LocalDB数据库的用法_资源管理器

    右键单击Sql Server,可以选择“添加Sql Server”。

VS自带的LocalDB数据库的用法_Server_02

    在弹出的窗体中浏览本地,可以看见有两个数据库实例:MSSQLLocalDB和ProjectsV13。这两个实例都是VS2015的自带LocalDB,之所以有两个,是因为楼主的VS2015进行过更新,MSSQLLocalDB是属于V12版本的LocalDB,ProjectsV13是属于V13版本的LocalDB,二者目前都可以被VS2015的工程使用。接下来以MSSQLLocalDB为例进行连接。成功连接后左侧的资源管理器就可以对数据库的资源进行管理。

VS自带的LocalDB数据库的用法_资源管理器_03

    然后我们可以尝试创建一个新的数据库Test。

VS自带的LocalDB数据库的用法_数据库_04

    成功创建Test数据库后,可以正常地数据库进行操作。然而,如果要连接外部的数据库文件,就无法直接在Sql Server资源管理器中进行操作,此时需要在服务器资源管理器中进行操作。过程如下:

在服务器资源管理器中选择添加连接,如果直接连接外部数据库文件,则该文件会默认附加到LocalDB中:

VS自带的LocalDB数据库的用法_资源管理器_05

在弹出的窗体中选择“浏览”来添加外部数据库文件:

VS自带的LocalDB数据库的用法_资源管理器_06

选中文件后点击“确定”,服务器资源管理器中就多了一个外部数据库文件的连接:

VS自带的LocalDB数据库的用法_数据库_07

再次查看Sql Server对象资源管理器中的MSSQLLocalDB,可以看见多了刚才添加的外部数据库被附加到了这个本地数据库中:

VS自带的LocalDB数据库的用法_数据库_08

至此,外部数据库也附加完毕,如果要在项目中连接该数据库,连接字符串如下:

"Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=外部数据库文件的绝对路径(注意转义字符)”。

 

命令行操作:

At the command line, you can interact using the program name SqlLocalDb. Start with getting information about the installation:


C:\> SqlLocalDb info


Result:


MSSQLLocalDB


This used to return the version number (in the original article, and with the 2012 release, this returned v11.0.

Next, you can create an instance with the following command:


C:\> SqlLocalDb create "MyInstance"


Result:


LocalDB instance "MyInstance" created with version 14.0.3030.27.


Check the info:


C:\> SqlLocalDb info "MyInstance"


Result:


Name:                MyInstance Version:             14.0.3030.27 Shared name: Owner:               PEDRO\aaronbertrand Auto-create:         No State:               Running Last start time:     7/20/18 10:44:51 AM Instance pipe name:  np:\\.\pipe\LOCALDB#9EBB1CD2\tsql\query


The Instance pipe name may come in handy later, though I’ve found that a lot of the connectivity issues in earlier versions of this feature have gone away. Also, in older versions you had to explicitly start the instance, but it now starts automatically.

If you want to stop and drop the instance, use:


C:\> SqlLocalDb stop "MyInstance" C:\> SqlLocalDb delete "MyInstance"


But don’t do that just yet. Evidence that this all works so far:

VS自带的LocalDB数据库的用法_Server_09

Connect using SQLCMD

Locate SQLCMD, making sure to use the newest version on your machine (your environment path may list an older version first). Look for the highest version in the Binn folders under C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\[version]\Tools\. You can connect to this instance using the following from the command line:


sqlcmd -S "(localdb)\MyInstance"


Then we’re greeted with a line number prompt and can enter commands at will. So something like:


SELECT @@VERSION; GO CREATE DATABASE blat; GO USE blat; GO CREATE TABLE dbo.splunge(Mort int); GO INSERT dbo.Splune(Mort) VALUES(1); SELECT * FROM dbo.splunge; GO


Yields:

VS自带的LocalDB数据库的用法_Server_10

 


https://www.mssqltips.com/sqlservertip/5612/getting-started-with-sql-server-2017-express-localdb/