public
DataView GetProducts()
{
DataSet dstProducts;
SqlConnection conNorthwind;
SqlDataAdapter dadProducts;
DataView dvwProducts = Cache["Products"] as DataView;
if (dvwProducts == null)
{
dstProducts = new DataSet();
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
dadProducts = new SqlDataAdapter( "Select * from Products", conNorthwind );
dadProducts.Fill( dstProducts, "Products" );
dvwProducts = dstProducts.Tables["Products"].DefaultView;
Cache["Products"] = dvwProducts;
}
return dvwProducts;
}
Cache一个DataSet
{
DataSet dstProducts;
SqlConnection conNorthwind;
SqlDataAdapter dadProducts;
DataView dvwProducts = Cache["Products"] as DataView;
if (dvwProducts == null)
{
dstProducts = new DataSet();
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
dadProducts = new SqlDataAdapter( "Select * from Products", conNorthwind );
dadProducts.Fill( dstProducts, "Products" );
dvwProducts = dstProducts.Tables["Products"].DefaultView;
Cache["Products"] = dvwProducts;
}
return dvwProducts;
}
DataSet中有两表,如果不用可以Cache.Remove["Products"]去除
dstStore
=
(DataSet)Cache[
"
Store
"
];
// Cache.Remove("Products");
if ( dstStore == null ) {
dstStore = new DataSet();
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
dadNorthwind = new SqlDataAdapter( "Select CategoryID, CategoryName From Categories", conNorthwind );
conNorthwind.Open();
dadNorthwind.Fill( dstStore, "Categories" );
dadNorthwind.SelectCommand = new SqlCommand( "Select * From Products", conNorthwind );
dadNorthwind.Fill( dstStore, "Products" );
conNorthwind.Close();
//DataSet dstStore = (DataSet)Cache["Store"];
}
// Cache.Remove("Products");
if ( dstStore == null ) {
dstStore = new DataSet();
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
dadNorthwind = new SqlDataAdapter( "Select CategoryID, CategoryName From Categories", conNorthwind );
conNorthwind.Open();
dadNorthwind.Fill( dstStore, "Categories" );
dadNorthwind.SelectCommand = new SqlCommand( "Select * From Products", conNorthwind );
dadNorthwind.Fill( dstStore, "Products" );
conNorthwind.Close();
//DataSet dstStore = (DataSet)Cache["Store"];
}
在内存中找到你所要的数据
DataSet dstEmployees;
SqlConnection conNorthwind;
SqlDataAdapter dadEmployees;
DataView dvwEmployees;
Object[] arrValues = new Object[ 2 ];
int intEmployeeIndex;
// Get cached DataView
dvwEmployees = (DataView)Cache[ " Employees " ];
if (dvwEmployees == null )
{
dstEmployees = new DataSet();
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
dadEmployees = new SqlDataAdapter( "Select * From Employees", conNorthwind );
dadEmployees.Fill( dstEmployees, "Employees" );
dvwEmployees = dstEmployees.Tables["Employees"].DefaultView;
dvwEmployees.Sort = "LastName, FirstName";
Cache["Employees"] = dvwEmployees;
}
// Find the employee
arrValues[ 0 ] = txtLastName.Text;
arrValues[ 1 ] = txtFirstName.Text;
if (dvwEmployees == null )
intEmployeeIndex =- 1 ;
else
intEmployeeIndex = dvwEmployees.Find( arrValues );
if (intEmployeeIndex > - 1 )
{
lblName.Text = txtLastName.Text + ", " + txtFirstName.Text;
lblPhone.Text = dvwEmployees[ intEmployeeIndex ].Row[ "HomePhone" ].ToString();
lblNotes.Text = dvwEmployees[ intEmployeeIndex ].Row[ "Notes" ].ToString();
}
else
lblError.Text = " Employee Not Found! " ;
SqlConnection conNorthwind;
SqlDataAdapter dadEmployees;
DataView dvwEmployees;
Object[] arrValues = new Object[ 2 ];
int intEmployeeIndex;
// Get cached DataView
dvwEmployees = (DataView)Cache[ " Employees " ];
if (dvwEmployees == null )
{
dstEmployees = new DataSet();
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
dadEmployees = new SqlDataAdapter( "Select * From Employees", conNorthwind );
dadEmployees.Fill( dstEmployees, "Employees" );
dvwEmployees = dstEmployees.Tables["Employees"].DefaultView;
dvwEmployees.Sort = "LastName, FirstName";
Cache["Employees"] = dvwEmployees;
}
// Find the employee
arrValues[ 0 ] = txtLastName.Text;
arrValues[ 1 ] = txtFirstName.Text;
if (dvwEmployees == null )
intEmployeeIndex =- 1 ;
else
intEmployeeIndex = dvwEmployees.Find( arrValues );
if (intEmployeeIndex > - 1 )
{
lblName.Text = txtLastName.Text + ", " + txtFirstName.Text;
lblPhone.Text = dvwEmployees[ intEmployeeIndex ].Row[ "HomePhone" ].ToString();
lblNotes.Text = dvwEmployees[ intEmployeeIndex ].Row[ "Notes" ].ToString();
}
else
lblError.Text = " Employee Not Found! " ;
以下是源程序
<%
@ Page Language
=
"
C#
"
%>
<% @ Import Namespace = " System.Data " %>
<% @ Import Namespace = " System.Data.SqlClient " %>
< script runat = server >
void Button_Click(Object sender , EventArgs e)
{
DataSet dstEmployees;
SqlConnection conNorthwind;
SqlDataAdapter dadEmployees;
DataView dvwEmployees;
Object[] arrValues = new Object[2];
int intEmployeeIndex;
//Get cached DataView
dvwEmployees = (DataView)Cache["Employees"];
if (dvwEmployees == null)
{
dstEmployees = new DataSet();
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
dadEmployees = new SqlDataAdapter( "Select * From Employees", conNorthwind );
dadEmployees.Fill( dstEmployees, "Employees" );
dvwEmployees = dstEmployees.Tables["Employees"].DefaultView;
dvwEmployees.Sort = "LastName, FirstName";
Cache["Employees"] = dvwEmployees;
}
//Find the employee
arrValues[0] = txtLastName.Text;
arrValues[1] = txtFirstName.Text;
if (dvwEmployees == null)
intEmployeeIndex=-1;
else
intEmployeeIndex = dvwEmployees.Find( arrValues );
if (intEmployeeIndex > -1 )
{
lblName.Text = txtLastName.Text + ", " + txtFirstName.Text;
lblPhone.Text = dvwEmployees[ intEmployeeIndex ].Row[ "HomePhone" ].ToString();
lblNotes.Text = dvwEmployees[ intEmployeeIndex ].Row[ "Notes" ].ToString();
}
else
lblError.Text = "Employee Not Found!";
}
</ Script >
< html >
< head >< title > CacheEmployees.aspx </ title ></ head >
< body >
< h2 > Employee Directory </ h2 >
< form Runat = " Server " >
< b > First Name: </ b >
< asp:TextBox
ID = " txtFirstName "
Runat = " Server " />
< p >
< b > Last Name: </ b >
< asp:TextBox
ID = " txtLastName "
Runat = " Server " />
< asp:Button
Text = " Find! "
OnClick = " Button_Click "
Runat = " Server " />
< hr >
< asp:Label
ID = " lblError "
ForeColor = " Red "
EnableViewState = " False "
Runat = " Server " />
< asp:Label
ID = " lblName "
EnableViewState = " False "
Runat = " Server " />
< p >
< asp:Label
ID = " lblPhone "
EnableViewState = " False "
Runat = " Server " />
< p >
< asp:Label
ID = " lblNotes "
EnableViewState = " False "
Runat = " Server " />
</ form >
</ body >
</ html >
<% @ Import Namespace = " System.Data " %>
<% @ Import Namespace = " System.Data.SqlClient " %>
< script runat = server >
void Button_Click(Object sender , EventArgs e)
{
DataSet dstEmployees;
SqlConnection conNorthwind;
SqlDataAdapter dadEmployees;
DataView dvwEmployees;
Object[] arrValues = new Object[2];
int intEmployeeIndex;
//Get cached DataView
dvwEmployees = (DataView)Cache["Employees"];
if (dvwEmployees == null)
{
dstEmployees = new DataSet();
conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;Database=Northwind" );
dadEmployees = new SqlDataAdapter( "Select * From Employees", conNorthwind );
dadEmployees.Fill( dstEmployees, "Employees" );
dvwEmployees = dstEmployees.Tables["Employees"].DefaultView;
dvwEmployees.Sort = "LastName, FirstName";
Cache["Employees"] = dvwEmployees;
}
//Find the employee
arrValues[0] = txtLastName.Text;
arrValues[1] = txtFirstName.Text;
if (dvwEmployees == null)
intEmployeeIndex=-1;
else
intEmployeeIndex = dvwEmployees.Find( arrValues );
if (intEmployeeIndex > -1 )
{
lblName.Text = txtLastName.Text + ", " + txtFirstName.Text;
lblPhone.Text = dvwEmployees[ intEmployeeIndex ].Row[ "HomePhone" ].ToString();
lblNotes.Text = dvwEmployees[ intEmployeeIndex ].Row[ "Notes" ].ToString();
}
else
lblError.Text = "Employee Not Found!";
}
</ Script >
< html >
< head >< title > CacheEmployees.aspx </ title ></ head >
< body >
< h2 > Employee Directory </ h2 >
< form Runat = " Server " >
< b > First Name: </ b >
< asp:TextBox
ID = " txtFirstName "
Runat = " Server " />
< p >
< b > Last Name: </ b >
< asp:TextBox
ID = " txtLastName "
Runat = " Server " />
< asp:Button
Text = " Find! "
OnClick = " Button_Click "
Runat = " Server " />
< hr >
< asp:Label
ID = " lblError "
ForeColor = " Red "
EnableViewState = " False "
Runat = " Server " />
< asp:Label
ID = " lblName "
EnableViewState = " False "
Runat = " Server " />
< p >
< asp:Label
ID = " lblPhone "
EnableViewState = " False "
Runat = " Server " />
< p >
< asp:Label
ID = " lblNotes "
EnableViewState = " False "
Runat = " Server " />
</ form >
</ body >
</ html >