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

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

问题描述

我正在尝试执行实体框架的 Code First 演练 ( http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx ).

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

我有最新的 SQL Server Express,当我通过命令行(sqllocaldb info)检查可用版本时:我看到 localdbApp1 和 v11.0.当我尝试通过一些小的调整来运行演练时,我收到了无法连接的错误.

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.

我的 app.config 如下所示:

My app.config looks like this:

<parameter value="Server=(LocalDB)v11.0; Integrated Security=True; MultipleActiveResultSets=True" />

我编写了一个简单的连接测试,如下所示,代码返回相同的 SQL 连接错误((提供程序:命名管道提供程序,错误:40 - 无法打开到 SQL Server 的连接)).

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();

我尝试将 "Data Source=..." 替换为 "Server=..." 但无济于事.

I've tried replacing "Data Source=..." with "Server=..." but to no avail there.

知道连接字符串应该是什么吗?

Any ideas what the connection string should be?

推荐答案

  1. 要求 .NET framework 4 至少更新到 4.0.2.如果你有 4.0.2,那么你应该有

HKEY_LOCAL_MACHINESOFTWAREMicrosoft.NETFrameworkv4.0.30319SKUs.NETFramework,Version=v4.0.2

HKEY_LOCAL_MACHINESOFTWAREMicrosoft.NETFrameworkv4.0.30319SKUs.NETFramework,Version=v4.0.2

如果您安装了最新的 VS 2012,那么您可能已经拥有 4.0.2.先验证一下.

If you have installed latest VS 2012 chances are that you already have 4.0.2. Just verify first.

  1. 接下来您需要有一个 LocalDb 的实例.默认情况下,您有一个实例,其名称是单个 v 字符,后跟格式为 xx.x 的 LocalDB 版本号.例如,v11.0 代表 SQL Server 2012.默认情况下,自动实例是公共的.您还可以拥有私有的命名实例.命名实例提供与其他实例的隔离,并且可以通过减少与其他数据库用户的资源争用来提高性能.您可以使用SqlLocalDb.exe 实用程序(从命令行运行).

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

接下来您的连接字符串应如下所示:

Next your connection string should look like:

"Server=(localdb)v11.0;Integrated Security=true;"或

"Server=(localdb)v11.0;Integrated Security=true;" or

"Data Source=(localdb) est;Integrated Security=true;"

"Data Source=(localdb) est;Integrated Security=true;"

来自您的代码.它们都是一样的.注意两个\ 是必需的,因为 v 表示特殊字符.另请注意,出现在 (localdb)\ 之后的是 LocalDb 实例的名称.v11.0 是默认的公共实例,test 是我手动创建的私有实例.

from your code. They both are the same. Notice the two \ required because v and 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. 如果您已经有一个数据库(.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) 使用默认设置将其保存在默认位置:

    var query = "CREATE DATABASE myDbName;";
    

    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%)" +
        ";";
    

    然后执行!

    可以使用以下内容将示例表加载到数据库中:

    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');";
    

    请注意,SqlLocalDb.exe 实用程序无法让您访问数据库,您需要单独使用 sqlcmd 实用程序,令人伤心..

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

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

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