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

时间:2021-12-14 04:07:20

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;
}

3 个解决方案

#1


0  

According to nawfal's benchmarks:

根据nawfal基准:

For some reason Fill():

因为某些原因填满():

dataAdapter.Fill(dataSet);

dataAdapter.Fill(数据集);

Is faster than Load():

速度比负载():

dataTable.Load(dataReader);

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.

查看他的答案以了解更多细节和基准时间。

#2


1  

It Depends on your Requirement..

这要看你们的要求而定。

There are many ways to retrieve data from database.

有许多方法可以从数据库中检索数据。

In Ado.net datareader and data adapter can be use. And they both have its advantage.

在Ado.net中,可以使用datareader和数据适配器。他们都有自己的优势。

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.

的问候。

#3


0  

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. I'd do this not for performance reasons, though - a persistence layer is more maintainable in the long run.

编辑:如果我正在做这样的事情,如果我使用一个合理大小的数据模型,如果我对业务层有影响,我将使用持久性框架(比如实体框架)而不是直接的ADO.NET。我这样做不是出于性能的原因,尽管从长远来看,持久层更容易维护。

#1


0  

According to nawfal's benchmarks:

根据nawfal基准:

For some reason Fill():

因为某些原因填满():

dataAdapter.Fill(dataSet);

dataAdapter.Fill(数据集);

Is faster than Load():

速度比负载():

dataTable.Load(dataReader);

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.

查看他的答案以了解更多细节和基准时间。

#2


1  

It Depends on your Requirement..

这要看你们的要求而定。

There are many ways to retrieve data from database.

有许多方法可以从数据库中检索数据。

In Ado.net datareader and data adapter can be use. And they both have its advantage.

在Ado.net中,可以使用datareader和数据适配器。他们都有自己的优势。

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.

的问候。

#3


0  

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. I'd do this not for performance reasons, though - a persistence layer is more maintainable in the long run.

编辑:如果我正在做这样的事情,如果我使用一个合理大小的数据模型,如果我对业务层有影响,我将使用持久性框架(比如实体框架)而不是直接的ADO.NET。我这样做不是出于性能的原因,尽管从长远来看,持久层更容易维护。