Preferred Language:
Listing 27.22 - Global.asax
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Configuration" %>
<script runat="server">
private string _conString;
private SqlConnection _con;
private SqlCommand _cmdSelect;
private SqlCommand _cmdInsert;
public override void Init()
{
// initialize connection
_conString = WebConfigurationManager.ConnectionStrings["Log"].ConnectionString;
_con = new SqlConnection(_conString);
// initialize select command
_cmdSelect = new SqlCommand("SELECT COUNT(*) FROM Log WHERE Path=@Path", _con);
_cmdSelect.Parameters.Add("@Path", SqlDbType.NVarChar, 500);
// initialize insert command
_cmdInsert = new SqlCommand("INSERT Log (Path) VALUES (@Path)", _con);
_cmdInsert.Parameters.Add("@Path", SqlDbType.NVarChar, 500);
}
public int NumberOfRequests
{
get
{
int result = 0;
_cmdSelect.Parameters["@Path"].Value = Request.AppRelativeCurrentExecutionFilePath;
try
{
_con.Open();
result = (int)_cmdSelect.ExecuteScalar();
}
finally
{
_con.Close();
}
return result;
}
}
void Application_BeginRequest(object sender, EventArgs e)
{
// Record new request
_cmdInsert.Parameters["@Path"].Value = Request.AppRelativeCurrentExecutionFilePath;
try
{
_con.Open();
_cmdInsert.ExecuteNonQuery();
}
finally
{
_con.Close();
}
}
</script>
Need ASP.NET and Visual Studio 2008 Training?
- Learn ASP.NET 3.5 from Stephen Walther, author of ASP.NET 3.5 Unleashed.
We've provided ASP.NET training for NASA, Lockheed Martin, the National Science Foundation, Verizon,
Boeing, the US House of Representatives, Kaiser, Petco, Mary Kay, and Microsoft.
Why not your company?
-
Receive a four day, hands-on, intensive workshop.
-
We fly to you, anywhere in the world.
-
We can bring our own laptops.
To learn more, visit the
Superexpert Training website.