|
<%@ WebHandler Language="C#" Class="InstallDatabase" %>
using System;
using System.Web;
using System.Web.Management;
using System.Data.Common;
using MessageBoard.DataAccess.Linq;
using System.Web.Configuration;
using System.IO;
/// <summary>
/// This handler installs the database if the database does not already exists.
/// This should not be used on production machines
/// </summary>
public class InstallDatabase : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse response = context.Response;
response.Buffer = false;
response.ContentType = "text/html";
response.Write("<html><head><title>Creating Database</title></head><body><div>");
response.Write("Please wait while the database is being created....<br />");
response.Flush();
string connectionString = WebConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
string dataDirectory = AppDomain.CurrentDomain.GetData("DataDirectory") as String;
if (!dataDirectory.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()))
dataDirectory += System.IO.Path.DirectorySeparatorChar;
connectionString = connectionString.Replace("|DataDirectory|", dataDirectory);
MessageBoardDataContext dataContext = new MessageBoardDataContext(connectionString);
if (!dataContext.DatabaseExists())
{
dataContext.CreateDatabase();
response.Write("Adding ASP.NET Services....<br />");
response.Flush();
//Now add ASP.NET features
SqlServices.Install(dataContext.Connection.Database, SqlFeatures.All, connectionString);
response.Write("Installing sample data....<br />");
response.Flush();
string sampleDataSqlFile = context.Request.MapPath("~/Install/InstallSampleData.sql");
dataContext.ExecuteCommand(File.ReadAllText(sampleDataSqlFile));
response.Write("Database created successfully!<br />");
}
else
{
response.Write("Database already exists<br />");
}
response.Write("<script type='text/javascript'>window.setTimeout(function() { window.location.href = '../default.aspx'; }, 3000);</script>");
response.Write("If you are not automatically redirected, <a href='../default.aspx'>Click here</a> to go to the home page.<br />");
response.Write("</div></body></html>");
response.Flush();
}
public bool IsReusable
{
get
{
return false;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.