小编Wat*_*nce的帖子

如何选择数据到List <T>而不是DataTable?

这就是我目前从数据库中选择数据的方式:

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;
}
Run Code Online (Sandbox Code Playgroud)

但它返回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 …
Run Code Online (Sandbox Code Playgroud)

c# c#-4.0

6
推荐指数
2
解决办法
8699
查看次数

标签 统计

c# ×1

c#-4.0 ×1