【发布时间】:2012-05-09 04:35:02
【问题描述】:
这是我目前从数据库中选择数据的方式:
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];
}
我该怎么做?
谢谢!
【问题讨论】:
-
您确定要使用低级 ADO.NET 调用而不是 LINQ to SQL 或实体框架吗?
-
我不确定。但我认为低级 ADO.NET 会更快。
-
是的,ADO.NET 会更快,但是这种速度提升对您的环境有影响吗?
-
更快的代码总是更好的。
-
不,真的不是。可维护和可读的代码通常更好 - 当您发现瓶颈真正在哪里时,您可以以简单为代价挤出微小的优化。如果您追求可能的绝对最快代码,为什么不使用手动调整的程序集编写代码?