62,046
社区成员
发帖
与我相关
我的任务
分享
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
using System.Collections.Generic;
namespace CoreApi.Controllers
{
[Route("api/[controller]")]
public class StudentController : Controller
{
// GET api/values
[HttpGet]
public List<Student> Get()
{
var students = new List<Student>();
using(MySqlConnection connection = new MySqlConnection("Server=local;Database=Student;Uid=root;Pwd=movoto1234;SslMode=None;")){
connection.Open();
MySqlCommand cmd = new MySqlCommand("select * from Student", connection);
using(MySqlDataReader reader = cmd.ExecuteReader()){
while(reader.Read()){
var s = new Student();
s.ID = reader.GetInt32("ID");
s.Name = reader.GetString("Name");
s.Sex = reader.GetString("Sex");
s.Age = reader.GetInt32("Age");
students.Add(s);
}
}
}
return students;
}
}
public class Student{
public int ID { get; set; }
public string Name { get; set; }
public string Sex { get; set; }
public int Age { get; set; }
}
}