什么是对的LocalDB连接字符串版本11 [英] What is the connection string for localdb for version 11

查看:269
本文介绍了什么是对的LocalDB连接字符串版本11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做code中的实体框架的第一次演练(<一个href=\"http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-$c$c-first-walkthrough.aspx\">http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-$c$c-first-walkthrough.aspx )。

我有最新的SQL Server防爆preSS,当我检查可用我的版本通过命令行( sqllocaldb信息的):我看到localdbApp1和V11.0。当我尝试运行了一些小的调整的演练中,我得到一个无法连接的错误。

我的app.config看起来是这样的:

 &LT;参数值=服务器=(的LocalDB)\\ V11.0;集成安全性= TRUE; MultipleActiveResultSets = TRUE/&GT;

我写了一个简单的连接测试,如下面和code返回相同的SQL连接错误((provider:命名管道提供程序,error:40 - 无法打开到SQL Server)的连接)。

 新System.Data.SqlClient.SqlConnection(数据源=(的LocalDB)\\ V11.0;集成安全性= TRUE; MultipleActiveResultSets =真)打开()。

我试过更换数据源= ...服务器= ...但无济于事那里。

任何想法连接字符串应该是什么?


解决方案

1)需要至少更新到4.0.2的.NET framework 4。 如果你有4.0.2,那么你应该已经

<$p$p><$c$c>HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319\\SKUs\\.NETFramework,Version=v4.0.2

如果您已经安装了最新的VS 2012有机会,你已经有4.0.2。只需验证第一。

2)接下来,你需要有的LocalDB 的一个实例。默认情况下你有一个实例,其名称为一个单一的 v 字符随后在格式xx.x.中的LocalDB发行版本号例如, 11.0 重新presents SQL Server 2012中的自动实例默认都是公有的。您也可以命名这是私人实例。命名实例从其他实例提供隔离,并且可以通过减少资源争与其它数据库用户提高性能。 您可以检查使用实例的状态的 SqlLocalDb.exe 实用程序(从命令行运行它)。

3)接下来在连接字符串应该是这样的:

 服务器=(的LocalDB)\\\\ 11.0;集成安全性= TRUE;

 数据源=(的LocalDB)\\\\测试;集成安全=真;

从code 他们都是一样的。注意两个 \\\\ 必需的,因为符\\ v \\ t 表示特殊字符。另外请注意,在显示的内容(的LocalDB)\\\\ 是你的LocalDB实例的名称。 11.0 是默认的公共实例,测试是我手动创建这是私人的。


  1. 如果您有一个数据库(.mdf文件)已:

     服务器=(的LocalDB)\\\\测试;集成安全性= TRUE; AttachDbFileName = myDbFile;


  2. 如果你没有一个SQL Server数据库:

     服务器=(的LocalDB)\\\\ 11.0;集成安全性= TRUE;


和您可以通过编程创建自己的数据库:

A)将它保存在默认设置的默认位置:

  VAR的查询=CREATE DATABASE myDbName;;

B)要与自己的自定义设置保存在一个特定的位置:

  //你的数据库名称
字符串DBNAME =myDbName;//路径,你的数据库文件:
//确保目录存在并且您已经阅读写权限。
字符串[] =文件{Path.Combine(Application.StartupPath,DBNAME +密度纤维板),
                   Path.Combine(Application.StartupPath,DBNAME +.LDF)};//创建数据库查询:
//注意,数据文件和日志文件有不​​同的逻辑名称
VAR的查询=CREATE DATABASE+ DBNAME +
    ON PRIMARY+
    (NAME =+ DBNAME +_data,+
    「FILENAME ='+文件[0] +',+
    SIZE = 3MB,+
    MAXSIZE = 10MB,+
    FILEGROWTH = 10%)+    登录+
    (NAME =+ DBNAME +_log,+
    「FILENAME ='+文件[1] +',+
    SIZE = 1MB,+
    MAXSIZE = 5MB,+
    FILEGROWTH = 10%)+
    ;;

和执行!

一个试样台可以加载到数据库中与类似:

  @CREATE TABLE supportContacts
    (
        ID INT标识的主键,
        类型为varchar(20)
        细节VARCHAR(30)
    );
   INSERT INTO supportContacts
   (类型,详情)
   VALUES
   (电子邮件,admin@sqlfiddle.com'),
   ('微','@sqlfiddle');;

注意 SqlLocalDb.exe 实用犯规让你访问数据库,你需要单独的 SQLCMD 工具,它是可悲的。

编辑:移动分号的位置,否则错误将会发生,如果code是复制/粘贴

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 ).

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.

My app.config looks like this:

<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)).

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.

Any ideas what the connection string should be?

解决方案

1) Requires .NET framework 4 updated to at least 4.0.2. If you have 4.0.2, then you should have

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.

2) Next you need to have an instance of LocalDb. By default you have an instance whose name is a single v character followed by the LocalDB release version number in the format xx.x. For example, v11.0 represents 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.exe utility (run it from command line).

3) Next your connection string should look like:

"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 \v and \t means special characters. Also note that what appears after (localdb)\\ is the name of your LocalDb instance. v11.0 is the default public instance, test is something I have created manually which is private.

  1. If you have a database (.mdf file) already:

    "Server=(localdb)\\Test;Integrated Security=true;AttachDbFileName= myDbFile;"
    

  2. If you don't have a Sql Server database:

    "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:

var query = "CREATE DATABASE myDbName;";

b) To save it in a specific location with your own custom settings:

// 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', 'admin@sqlfiddle.com'),
   ('Twitter', '@sqlfiddle');";

Note that SqlLocalDb.exe utility doesnt give you access to databases, you separately need sqlcmd utility which is sad..

EDIT: moved position of semicolon otherwise error would occur if code was copy/pasted

这篇关于什么是对的LocalDB连接字符串版本11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆