I have created an ASP.NET project in Visual Studio. The database is a SQL Server Express database TestDB.mdf
. I uploaded this with .ldf
file to the standard App_Data
directory but the connection string doesn't seem to work. How can I use this SQL Server Express database in a shared hosting environment?
My Connection String is:
Data Source=.\\SQLEXPRESS;
Initial Catalog=C:\\inetpub\\dir1\\dir2\\docs\\App_Data\\Test.mdf;
Integrated Security=SSPI;
Connect Timeout=120";
It gives always the same 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)
2 Answers 2
Most hosting companies are going to want you to connect to a database server. Having the database on the web server is more for development ease, not for production.
-
3And just to add, please don't use the deprecated
User instance
andAttachDbFileName
syntax when you move the database to the web server. <shudder>Aaron Bertrand– Aaron Bertrand2013年02月10日 20:36:00 +00:00Commented Feb 10, 2013 at 20:36 -
@MarkStorey-Smith but still it will require a proper instance of SQL Server being present, so why not do it the right way and actually attach the database instead of this hokey reference-a-file stuff? Especially since most users who do that are surprised when two users of the app have two copies of the database open...Aaron Bertrand– Aaron Bertrand2013年02月11日 01:32:57 +00:00Commented Feb 11, 2013 at 1:32
-
1@Mark but when the app is in production, a new instance of the database is highly unlikely to be what anybody wants. That feature is for development.Aaron Bertrand– Aaron Bertrand2013年02月11日 11:58:26 +00:00Commented Feb 11, 2013 at 11:58
-
1@AaronBertrand Acknowledge that assuming someone can differentiate between when its appropriate and not is risky business. Will remove the suggestion. Hadn't logged that it had been deprecated either... presumably because too many people were doing exactly this :)Mark Storey-Smith– Mark Storey-Smith2013年02月11日 14:26:53 +00:00Commented Feb 11, 2013 at 14:26
The reason this works in Development is because Visual Studio has the ability to host the database contained within the solution - essentially acting as a single connection only version of SQL Express. When you deploy your Application to a Web Server there is nothing on there (by default) that has the ability to do this - as such ASP.NET/IIS does not know what to do with the file - hence why you cannot establish a connection. To connect to the database you must host it on an instance of SQL Server, whether that be SQL Express or a fully fledged version. Whether you should put your database on the same server as IIS is another issue altogether.