Samantha J T Star
Samantha J T Star

Reputation: 32808

What ways are available for me to query my SQL Server relational database from Web API?

I have been looking at the Microsoft examples and most use Entity Framework. However it seems overkill for when I need to make a quick connection. Gets some data from a report and the return it to my web page. It also seems like it is difficult to compose such things as complex SQL that might require a multi-table join and some input parameters.

So what other options are available?

Upvotes: 0

Views: 246

Answers (2)

Kenneth
Kenneth

Reputation: 28737

If you want a quick way to connect to your datasrouce you can look into some micro ORM-frameworks:

https://code.google.com/p/dapper-dot-net/

https://github.com/robconery/massive

http://www.toptensoftware.com/petapoco/

These are all tiny libraries that allow you to write your own SQL but still help you (a bit) with mapping the results to classes.

Upvotes: 2

Yasser Shaikh
Yasser Shaikh

Reputation: 47784

Here is what we do for our reports app that fetches data using Web Api and SQL.

1. Stored Procedures with EF

Since you mention you need to fetch data that might require a multi-table join, best approach here would be to use Stored Procedures to get that data. Import that Stored Procedure to the EntityFramework.

2. Service Layer Class & AutoMapper

Now create a similar object that will map to the Data Layer's object that will be returned from the Stored Procedure you just added. To map these two classes we use a mapper called as AutoMapper. We create two classes because we dont want our web app to directly access the Data Layer classes.

3. Expose your Web API

Now this data can be sent from the api to whoever wants to use it.

Hope this helps.

Upvotes: 0

Related Questions