简体   繁体   中英

Connection problems using Windows Azure hosting

When I connect to SQL Azure from my ASP.NET MVC Application, I get the following error..

Exception Details: System.Data.SqlClient.SqlException: 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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)

But, I am able to connect to SQL Azure from SQL Server Management Studio successfully. I also tried connecting using sqlCMD and that connects without any issue. Only issue arises is during runtime when I get this exception.

There must be something wrong with your connection string. You can't connect to Windows Azure SQL Database (aka SQL Azure) via Named Pipes. The only supported protocol is TCP. So your conn string would look something like:

Server=tcp:[servername].database.windows.net,1433;Database=[db_name];User ID=[username]@[servername];Password=[password];Trusted_Connection=False;Encrypt=True;Connection Timeout=30

You can get connection string from the management portal. When you go to the database, you have the Connection string link at the right (new portal):

SQL Azure显示连接字符串(新门户)

And the old portal almost the same:

SQL Azure连接字符串旧门户

And finally, you must have enabled "Allow other Windows Azure Services to connect to this server" in the Firewall rules.

**** UPDATE according to comments below ****

And where is this connection string entry? IN web.config, or in web.release.config / web.debug.config. Note that web.config transformations will not work for while developing, only when packaging! Does this error occurs when you debug locally, or when you deploy?

If locally, can you check which connection string are you actually using?

Note that there is default "ApplicationServices" connection string (which is in apphost.config), which might take place when you try to "login/register" in the default MVC app! That connection string assumes local SQL Express instance is present and running. And this is default connection string for Memebership and Role providers. You will not be able to logn/register and whatever action that requires authentication, unless you change the Membership provider settings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM