protected void createDatabaseButton_Click(object sender, EventArgs e) { string databasePath = Path.Combine(Server.MapPath(Request.ApplicationPath), "App_Data"); if (!Directory.Exists(databasePath)) { Directory.CreateDirectory(databasePath); } string connectionString = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString.Replace("|DataDirectory|", databasePath); var dc = new DataClassesDataContext(connectionString); if (dc.DatabaseExists()) { dc.DeleteDatabase(); } try { dc.CreateDatabase(); // Fill with sample data. dc.OAuthConsumers.InsertOnSubmit(new OAuthConsumer { ConsumerKey = "sampleconsumer", ConsumerSecret = "samplesecret", }); dc.Users.InsertOnSubmit(new User { OpenIDFriendlyIdentifier = "=arnott", OpenIDClaimedIdentifier = "=!9B72.7DD1.50A9.5CCD", Age = 27, FullName = "Andrew Arnott", FavoriteSites = new System.Data.Linq.EntitySet<FavoriteSite> { new FavoriteSite { SiteUrl = "http://www.microsoft.com" }, new FavoriteSite { SiteUrl = "http://www.google.com" }, }, }); dc.SubmitChanges(); this.databaseStatus.Visible = true; } catch (System.Data.SqlClient.SqlException ex) { foreach (System.Data.SqlClient.SqlError error in ex.Errors) { Response.Write(error.Message); } } }
private void Application_Error(object sender, EventArgs e) { Logger.Error("An unhandled exception occurred in ASP.NET processing: " + Server.GetLastError(), Server.GetLastError()); // In the event of an unhandled exception, reverse any changes that were made to the database to avoid any partial database updates. var dataContext = dataContextSimple; if (dataContext != null) { dataContext.Transaction.Rollback(); dataContext.Connection.Close(); dataContext.Dispose(); dataContextSimple = null; } }