我遵循本教程(http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api)在我的ASP.NET Web应用程序中设置Web API。但是,它并没有讨论如何从数据库中检索记录。它不是硬编码哑数据到控制器,就像这样:从SQL DB获取数据用于ASP.NET Web窗体API
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
比方说,我有我的SQL数据库的表称为“产品”,我怎么会在那里,而不是获取产品数据?如果有人能指出我的方向正确,我将不胜感激。
我曾尝试使用函数,但它没有工作。
DataTable dbProducts = new DataTable();
dbProducts = Wrapper.getProducts();
首先你需要有一个数据库表并用它记录如果您对打想要从数据库中获取数据。其次,您需要添加数据访问层,以便查询数据库并获取所需的记录,以便Web API使用它们。 请按照本教程中的步骤进行操作,因为它显示了如何在Web API中使用实体框架:http://www.asp.net/web-api/overview/creating-web-apis/using-web-api-with -entity-framework/part-1 – 2014-10-10 16:09:56