0

I've found enough MVC 4 API samples that use a hard-coded array as the data source, but am having trouble finding a simple example with and SQL query.

I have this model:

public class Person
{
    public int id {get;set;}
    public string Name {get;set;}
}

and I would like to construct a controller that returns a list of Person using this database query:

select id, name from names order by name

If it matters, the connection string to the database:

Server=sql12;Database=Mydb;Integrated Security=true;"

Thanks in advance!

2
  • write a method which executes the query and read the results and fill list of Person objects and return. Call that method in your action method. Commented May 29, 2013 at 17:11
  • Thought about doing that, a bit awkward, last resort, I guess. Commented May 29, 2013 at 17:18

1 Answer 1

2

Have a look at the Entity Framework examples for ASP.NET MVC. There are several tutorials that apply to what you're doing.

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc

Sign up to request clarification or add additional context in comments.

2 Comments

Is there not a simple ADO.NET solution? Or does WebAPI require the use of a new data access paradigm?
@Metaphor: No you can use any data access technology as per your wish.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.