Error connecting to SQL Server 2008 R2 Express

2019-02-27 18:44发布

问题:

I have installed SQL Server 2008 R2 Express on Windows 7.

When I run a web project in VS2010 I receive the following error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

Strange, I am working on the local machine (VS2010, IIS, SQL Server, etc. no network needed).

I googled around and followed many tips. Nothing helps.

I use the correct instance name (e.g. like Data Source=PCName\SQLServer;Initial Catalog=SampleDBName;Integrated Security=True)

Gave the Network Service the right credentials, etc. etc. etc.

I am lost now and having this problem for a week now. Pulling my hair out!

Update:

I got a tip to test my connection string with SQL Server Management Studio Express. In the Connect popup screen you usually use the Login tab (first and default tab). You can also use the third tab 'Additional Connection Parameters'. I tested my connect string and it just works fine!

So, I think it's something with rights. But I have no idea what or which ...

O yeah. Some extra info. I changed Application pools default options for IIS 7.5 from 'ApplicationPoolIdentity' to 'NetworkService'. I added the Network Service as login to SQL, gave grant rights, etc. I turned SQL Browser service on, the service is running (but it is not really necessary because I run local).

Update 2:

It's seems like I found what causes the error, but I have no clue why.

For my website I created a website with some virtual folders and applications. I need an url in my logic. Therefor I use a host header (like http://server.name.tld)

In VS2010 I configured Property pages -> start options -> use custom web server with the url (same as the earlier mentioned host header).

If I do that I get the error.

If I use use Property pages -> start options -> default web server, it seems that I get a connection to the db.

But I need the use of the host header.

What do I need to do, to get the 'custom web server' working?

Update 3:

Tried today also with a test virtual machine with windows server 2008. Still getting the error.

Also made a test web application, without an IIS site an without a host-header. That one works just fine (using the same connectstring). Also on the VM.

I am lost. I really need someone who can exchange some knowledge to me in this field ...

回答1:

Did you try to use SQLExpress as instance name in the connection string?

PCName\SQLExpress

Moreover check if your server is configured to accept connections via TCP/IP (check Sql Server Configuration Manager). Can you connect to your database using SQL Server Management Studio? Test your connection from Visual Studio, does it work?



回答2:

Named instances that aren't using a TCP binding are usually referenced as .\[instance name], such as .\SQLExpress or perhaps .\SQLServer in your example.



回答3:

red cheeks

Found the problem. Web.config configured with a session db, but no session db created and wasn't planning any longer to do so. Forget to turn it off ...