Is datareader quicker than dataset when populating a datatable?

前端 未结 8 1111
一个人的身影
一个人的身影 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: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().

提交回复
热议问题