SqlDataReader的VS SqlDataAdapter的:其中一个拥有返回一个DataTable更好的性能? [英] SqlDataReader vs SqlDataAdapter: which one has the better performance for returning a DataTable?

查看:317
本文介绍了SqlDataReader的VS SqlDataAdapter的:其中一个拥有返回一个DataTable更好的性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道哪一个有返回数据表更好的性能。这里 SqlDataReader的我用 DataTable.Load(DR)



使用 SqlDataReader的

 公共静态数据表populateUsingDataReader(字符串更改为MyQuery) 
{
DataTable的DT =新的DataTable();
使用(SqlConnection的CON =新的SqlConnection(constring))
{
的SqlCommand CMD =新的SqlCommand(更改为MyQuery,CON);
con.Open();
SqlDataReader的博士= NULL;
博士= cmd.ExecuteReader(CommandBehavior.CloseConnection);
如果(dr.HasRows)
{
dt.Load(DR);
}
返回DT;
}
}



使用的SqlDataAdapter

 公开数据表populateUsingDataAdapter(字符串更改为MyQuery)
{
SqlDataAdapter的DAP =新的SqlDataAdapter (更改为MyQuery,CN);
的DataSet DS =新的DataSet();
dap.Fill(DS);
返回ds.Tables [0];
}


解决方案

的差异可以忽略不计,所以它可能会更好用更简洁的版本: SqlDataAdapter.Fill



SqlDataReader的.fill伪创建内部类 LoadAdapter 内部(从的DataAdapter 导出),并调用它填写方法:表现会非常类似于 SqlDataAdapter.Fill(数据表)



有将在参数初始化/确认一些小的差异,但由于行的数量增加,这将成为少显著



还要注意你的第二个样品应修改为与第一相媲美:

 公开数据表populateUsingDataAdapter(字符串更改为MyQuery)
{
使用(SqlConnection的CON =新的SqlConnection(constring))
{
SqlDataAdapter的DAP =新SqlDataAdapter的(更改为MyQuery,CON);
DataTable的DT =新的DataTable();
dap.Fill(DT);
返回DT;
}
}


I want to know which one has the better performance for returning a DataTable. Here for SqlDataReader I use DataTable.Load(dr)

Using SqlDataReader:

public static DataTable populateUsingDataReader(string myQuery)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(constring))
    {
        SqlCommand cmd = new SqlCommand(myQuery, con);
        con.Open();
        SqlDataReader dr = null;
        dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        if (dr.HasRows)
        {
            dt.Load(dr);
        }
        return dt;
    }
}

using SqlDataAdapter:

public DataTable populateUsingDataAdapter(string myQuery)
{
    SqlDataAdapter dap = new SqlDataAdapter(myQuery,cn);
    DataSet ds = new DataSet();
    dap.Fill(ds);
    return ds.Tables[0];
}

解决方案

The difference will be negligible, so it's probably better to use the more concise version: SqlDataAdapter.Fill.

SqlDataReader.Fill creates an internal class LoadAdapter (derived from DataAdapter) internally, and calls its Fill method: performance will be very similar to SqlDataAdapter.Fill(DataTable).

There will be some small differences in initialization / validation of arguments, but as the number of rows increases, this will become less and less significant.

Note also that your second sample should be modified to be comparable with the first:

public DataTable populateUsingDataAdapter(string myQuery)
{
    using (SqlConnection con = new SqlConnection(constring))
    {
        SqlDataAdapter dap = new SqlDataAdapter(myQuery,con);
        DataTable dt = new DataTable();
        dap.Fill(dt);
        return dt;
    }
}

这篇关于SqlDataReader的VS SqlDataAdapter的:其中一个拥有返回一个DataTable更好的性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆