如何连接到 LocalDB

我使用 SqlLocalDb.msi 包安装了 LocalDb,并且可以使用服务器名称 (LocalDb)\v11.0使用 SSMS 连接它。目前为止还不错。问题是,当我试图通过一个。NET 4.5应用程序我得到的错误

找不到或无法访问服务器。

我从连接字符串 Data Source=(LocalDb)\v11.0开始,然后添加了许多没有用的东西(集成安全 true/sspi、附加数据库、使用实例等等)

159532 次浏览

I think you hit the same issue as discussed in this post. You forgot to escape your \ character.

I was able to connect from SSMS using "(LocalDb)\Projects". That's the way it appears in VS2012 as well.

Use (localdb)\MSSQLLocalDB. That is the LocalDB instance intended for applications, independent of Visual Studio version.


Disregard my original answer: "With SQL Server 2014 Express LocalDB, use (localdb)\ProjectsV12. This works in both Visual Studio 2013 and SQL Server 2014 Management Studio." While ProjectsV12 will indeed give you a LocalDB instance, it's the wrong one, intended for use by SQL Server Data Tools.

I am totally unable to connect to localdb with any tool including MSSMA, sqlcmd, etc. You would think Microsoft would document this, but I find nothing on MSDN. I have v12 and tried (localdb)\v12.0 and that didn't work. Issuing the command sqllocaldb i MSSQLLocalDB shows that the local instance is running, but there is no way to connect to it.

c:\> sqllocaldb i MSSQLLocalDB
Name:               MSSQLLocalDB
Version:            12.0.2000.8
Shared name:
Owner:              CWOLF-PC\cwolf
Auto-create:        Yes
State:              Running
Last start time:    6/12/2014 8:34:11 AM
Instance pipe name: np:\\.\pipe\LOCALDB#C86052DD\tsql\query
c:\>
c:\> sqlcmd -L


Servers:
;UID:Login ID=?;PWD:Password=?;Trusted_Connection:Use Integrated Security=?;
*APP:AppName=?;*WSID:WorkStation ID=?;

I finally figured it out!! the connect string is (localdb)\MSSQLLocalDB, e.g.:

$ sqlcmd -S \(localdb\)\\MSSQLLocalDB
1> select 'hello!'
2> go


------
hello!


(1 rows affected)

You can connect with MSSMS to LocalDB. Type only in SERVER NAME: (localdb)\v11.0 and leave it by Windows Authentication and it connects to your LocalDB server and shows you the databases in it.

Your Connection string should be like`

Data Source=(localdb)\ProjectsV13;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False

Use (localdb)\MSSQLLocalDBwith Windows Auth

 <add name="Default" connectionString="Data Source=(LocalDb)\MSSqlLocalDB; Initial Catalog=CRM_Default_v1; Integrated Security=True"
providerName="System.Data.SqlClient"/>

your web.config files in visual studio under connectiionString or Go to View > SQL Server Object Viewer > Add Sql Server> add your server there

Suppose: SqlConnection connectionObj = new SqlConnection()

for : connectionObj.ConnectionString -> use server name : (localdb)\\MSSQLLocalDB.

Note: Double back slash

for : App.config -> use server name : (localdb)\MSSQLLocalDB

Note: Single back slash

To locate a DB from SQL Server management studio can be done through browse - in the connect to database screen

enter image description here

Also make sure a local database is installed during installation:

enter image description here

To connect to LocalDB from Microsoft SQL Server Management Studio or VS2019 and higher versions use the server name as (LocalDB)\MSSQLLocalDB with Windows Authentication

Dont waste time with the stress just use (localdb)\mssqllocaldb as your connection string and also as your as your server name in Microsoft SQL Server, It worked for me and it could work for you if you go through the right process