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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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
- install SQL Server Management Studio Express
-
create your database in SSMS Express, give it a logical name (e.g.
MyDatabase) -
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.
Method 2
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”
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0