Tuesday, February 14, 2012

Working with DataCache

I have coded the following program to Explain DataCache ( SQLInjection to get datafrom database and put it in to cache) and reading data from an XML file and writing it in to cache




DataCache.aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataCache.aspx.cs" Inherits="DataCache" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
  
    <center ><fieldset ><legend>Data</legend>
<asp:GridView ID="gdData" runat="server" AutoGenerateColumns="true"  EnableModelValidation="True">
</asp:GridView>
   
</fieldset></center>
<fieldset >
<legend>reading from XML</legend>
<asp:GridView ID="gdXmlData" runat="server"></asp:GridView>
 </fieldset>
  
  
    </form>
</body>
</html>

DataCache.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DLL;

public partial class DataCache : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        gdData.DataSource = DataLayer.selectDept();
        gdData.DataBind();
        gdXmlData.DataSource = DataLayer.ReadFromXmlFile();
        gdXmlData.DataBind();
    }
   
}
DataLayer.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DLL
{
    public static  class DataLayer
    {
        public static void DeleteStaleCache()// explicitly remove cached data from Cache
        {
            HttpContext.Current.Cache.Remove("data");
        }
       
        public static DataTable selectDept()// selecting data from datasource and adding in to Cache
        {
            DataTable dt = new DataTable();
            if (HttpContext.Current.Cache["data"] == null)
            {
               // string conn = ConfigurationManager.ConnectionStrings["SqlDataSource1"].ToString();//  using predefined datasources to create connection
                SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\inetpub\wwwroot\Cachedata\App_Data\SCOTT.mdf;Integrated Security=True;User Instance=True");
               
                SqlCommand cmd = new SqlCommand("Select * from Department", connection);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(dt);
               
                HttpContext.Current.Cache.Insert("data", dt, null, DateTime .Now.AddSeconds(10), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            else
            {
                dt = HttpContext.Current.Cache["data"] as DataTable;
            }
            return dt;
        }

        public static DataTable ReadFromXmlFile()
        {
            DataSet ds = new DataSet();
            DataTable dt = null;
            if (HttpContext.Current.Cache["xmlOutput"] == null)
            {

                string filePath = HttpContext.Current.Server.MapPath("~/App_Data/Department.xml");
                ds.ReadXml(filePath);
             
                dt = ds.Tables[0];
                HttpContext.Current.Cache.Insert("xmlOutput", dt, null, DateTime.Now.AddSeconds(20), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            else
            {
                dt = HttpContext.Current.Cache["xmlOutput"] as DataTable;
            }
          
            return dt;
        }
    }
}

No comments:

Post a Comment