SQL Network Interfaces, error: 26 - Error Locating

2019-03-01 17:31发布

I have added a database file (.mdf) to my application using Visual Studio built-in functionality. Database is in the App_Data folder. It is running fine. But when I publish it and upload it to server it gives this 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)

I visited similar situation but confused. I don't know which version and edition of SQL Server is running at server. I upload project using Filezilla. My connection string is as follows in my web.config:

<add name="ConnectionString" 
     connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True" 
     providerName="System.Data.SqlClient"/>

Please describe process step by step is possible.

Thanks in advance.

2条回答
我只想做你的唯一
2楼-- · 2019-03-01 18:19

The whole User Instance and AttachDbFileName= approach is flawed - at best! Also, it's only supported by the Express edition of SQL Server - so if your hoster has anything other than Expres, you're basically screwed.

The real solution in my opinion would be to

  1. install SQL Server Management Studio Express

  2. create your database in SSMS Express, give it a logical name (e.g. MyDatabase)

  3. connect to it using its logical database name (given when you create it on the server) - and don't mess around with physical database files and user instances. In that case, your connection string would be something like:

    Data Source=.\\SQLEXPRESS;Database=MyDatabase;Integrated Security=True
    

    and everything else is exactly the same as before...

Now, when you go and upload your web site to the hosting site, you need to get the necessary SQL scripts to create and populate your database on the hosting site - you can do this by using Tasks > Generate Scripts from the SSMS Express tool, or by some other means.

Then, on the hosting site, you need to execute those SQL scripts to create and populate your database (and later on: execute the change scripts to keep the database up to date).

Your connection string will also most likely change to something like:

Server=(ip-address-or-name);Database=MyDatabase;User ID=YourUserIdHere;Pwd=YourPassword

since the hosting provider doesn't use the SQLEXPRESS named instance (but most likely a default instance without any name). You need to ask your provider for the details - it could be that you have to provide some other instance name! That's totally up to the provider, we cannot know that - you'll have to ask and find out.

查看更多
We Are One
3楼-- · 2019-03-01 18:23

The fix for me was making sure I had the correct project set for startup in the solution. Right click project and "Set as Startup Project"

查看更多
登录 后发表回答