C# 版本 11 的 localdb 的连接字符串是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10540438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the connection string for localdb for version 11
提问by Bill Nielsen
I'm trying to do the Code First Walkthrough of the entity framework ( http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx).
我正在尝试执行实体框架的代码优先演练(http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx)。
I have the latest SQL Server Express and when I check my versions available via command line (sqllocaldb info): I see localdbApp1 and v11.0. When I try to run the walkthrough with a few minor tweaks, I get a can't connect error.
我有最新的 SQL Server Express,当我通过命令行(sqllocaldb info)检查可用版本时:我看到 localdbApp1 和 v11.0。当我尝试通过一些小的调整来运行演练时,出现无法连接错误。
My app.config looks like this:
我的 app.config 看起来像这样:
<parameter value="Server=(LocalDB)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />
I wrote a simple connection test like below and the code returns the same SQL Connection error ((provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)).
我写了一个简单的连接测试,如下所示,代码返回相同的 SQL 连接错误((提供者:命名管道提供者,错误:40 - 无法打开到 SQL Server 的连接))。
new System.Data.SqlClient.SqlConnection("Data Source=(LocalDB)\v11.0; Integrated Security=True; MultipleActiveResultSets=True").Open();
I've tried replacing "Data Source=..."with "Server=..."but to no avail there.
我试过用替换"Data Source=...","Server=..."但无济于事。
Any ideas what the connection string should be?
任何想法连接字符串应该是什么?
回答by Steve
回答by Arif Eqbal
This is for others who would have struggled like me to get this working....I wasted more than half a day on a seemingly trivial thing...
这是给那些像我一样努力让它工作的人......我在一件看似微不足道的事情上浪费了半天多......
If you want to use SQL Express 2012 LocalDB from VS2010 you must have this patch installed http://www.microsoft.com/en-us/download/details.aspx?id=27756
如果你想从 VS2010 使用 SQL Express 2012 LocalDB,你必须安装这个补丁 http://www.microsoft.com/en-us/download/details.aspx?id=27756
Just like mentioned in the comments above I too had Microsoft .NET Framework Version 4.0.30319 SP1Rel and since its mentioned everywhere that you need "Framework 4.0.2 or Above" I thought I am good to go...
就像在上面的评论中提到的,我也有 Microsoft .NET Framework Version 4.0.30319 SP1Rel 并且因为它到处都提到你需要“Framework 4.0.2 或更高版本”,我想我很高兴......
However, when I explicitly downloaded that 4.0.2 patch and installed it I got it working....
但是,当我明确下载该 4.0.2 补丁并安装它时,我开始工作了....
回答by Eduardo Fernandes
I installed the mentioned .Net 4.0.2 update but I got the same error message saying:
我安装了提到的 .Net 4.0.2 更新,但收到相同的错误消息:
A network-related or instance-specific error occurred while establishing a connection to SQL Server
与 SQL Server 建立连接时发生与网络相关或特定于实例的错误
I checked the SqlLocalDb via console as follows:
我通过控制台检查了 SqlLocalDb,如下所示:
C:\>sqllocaldb create "Test"
LocalDB instance "Test" created with version 11.0.
C:\>sqllocaldb start "Test"
LocalDB instance "Test" started.
C:\>sqllocaldb info "Test"
Name: Test
Version: 11.0.2100.60
Shared name:
Owner: PC\TESTUSER
Auto-create: No
State: Running
Last start time: 05.09.2012 21:14:14
Instance pipe name: np:\.\pipe\LOCALDB#B8A5271F\tsql\query
This means that SqlLocalDb is installed and running correctly. So what was the reason that I could not connect to SqlLocalDB via .Net code with this connectionstring: Server=(LocalDB)\v11.0;Integrated Security=true;?
这意味着 SqlLocalDb 已安装并正确运行。那么,我无法使用以下连接字符串通过 .Net 代码连接到 SqlLocalDB 的原因是什么:Server=(LocalDB)\v11.0;Integrated Security=true;?
Then I realized that my application was compiled for DotNet framework 3.5 but SqlLocalDb only works for DotNet 4.0.
然后我意识到我的应用程序是为 DotNet 框架 3.5 编译的,但 SqlLocalDb 只适用于 DotNet 4.0。
After correcting this, the problem was solved.
改正后,问题就解决了。
回答by nawfal
1) Requires .NET framework 4 updated to at least 4.0.2. If you have 4.0.2, then you should have
1) 需要 .NET framework 4 至少更新到 4.0.2。如果你有 4.0.2,那么你应该有
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319\SKUs\.NETFramework,Version=v4.0.2
If you have installed latest VS 2012 chances are that you already have 4.0.2. Just verify first.
如果您安装了最新的 VS 2012,您可能已经拥有 4.0.2。先验证一下。
2) Next you need to have an instance of LocalDb. By default you have an instance whose name is a single vcharacter followed by the LocalDB release version number in the format xx.x. For example, v11.0represents SQL Server 2012. Automatic instances are public by default. You can also have named instances which are private. Named instances provide isolation from other instances and can improve performance by reducing resource contention with other database users. You can check the status of instances using the SqlLocalDb.exeutility (run it from command line).
2)接下来你需要有一个LocalDb. 默认情况下,您有一个实例,其名称为单个v字符,后跟 xx.x 格式的 LocalDB 版本号。例如,v11.0代表 SQL Server 2012。默认情况下,自动实例是公共的。您还可以拥有私有的命名实例。命名实例提供与其他实例的隔离,并且可以通过减少与其他数据库用户的资源争用来提高性能。您可以使用该SqlLocalDb.exe实用程序检查实例的状态(从命令行运行它)。
3) Next your connection string should look like:
3)接下来你的连接字符串应该是这样的:
"Server=(localdb)\v11.0;Integrated Security=true;"
or
或者
"Data Source=(localdb)\test;Integrated Security=true;"
from your code. They both are the same.Notice the two \\required because \vand \tmeans special characters. Also note that what appears after (localdb)\\is the name of your LocalDb instance. v11.0is the default public instance, testis something I have created manually which is private.
从你的代码。他们都是一样的。注意这两个是\\必需的,因为\v和\t表示特殊字符。另请注意,后面出现的(localdb)\\是您的 LocalDb 实例的名称。v11.0是默认的公共实例,test是我手动创建的私有实例。
If you have a database (.mdf file) already:
"Server=(localdb)\Test;Integrated Security=true;AttachDbFileName= myDbFile;"If you don't have a Sql Server database:
"Server=(localdb)\v11.0;Integrated Security=true;"
如果您已经有一个数据库(.mdf 文件):
"Server=(localdb)\Test;Integrated Security=true;AttachDbFileName= myDbFile;"如果您没有 Sql Server 数据库:
"Server=(localdb)\v11.0;Integrated Security=true;"
And you can create your own database programmatically:
您可以以编程方式创建自己的数据库:
a) to save it in the default location with default setting:
a)使用默认设置将其保存在默认位置:
var query = "CREATE DATABASE myDbName;";
b) To save it in a specific location with your own custom settings:
b)使用您自己的自定义设置将其保存在特定位置:
// your db name
string dbName = "myDbName";
// path to your db files:
// ensure that the directory exists and you have read write permission.
string[] files = { Path.Combine(Application.StartupPath, dbName + ".mdf"),
Path.Combine(Application.StartupPath, dbName + ".ldf") };
// db creation query:
// note that the data file and log file have different logical names
var query = "CREATE DATABASE " + dbName +
" ON PRIMARY" +
" (NAME = " + dbName + "_data," +
" FILENAME = '" + files[0] + "'," +
" SIZE = 3MB," +
" MAXSIZE = 10MB," +
" FILEGROWTH = 10%)" +
" LOG ON" +
" (NAME = " + dbName + "_log," +
" FILENAME = '" + files[1] + "'," +
" SIZE = 1MB," +
" MAXSIZE = 5MB," +
" FILEGROWTH = 10%)" +
";";
And execute!
并执行!
A sample table can be loaded into the database with something like:
可以使用以下内容将示例表加载到数据库中:
@"CREATE TABLE supportContacts
(
id int identity primary key,
type varchar(20),
details varchar(30)
);
INSERT INTO supportContacts
(type, details)
VALUES
('Email', '[email protected]'),
('Twitter', '@sqlfiddle');";
Note that SqlLocalDb.exeutility doesnt give you access to databases, you separately need sqlcmdutility which is sad..
请注意,SqlLocalDb.exe实用程序不会让您访问数据库,您单独需要sqlcmd实用程序,这很遗憾..
EDIT: moved position of semicolon otherwise error would occur if code was copy/pasted
编辑:移动分号的位置,否则如果复制/粘贴代码会发生错误
回答by linquize
I have connection string Server=(localdb)\v11.0;Integrated Security=true;Database=DB1;
我有连接字符串 Server=(localdb)\v11.0;Integrated Security=true;Database=DB1;
and even a .NET 3.5program connects and execute SQL successfully.
甚至.NET 3.5程序也能成功连接并执行 SQL。
But many people say .NET 4.0.2 or 4.5 is required.
但是很多人说需要 .NET 4.0.2 或 4.5。
回答by yeye
In Sql Server 2008 R2 database files you can connect with
在 Sql Server 2008 R2 数据库文件中,您可以连接
Server=np:\.\pipe\YourInstance\tsql\query;InitialCatalog=yourDataBase;Trusted_Connection=True;
only, but in sql Server 2012 you can use this:
仅,但在 sql Server 2012 中你可以使用这个:
Server=(localdb)\v11.0;Integrated Security=true;Database=DB1;
Server=(localdb)\v11.0;Integrated Security=true;Database=DB1;
and it depended on your .mdf.ldfversion.
这取决于您的.mdf.ldf版本。
for finding programmicaly i use this Method that explained in this post
为了以编程方式查找,我使用了这篇文章中解释的方法
回答by SpiritBH
I had the same problem for a bit. I noticed that I had:
我遇到了同样的问题。我注意到我有:
Data Source= (localdb)\v11.0"
Simply by adding one back-slash it solved the problem for me:
只需添加一个反斜杠,它就为我解决了这个问题:
Data Source= (localdb)\v11.0"
回答by Groo
This is a fairly old thread, but since I was reinstalling my Visual Studio 2015 Community today, I thought I might add some info on what to use on VS2015, or what might work in general.
这是一个相当古老的线程,但由于我今天重新安装了 Visual Studio 2015 社区,我想我可能会添加一些关于在 VS2015 上使用什么的信息,或者什么可以正常工作。
To see which instances were installed by default, type sqllocaldb infoinside a command prompt. On my machine, I get two instances, the first one named MSSQLLocalDB.
要查看默认安装了哪些实例,请sqllocaldb info在命令提示符中键入。在我的机器上,我得到两个实例,第一个名为MSSQLLocalDB.
C:\>sqllocaldb info
MSSQLLocalDB
ProjectsV13
You can also create a new instance if you wish, using sqllocaldb create "some_instance_name", but the default one will work just fine:
如果你愿意,你也可以创建一个新的实例,使用sqllocaldb create "some_instance_name",但默认的就可以了:
// if not using a verbatim string literal, don't forget to escape backslashes
@"Server=(localdb)\MSSQLLocalDB;Integrated Security=true;"

