IT TIP

버전 11의 localdb에 대한 연결 문자열은 무엇입니까?

itqueen 2020. 11. 26. 20:35
반응형

버전 11의 localdb에 대한 연결 문자열은 무엇입니까?


엔터티 프레임 워크 ( http://blogs.msdn.com/b/adonet/archive/2011/09/28/ef-4-2-code-first-walkthrough.aspx) 의 Code First Walkthrough를 수행하려고합니다. ).

최신 SQL Server Express가 있고 명령 줄을 통해 사용 가능한 버전을 확인할 때 ( sqllocaldb info ) : localdbApp1 및 v11.0이 표시됩니다. 몇 가지 사소한 조정으로 연습을 실행하려고하면 연결할 수 없음 오류가 발생합니다.

내 app.config는 다음과 같습니다.

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

아래와 같은 간단한 연결 테스트를 작성했고 코드는 동일한 SQL 연결 오류를 반환합니다 ((공급자 : 명명 된 파이프 공급자, 오류 : 40-SQL Server에 대한 연결을 열 수 없음)).

new System.Data.SqlClient.SqlConnection("Data Source=(LocalDB)\v11.0; Integrated Security=True; MultipleActiveResultSets=True").Open();

나는 교체 "Data Source=..."시도했지만 "Server=..."거기에서 아무 소용이 없습니다.

연결 문자열이 무엇인지 아이디어가 있습니까?


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

최신 VS 2012를 설치했다면 이미 4.0.2가있을 가능성이 있습니다. 먼저 확인하십시오.

2) 다음으로 인스턴스가 필요합니다 LocalDb. 기본적으로 이름이 단일 v문자 이고 뒤에 xx.x 형식의 LocalDB 릴리스 버전 번호가 오는 인스턴스가 있습니다 . 예를 들어 v11.0는 SQL Server 2012를 나타냅니다. 자동 인스턴스는 기본적으로 공용입니다. 개인용으로 명명 된 인스턴스를 가질 수도 있습니다 . 명명 된 인스턴스는 다른 인스턴스와의 격리를 제공하고 다른 데이터베이스 사용자와의 리소스 경합을 줄여 성능을 향상시킬 수 있습니다. 유틸리티 를 사용하여 인스턴스의 상태를 확인할 수 있습니다 SqlLocalDb.exe(명령 줄에서 실행).

3) 다음으로 연결 문자열은 다음과 같아야합니다.

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

또는

"Data Source=(localdb)\\test;Integrated Security=true;"

귀하의 코드에서 . 둘 다 동일합니다. 두 가지주의 \\가 필요하기 때문에 \v\t수단 특수 문자를. 또한 뒤에 나타나는 (localdb)\\것은 LocalDb 인스턴스의 이름입니다. v11.0기본 공개 인스턴스이며 test수동으로 만든 비공개 인스턴스 입니다.

  1. 데이터베이스 (.mdf 파일)가 이미있는 경우 :

    "Server=(localdb)\\Test;Integrated Security=true;AttachDbFileName= myDbFile;"
    
  2. Sql Server 데이터베이스가없는 경우 :

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

프로그래밍 방식으로 고유 한 데이터베이스를 만들 수 있습니다.

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

그리고 실행하십시오!

샘플 테이블은 다음과 같이 데이터베이스에로드 할 수 있습니다.

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


I installed the mentioned .Net 4.0.2 update but I got the same error message saying:

A network-related or instance-specific error occurred while establishing a connection to SQL Server

I checked the SqlLocalDb via console as follows:

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;?

Then I realized that my application was compiled for DotNet framework 3.5 but SqlLocalDb only works for DotNet 4.0.

After correcting this, the problem was solved.


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.

To see which instances were installed by default, type sqllocaldb info inside a command prompt. On my machine, I get two instances, the first one named 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:

// if not using a verbatim string literal, don't forget to escape backslashes
@"Server=(localdb)\MSSQLLocalDB;Integrated Security=true;"

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"

In Sql Server 2008 R2 database files you can connect with

Server=np:\\.\pipe\YourInstance\tsql\query;InitialCatalog=yourDataBase;Trusted_Connection=True;

only, but in sql Server 2012 you can use this:

Server=(localdb)\v11.0;Integrated Security=true;Database=DB1;

and it depended on your .mdf .ldf version.

for finding programmicaly i use this Method that explained in this post


You need to install Dot Net 4.0.2 or above as mentioned here.
The 4.0 bits don't understand the syntax required by LocalDB

See this question here

You can dowload the update here


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

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

However, when I explicitly downloaded that 4.0.2 patch and installed it I got it working....


I have connection string Server=(localdb)\v11.0;Integrated Security=true;Database=DB1;

and even a .NET 3.5 program connects and execute SQL successfully.

But many people say .NET 4.0.2 or 4.5 is required.

참고URL : https://stackoverflow.com/questions/10540438/what-is-the-connection-string-for-localdb-for-version-11

반응형