[测试]Gridview绑定SqlDataReader+IList和SqlDataAdapter+DataSet的效率

1,SqlDataAdapter+DataSet

     protected   void  Page_Load( object  sender, EventArgs e)
    {
        Stopwatch sp 
=   new  Stopwatch();

        sp.Start();
        
for  ( int  i  =   0 ; i  <   10000 ; i ++ )
        {
            Band_DataSet();
        }
        sp.Stop();
        Response.Write(sp.ElapsedMilliseconds.ToString() 
+   " <br> " );
    }

    
private   void  Band_DataSet()
    {

        SqlConnection conn 
=   new  SqlConnection();
        conn.ConnectionString 
=   "" ;
        conn.Open();
        SqlCommand cmd 
=   new  SqlCommand();
        cmd.CommandText 
=   " select * FROM province " ;
        cmd.CommandType 
=  CommandType.Text;
        cmd.Connection 
=  conn;

        SqlDataAdapter da 
=   new  SqlDataAdapter();
        da.SelectCommand 
=  cmd;
        DataSet ds 
=   new  DataSet();
        da.Fill(ds);
        conn.Close();

        GridView1.DataSource 
=  ds.Tables[ 0 ].DefaultView;
        GridView1.DataBind();
    }

 

结果,测试了4组:
Band_DataSet():单位MS
10712
10595
11018
10500
去掉第一组,平均为:10704

2,SqlDataReader+IList<T>

     protected   void  Page_Load( object  sender, EventArgs e)
    {
        Stopwatch sp 
=   new  Stopwatch();

        sp.Start();
        
for  ( int  i  =   0 ; i  <   10000 ; i ++ )
        {
            Band_DataReader();
        }
        sp.Stop();
        Response.Write(sp.ElapsedMilliseconds.ToString() 
+   " <br> " );
    }

    
private   void  Band_DataReader()
    {
        IList
< Province >  province  =   new  List < Province > ();

        SqlConnection conn 
=   new  SqlConnection();
        conn.ConnectionString 
=   " server=.;database=mytest;uid=myid;pwd=myid; " ;
        conn.Open();
        SqlCommand cmd 
=   new  SqlCommand();
        cmd.CommandText 
=   " select * FROM province " ;
        cmd.CommandType 
=  CommandType.Text;
        cmd.Connection 
=  conn;

        SqlDataReader dr 
=  cmd.ExecuteReader(CommandBehavior.CloseConnection);

        
while  (dr.Read())
        {
            Province p 
=   new  Province(dr[ 1 ].ToString(), dr[ 2 ].ToString());
            province.Add(p);
        }
        dr.Close();
        conn.Close();

        GridView1.DataSource 
=  province;
        GridView1.DataBind();
    }

 


Model类:
public   class  Province
{
    
public  Province()
    {
        
//
        
//  TODO: 在此处添加构造函数逻辑
        
//
    }

    
public  Province( string  _provinceID,  string  _provinceName)
    {
        provinceID 
=  _provinceID;
        provinceName 
=  _provinceName;
    }

    
private   string  provinceID;
    
private   string  provinceName;

    
public   string  ProvinceID
    {
        
get  {  return   this .provinceID; }
        
set  {  this .provinceID  =  value; }
    }

    
public   string  ProvinceName
    {
        
get  {  return   this .provinceName; }
        
set  {  this .provinceName  =  value; }
    }
}

同样测试4组:
Band_DataReader():单位:ms
7238
7222
7231
7141
去掉第一组,平均:7198

可见,datareader的效率高,后者只有前者的67%的时间。

ps:个人单纯的测试,可能有考虑不周的地方。

你可能感兴趣的:(GridView)