A-Sharabiani
A-Sharabiani

Reputation: 19329

Connect to SQL database when the application and database are on the same Azure VM

I have an ASP.NET MVC 5 application published on a Virtual Machine on Azure. I also have a database on the same Virtual Machine.

Application works fine, however it cannot connect to the database.

I have these information from Azure:

This is my connection string:

<add name="RepositoryDbConnectionString"
 connectionString="Data Source=137.135.123.149,1433; Database=Codex-Repo-Cloud; User Id=myUser; Password=mypass; MultipleActiveResultSets=True;"
 providerName="System.Data.SqlClient" />

I also have tried these connection strings, none worked so far:

connectionString="Data Source=something.cloudapp.net; Database=Codex-Repo-Cloud; User Id=myUser; Password=mypass; MultipleActiveResultSets=True;"

connectionString="Data Source=10.0.0.4; Database=Codex-Repo-Cloud; User Id=myUser; Password=mypass; MultipleActiveResultSets=True;"

connectionString="Server=localhost; Database=Codex-Repo-Cloud; Integrated Security=false; User Id=myUser; Password=myPass;"

NOTE With the first connection string, I can remotely connect to the database in Visual Studio Server Explorer and I can actually modify the data.

Question When the SQL Database and the Web application are both on the same Azure Virtual Machine, how can I connect to the database within my application. Is there any settings, or is it just my connection string?

Upvotes: 2

Views: 386

Answers (3)

Luis Bosquez
Luis Bosquez

Reputation: 386

You could also try to see if SQL Server authentication is enabled on your database server. (It is disabled by default when you install it on-premise).

To do so, you can log in from SSMS > Right click on your database server > Select 'Properties' from the pop-up menu > Navigate to the 'Security' tab > Under 'Server Authentication' select 'SQL Server and Windows Authentication mode'.

After restarting the server, it should be able to log in with SQL Server credentials.

Thanks, Luis

Upvotes: 0

vishal vazkar
vishal vazkar

Reputation: 338

Check the below things are configured on your Database in azure:

  1. The user which you are using in the Web Config has enough rights to the database (change to sysadmin and see if it works and then change to specific user roles if this works).

  2. open SQL Server Configuration Manager and make sure that Sql Server Network Configuration > "Protocols For Database Name" > TCP/IP is enabled.

  3. right click the website folder and the app data folder which is used for the project > properties > security > users > add IIS apppool "website apppool name" and give this user read write access.

  4. Make sure that the step 3 is done on the Database.mdf file also.

Let me know if it works !

Cheers, Vishal

Upvotes: 0

Jakub
Jakub

Reputation: 11

I have the same issue with JDBC connections. With the same connection string I can connect from the outside world, but cannot from the within the Azure VM environment

This might be of some help: https://msdn.microsoft.com/en-us/library/azure/dn133152.aspx.

Upvotes: 1

Related Questions