DataSet 的Cache功能

在内存中保存一个DataView
None.gif public  DataView GetProducts()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    DataSet dstProducts;
InBlock.gif    SqlConnection conNorthwind;
InBlock.gif    SqlDataAdapter dadProducts;
InBlock.gif    DataView dvwProducts 
= Cache["Products"as DataView;
InBlock.gif    
if (dvwProducts == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        dstProducts 
= new DataSet();
InBlock.gif        conNorthwind 
= new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
InBlock.gif        dadProducts 
= new SqlDataAdapter( "Select * from Products", conNorthwind );
InBlock.gif        dadProducts.Fill( dstProducts, 
"Products" );
InBlock.gif        dvwProducts 
= dstProducts.Tables["Products"].DefaultView;
InBlock.gif        Cache[
"Products"= dvwProducts;
ExpandedSubBlockEnd.gif    }

InBlock.gif    
return dvwProducts;
ExpandedBlockEnd.gif }
 
Cache一个DataSet
DataSet中有两表,如果不用可以Cache.Remove["Products"]去除
None.gif dstStore  =  (DataSet)Cache[ " Store " ];
None.gif    
// Cache.Remove("Products");
None.gif
     
ExpandedBlockStart.gifContractedBlock.gif    
if  ( dstStore  ==   null  )  dot.gif {
InBlock.gif        dstStore 
= new DataSet();
InBlock.gif        conNorthwind 
= new SqlConnection(  @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
InBlock.gif        dadNorthwind 
= new SqlDataAdapter( "Select CategoryID, CategoryName From Categories", conNorthwind );
InBlock.gif        conNorthwind.Open();
InBlock.gif        dadNorthwind.Fill( dstStore, 
"Categories" );
InBlock.gif        dadNorthwind.SelectCommand 
= new SqlCommand( "Select * From Products", conNorthwind );
InBlock.gif        dadNorthwind.Fill( dstStore, 
"Products" );
InBlock.gif        conNorthwind.Close();
InBlock.gif        
//DataSet dstStore = (DataSet)Cache["Store"];
ExpandedBlockEnd.gif
    }

在内存中找到你所要的数据
None.gif DataSet dstEmployees;
None.gif    SqlConnection conNorthwind;
None.gif    SqlDataAdapter dadEmployees;
None.gif    DataView dvwEmployees;
None.gif    Object[] arrValues 
=   new  Object[ 2 ];
None.gif    
int  intEmployeeIndex;
None.gif
None.gif    
// Get cached DataView
None.gif
      
None.gif    dvwEmployees 
=  (DataView)Cache[ " Employees " ];
None.gif    
if  (dvwEmployees  ==   null )
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        dstEmployees 
= new DataSet();
InBlock.gif        conNorthwind 
= new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
InBlock.gif        dadEmployees 
= new SqlDataAdapter( "Select * From Employees", conNorthwind );
InBlock.gif        dadEmployees.Fill( dstEmployees, 
"Employees" );
InBlock.gif        dvwEmployees 
= dstEmployees.Tables["Employees"].DefaultView;
InBlock.gif        dvwEmployees.Sort 
= "LastName, FirstName";
InBlock.gif        Cache[
"Employees"= dvwEmployees;
ExpandedBlockEnd.gif    }

None.gif
None.gif    
// Find the employee
None.gif
    arrValues[ 0 =  txtLastName.Text;
None.gif    arrValues[
1 =  txtFirstName.Text;
None.gif    
if  (dvwEmployees  ==   null )
None.gif        intEmployeeIndex
=- 1 ;
None.gif    
else
None.gif        intEmployeeIndex 
=  dvwEmployees.Find( arrValues );
None.gif    
if  (intEmployeeIndex  >   - 1  ) 
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        lblName.Text 
= txtLastName.Text + "" + txtFirstName.Text;
InBlock.gif        lblPhone.Text 
= dvwEmployees[ intEmployeeIndex ].Row[ "HomePhone" ].ToString();
InBlock.gif        lblNotes.Text 
= dvwEmployees[ intEmployeeIndex ].Row[ "Notes" ].ToString();
ExpandedBlockEnd.gif    }

None.gif    
else
None.gif        lblError.Text 
=   " Employee Not Found! " ;

以下是源程序
None.gif <% @ Page Language = " C# "   %>
None.gif
<% @ Import Namespace = " System.Data "   %>
None.gif
<% @ Import Namespace = " System.Data.SqlClient "   %>
None.gif
None.gif
< script runat = server >
None.gif
void  Button_Click(Object sender , EventArgs e) 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    DataSet dstEmployees;
InBlock.gif    SqlConnection conNorthwind;
InBlock.gif    SqlDataAdapter dadEmployees;
InBlock.gif    DataView dvwEmployees;
InBlock.gif    Object[] arrValues 
= new Object[2];
InBlock.gif    
int intEmployeeIndex;
InBlock.gif
InBlock.gif    
//Get cached DataView
InBlock.gif
      
InBlock.gif    dvwEmployees 
= (DataView)Cache["Employees"];
InBlock.gif    
if (dvwEmployees == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        dstEmployees 
= new DataSet();
InBlock.gif        conNorthwind 
= new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
InBlock.gif        dadEmployees 
= new SqlDataAdapter( "Select * From Employees", conNorthwind );
InBlock.gif        dadEmployees.Fill( dstEmployees, 
"Employees" );
InBlock.gif        dvwEmployees 
= dstEmployees.Tables["Employees"].DefaultView;
InBlock.gif        dvwEmployees.Sort 
= "LastName, FirstName";
InBlock.gif        Cache[
"Employees"= dvwEmployees;
ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
//Find the employee
InBlock.gif
    arrValues[0= txtLastName.Text;
InBlock.gif    arrValues[
1= txtFirstName.Text;
InBlock.gif    
if (dvwEmployees == null)
InBlock.gif        intEmployeeIndex
=-1;
InBlock.gif    
else
InBlock.gif        intEmployeeIndex 
= dvwEmployees.Find( arrValues );
InBlock.gif    
if (intEmployeeIndex > -1 ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        lblName.Text 
= txtLastName.Text + "" + txtFirstName.Text;
InBlock.gif        lblPhone.Text 
= dvwEmployees[ intEmployeeIndex ].Row[ "HomePhone" ].ToString();
InBlock.gif        lblNotes.Text 
= dvwEmployees[ intEmployeeIndex ].Row[ "Notes" ].ToString();
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
InBlock.gif        lblError.Text 
= "Employee Not Found!";
ExpandedBlockEnd.gif }

None.gif
None.gif
</ Script >
None.gif
None.gif
< html >
None.gif
< head >< title > CacheEmployees.aspx </ title ></ head >
None.gif
< body >
None.gif
None.gif
< h2 > Employee Directory </ h2 >
None.gif
None.gif
< form Runat = " Server " >
None.gif
< b > First Name: </ b >
None.gif
< asp:TextBox
None.gif  ID
= " txtFirstName "
None.gif  Runat
= " Server "   />
None.gif
< p >
None.gif
< b > Last Name: </ b >
None.gif
< asp:TextBox
None.gif  ID
= " txtLastName "
None.gif  Runat
= " Server "   />
None.gif
< asp:Button
None.gif  Text
= " Find! "
None.gif  OnClick
= " Button_Click "
None.gif  Runat
= " Server "   />
None.gif
< hr >
None.gif
< asp:Label
None.gif  ID
= " lblError "
None.gif  ForeColor
= " Red "
None.gif  EnableViewState
= " False "
None.gif  Runat
= " Server "   />
None.gif
< asp:Label
None.gif  ID
= " lblName "
None.gif  EnableViewState
= " False "
None.gif  Runat
= " Server "   />
None.gif
< p >
None.gif
< asp:Label
None.gif  ID
= " lblPhone "
None.gif  EnableViewState
= " False "
None.gif  Runat
= " Server "   />
None.gif
< p >
None.gif
< asp:Label
None.gif  ID
= " lblNotes "
None.gif  EnableViewState
= " False "
None.gif  Runat
= " Server "   />
None.gif
</ form >
None.gif
None.gif
</ body >
None.gif
</ html >
None.gif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值