shicl个人天地

程序员的学习历程

博客园 首页 新随笔 联系 订阅 管理

    如何处理批量的数据,将是每个从事WEB开发的人所面临的问题。现在web 开发对这些的解决方案并不是太好,例如AJAX很好的提高了用户的体验,但是不可否认,运用Ajax技术不同程度上会造成速度的缓慢。
   如果是批量的大量数据处理我一直不太认为对于大型的数据处理使用Ajax。
    言归正传,我们如何提高用户操作的方便性,就像OFFICE一样处理我们的web页面。Matt Berseth提供了一个解决方案。
   首先我们需要建立Customer类如下:

Code

然后创建类CustomerDAO
public CustomerDAO()
{
//
//TODO: 
//
}

private List<Customer> Customers
{
get
{
List
<Customer> customers =
HttpContext.Current.Session[
"Customers"as List<Customer>;
if (customers == null)
{
customers 
= new List<Customer>();
for (int i = 0; i < 8; i++)
{
customers.Add(
new Customer() { ID = (i + 1) });
}

// cache the list
HttpContext.Current.Session["Customers"= customers;
}

return customers;
}

set { HttpContext.Current.Session["Customers"= value; }
}

private List<Customer> PersistedCustomers
{
get
{
List
<Customer> customers = HttpContext.Current.Session["PersistedCustomers"as List<Customer>;
// load the customers on first access
if (customers == null)
{
customers 
= new List<Customer>();
// cache the list
HttpContext.Current.Session["PersistedCustomers"= customers;
}

return customers;
}

}

[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<Customer> FindAll()
{
return this.Customers;
}

[DataObjectMethod(DataObjectMethodType.Select)]
public IEnumerable<Customer> FindAllPersistedCustomers()
{
return this.PersistedCustomers;
}

[DataObjectMethod(DataObjectMethodType.Update)]
public void Update(Customer newValues)
{
// simulate putting this record back into the database
Customer oldValues = this.Customers.Find(x => x.ID == newValues.ID);
// update the in-memory values
oldValues.FirstName = newValues.FirstName;
oldValues.LastName 
= newValues.LastName;
oldValues.Address 
= newValues.Address;
oldValues.City 
= newValues.City;
oldValues.State 
= newValues.State;
}

public void Save()
{
this.Customers.ForEach(delegate(Customer c)
{
if (c.IsValid)
{
c.ID 
= 1;
if (this.PersistedCustomers.Count > 0)
{
c.ID 
= this.PersistedCustomers.Last().ID + 1;
}

this.PersistedCustomers.Add(c);
}

}
);
this.Customers = null;
}


 然后创建 页面代码如下:
 
Code

后体代码为:
 
protected void Page_Load(object sender, EventArgs e)
{
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
// move the data from the controls
// back to our data object
foreach (ListViewDataItem i in this.lv.Items)
{
this.lv.UpdateItem(i.DataItemIndex, true);
}

// save the changes to the database
new CustomerDAO().Save();
}

protected void Page_PreRender(object sender, EventArgs e)
{
this.lvDebug.DataSource = new CustomerDAO().FindAllPersistedCustomers();
this.lvDebug.DataBind();
}

英文    http://mattberseth.com/blog/2008/05/bulk_inserting_data_with_the_l.html)

posted on 2008-05-26 22:33  SHICL  阅读(451)  评论(0)    收藏  举报