收集一些关于几个类型的知识点:
一.关系
IList il=new ArrayList();
IList是一个接口,AarrayList继承这个接口,il是一个ArrayList类型的对象
C#中实例变量定义的格式是
静态类型 变量名=实例;
如果静态类型是类,实例可以是静态类型本身的实例对象;也可以是其子类的实例对象
如果静态类型是接口,实例可以是其实现类的实例或其子接口实现类的实例
IList il=new ArrayList();
是第二种情况
二.性能
显示数据时,通常以上几个对象作为载体,具体哪种方式性能比较好呢,已有人提供测试的方式,这里只引用
其测试结果。
TypedDataTable < DataTable < ArrayList < IList < Array
TypedDatatTable比DataTalbe花的时间将近几倍
ArrayList比IList慢,是因为需要转型
在长度确定的情况下Array比IList要快。
ArrayList、IList、Array在性能上相差无几,综合以上,建议采取IList。
if (!IsPostBack) { /******************************************************************************** declare @i int --变义变量@i set @i = 10 --初始化@i为1 while @i < 100000000 --如果@i小于100,这里改成100万即是插入100万条记录 begin ------------------ insert into price (name, ProvinceID) values('美称朝鲜同意将1.2万枚核燃料棒出售运往国外 美称朝鲜同意将1.2万枚核燃料棒出售运往国外 美称朝鲜同意将1.2万枚核燃料棒出售运往国外 美称朝鲜同意将1.2万枚核燃料棒出售运往国外 美称朝鲜同意将1.2万枚核燃料棒出售运往国外 美称朝鲜同意将1.2万枚核燃料棒出售运往国外 ' , 1) set @i = @i + 1 end -------------------- SET NOCOUNT on /******************************************************************************** /************* 清除数据缓存 ****************/ Response.Buffer = true; Response.Expires = 0; Response.ExpiresAbsolute = DateTime.Now.AddDays(-1); Response.AddHeader("pragma", "no-cache"); Response.AddHeader("cache-control", "private"); Response.CacheControl = "no-cache"; int i = 0; SqlConnection cn = new SqlConnection(DAL.DBHelper.ConnectionString); SqlCommand cm = new SqlCommand("select * from test", cn); cn.Open(); SqlDataReader rdr = cm.ExecuteReader(); while (rdr.Read()) { i++; } rdr.Close(); cn.Close(); SqlDataAdapter ada = null; Response.Write("开始....<br><table border=\"1\" cellpadding=\"2\" cellspacing=\"1\">"); DateTime start; DateTime end; TimeSpan[] TS = new TimeSpan[5]; //for (int counter = 1; counter <= 100; counter++) //{ start = DateTime.Now; for (int smallCounter = 1; smallCounter <= 1; smallCounter++) { ada = new SqlDataAdapter("select * from test", cn); DataSet ds = new DataSet(); cn.Open(); ada.Fill(ds); cn.Close(); } end = DateTime.Now; TS[0] += end - start; Response.Write("<tr style='height:20px;'><td>DataSet:</td><td style='100px;'>" + Convert.ToString(end - start) + "</td></tr>"); start = DateTime.Now; for (int smallCounter = 1; smallCounter <= 1; smallCounter++) { cm = new SqlCommand("select * from test", cn); IList<DAL.Tts_Hotel_Price> MyList = new List<DAL.Tts_Hotel_Price>(); cn.Open(); rdr = cm.ExecuteReader(); while (rdr.Read()) { //new DAL.Tts_Hotel_Price(rdr.GetInt64(0), rdr.GetInt64(1), rdr.GetString(2)) MyList.Add(new DAL.Tts_Hotel_Price()); } rdr.Close(); cn.Close(); } end = DateTime.Now; TS[1] += end - start; Response.Write("<tr><td>IList:</td><td>" + Convert.ToString(end - start) + "</td></tr>"); start = DateTime.Now; for (int smallCounter = 1; smallCounter <= 1; smallCounter++) { cm = new SqlCommand("select * from test", cn); ArrayList MyAl = new ArrayList(); cn.Open(); rdr = cm.ExecuteReader(); while (rdr.Read()) { //new DAL.Tts_Hotel_Price(rdr.GetInt64(0), rdr.GetInt64(1), rdr.GetString(2)) MyAl.Add(rdr); } rdr.Close(); cn.Close(); } end = DateTime.Now; TS[2] += end - start; Response.Write("<tr><td>ArrayList:</td><td>" + Convert.ToString(end - start) + "</td></tr>"); start = DateTime.Now; for (int smallCounter = 1; smallCounter <= 1; smallCounter++) { cm = new SqlCommand("select * from test", cn); DAL.Tts_Hotel_Price[] MyArray = new DAL.Tts_Hotel_Price[200800]; cn.Open(); int flag = 0; rdr = cm.ExecuteReader(); while (rdr.Read()) { MyArray[flag] = new DAL.Tts_Hotel_Price(); // new DAL.Tts_Hotel_Price(rdr.GetInt64(0), rdr.GetInt64(1), rdr.GetString(2)); flag++; } rdr.Close(); cn.Close(); } end = DateTime.Now; TS[3] += end - start; Response.Write("<tr><td>[]:</td><td>" + Convert.ToString(end - start) + "</td></tr>"); start = DateTime.Now; for (int smallCounter = 1; smallCounter <= 1; smallCounter++) { ada = new SqlDataAdapter("select * from test", cn); DataTable aTable = new DataTable(); cn.Open(); ada.Fill(aTable); cn.Close(); } end = DateTime.Now; TS[4] += end - start; Response.Write("<tr><td>DataTable:</td><td>" + Convert.ToString(end - start) + "</td></tr>"); Response.Write("</table><br>结束............"); //foreach (TimeSpan ts in TS) //{ // Response.Write("<br>" + ts ); //} }