Victor
Victor

Reputation: 1205

How can I consume an SQL Azure database from an ASP.NET API?

I'm creating an API meant to consume data on an exiting SQL Azure database. I'm creating this API based on ASP.NET's Web API 2.

I'm a complete newbie on this area. This is why I've been following ASP.NET tutorials. I'm able to create a Web API and create models/controllers. But on the database side, every tutorial I come across is about creating a database from the scratch dedicated for the API, not about connecting an existing database that is consumed by the API.

How can I make the connection and query the database from the API?

Tutorials I've followed

Upvotes: 3

Views: 5306

Answers (1)

Denys Wessels
Denys Wessels

Reputation: 17049

  1. Login to the Windows Azure management portal
  2. Click on "SQL DATABASES" in the menu
  3. Click on the database you want to connect to
  4. Right at the bottom click on "View SQL Database connection strings for ADO .Net, ODBC, PHP, and JDBC"
  5. Copy the connection from the "ADO.NET" text box and change the password to the valid password.
  6. Now in your WEB API project open the .config file and paste the connection string in the designated element:

Connection string:

  <connectionStrings>
    <add name="MyAzzureConnection" connectionString="paste the connection string from Azure here" />
  </connectionStrings>

Now you can write your data access code using the technology of your choice, just make sure you use the Azure connection from the Web.config file.

Upvotes: 4

Related Questions