我正在研究Angular 2项目。我有一个关于Mysql数据库的表。我已连接到我的数据库。现在,我想从我的Mysql数据库表中获取数据。如何使用asp.net core 2.0 Web Api ???从数据库中获取数据
答案 0 :(得分:0)
我强烈建议使用SQL Server。快递版应该这样做。您还应该检查EntityFramework。
检查this one。
[Route("API/my-api-url")]
public class MyController : Controller
{
public async Task<IActionResult> List()
{
List<Film> list = new List<Film>();
using (MySqlConnection conn = new MySqlConnection(ConnectionString))
{
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT * FROM film", conn);
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
list.Add(new Film()
{
FilmId = reader.GetInt32("film_id"),
Title = reader.GetString("title"),
Description = reader.GetString("description"),
ReleaseYear = reader.GetInt32("release_year"),
Length = reader.GetInt32("length"),
Rating = reader.GetString("rating")
});
}
}
}
return new JsonResult(list);
}
}