这就是我目前从数据库中选择数据的方式:
public DataTable GetData()
{
DataTable table = new DataTable("Table");
using (SqlConnection connection = new SqlConnection("Connection string"))
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "query string";
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(table);
}
return table;
}
但它返回 DataTable,我想选择 List 而不是 DataTable。像这样:
public List<MyClass> GetData()
{
DataTable table = new DataTable("Table");
using (SqlConnection connection = new SqlConnection("Connection string"))
{
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "query string";
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(table);
}
...
return [List of MyClass];
}
我怎样才能做到这一点?
谢谢!