Is datareader quicker than dataset when populating a datatable?

前端 未结 8 1068
一个人的身影
一个人的身影 2020-11-29 11:43

Which would be quicker.

1) Looping a datareader and creating a custom rows and columns based populated datatable

2) Or creating a dataAdapter object and just

相关标签:
8条回答
  • 2020-11-29 12:15

    It's nice to have DataReader when you need for example show progress of loading data. In DataSet you can't do something in a middle of loading data.

    On the other hand DataSet is all-in-one object. So DataSet is much slower. DataReader can give you additional boost in places in your code where data operation is very slow. In these places change it from DataSet to DataReader. DataReader also takes less space in memory.

    Oh course it takes more time to code good DataReader, but it's worth it. For example when you play with images or music taken from database.

    More on this topic in MSDN Magazine

    0 讨论(0)
  • 2020-11-29 12:16

    Your option #1 would be slower. However, there's a better way to convert a datareader to a datatable than adding custom rows by hand:

    DataTable dt = new DataTable();
    
    using (SqlConnection conn = GetOpenSqlConnection())
    using (SqlCommand cmd = new SqlCommand("SQL Query here", conn)
    using (IDataReader rdr = cmd.ExecuteReader())
    {
        dt.Load(rdr);
    }
    

    I can't comment on the difference between this and using .Fill().

    0 讨论(0)
提交回复
热议问题