[英]Is this the fastest and efficient way to retrieve data from the database?
Is this the fastest and efficient way to retrieve data from the database to the business logic layer? 这是从数据库检索数据到业务逻辑层的最快捷有效的方法吗?
public static DataTable Getdata(Guid companyId)
{
DbCommand command = db.GetStoredProcCommand("dbo.P_GetData");
db.AddInParameter(command, "@company_id", DbType.Guid, companyId);
IDataReader reader = db.ExecuteReader(command);
DataTable data = new DataTable();
data.Load(reader, LoadOption.OverwriteChanges);
reader.Close();
return data;
}
According to nawfal's benchmarks: 根据nawfal的基准:
For some reason Fill()
: 由于某种原因Fill()
:
dataAdapter.Fill(dataSet);
Is faster than Load()
: 比Load()
更快 :
dataTable.Load(dataReader);
For example, something like this might be 4-5x faster than what you have: 例如,像这样的东西可能比你拥有的东西快4-5倍:
using (var da = new MySqlDataAdapter())
{
using (da.SelectCommand = conn.CreateCommand())
{
da.SelectCommand.CommandText = query;
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
}
See his answer for more details and benchmark times. 有关详细信息和基准时间,请参阅他的答案。
It Depends on your Requirement.. 这取决于你的要求..
There are many ways to retrieve data from database. 有许多方法可以从数据库中检索数据。
In Ado.net datareader and data adapter can be use. 在Ado.net中,可以使用datareader和数据适配器。 And they both have its advantage. 他们都有自己的优势。
you can also use Linq to sql 你也可以使用Linq来sql
and check this Performance Comparison: Data Access Techniques 并检查此性能比较:数据访问技术
http://msdn.microsoft.com/en-us/library/ms978388.aspx http://msdn.microsoft.com/en-us/library/ms978388.aspx
Regards. 问候。
Assuming that your logic layer requires a DataTable
, and ignoring other coding issues and requirements, this is probably plenty fast. 假设您的逻辑层需要DataTable
,并忽略其他编码问题和要求,这可能很快。
Do you have reason to believe that it's too slow for your needs? 你有理由相信它对你的需求来说太慢吗? If so, experiment with different approaches, and don't neglect: 如果是这样,尝试不同的方法,不要忽视:
Edit: If I were doing something like this, if I was working with a reasonably-sized data model, and if I had influence over the business layer, I'd use a persistence framework (like the Entity Framework ) rather than straight ADO.NET. 编辑:如果我正在做这样的事情,如果我正在使用一个合理大小的数据模型,如果我对业务层有影响,我会使用持久性框架(如实体框架 )而不是直接ADO。净。 I'd do this not for performance reasons, though - a persistence layer is more maintainable in the long run. 我这样做不是出于性能原因 - 从长远来看,持久层更易于维护。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.