简体   繁体   English

这是从数据库中检索数据的最快捷有效的方法吗?

[英]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: 如果是这样,尝试不同的方法,不要忽视:

  • the logic used by the stored procedure running your query, and the plan it generates 运行查询的存储过程使用的逻辑及其生成的计划
  • the amount of data that stored procedure is returning (do you need all of those columns?) 存储过程返回的数据量(您是否需要所有这些列?)
  • the bandwidth of the intervening network, if any 中间网络的带宽,如果有的话

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.

相关问题 从数据库中检索数据的最快方法 - Fastest way to retrieve data from database 从数据库中检索多个对象的有效方法 - Efficient way to retrieve multiple objects from database 使用BasicHttpBinding检索和更新数据的最快方法 - Fastest way to retrieve and update data using BasicHttpBinding 从SQL Server数据库中获取大量数据的最快方法 - Fastest way to fetch huge amount of data from SQL Server database SQLite - 从SQLite数据库读取数据的最快方法? - SQLite - Fastest way to read Data from SQLite Database? 从文本文件加载数据并将其存储到数据库的最快方法 - Fastest way to load data from text file then store it into database 从字典中检索值的最快方法 - Fastest way to retrieve values from dictionary 从EntityCollection检索单个值的最快方法 - Fastest way to retrieve single value from EntityCollection 从SQLite数据库文件检索一百万条记录并将其显示在WPF Datagrid中的最快方法是什么? - What is the fastest way to retrieve a million records from an SQLite database file and display it in the WPF Datagrid? 从 SqlDataReader 读取 JSON 字符串的最有效方式和最快方式 - Most efficient way and fastest way to read JSON string from SqlDataReader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM