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