Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Services.Authentication Prototype/Persistence/DatabaseUtil.cs @ 3970

Last change on this file since 3970 was 3970, checked in by bfarka, 14 years ago

made Database configable with app.config in the Persistence Project --> 2 different DBs can be configured (one for UnitTesting and a real one) (#1063)

File size: 1.5 KB
Line 
1using System;
2using System.Diagnostics;
3
4namespace Persistence {
5  /// <summary>
6  /// combines multiple used static methods into one class
7  /// </summary>
8  public class DatabaseUtil {
9
10    protected static bool productionDatabase = true;
11
12    public static bool ProductionDatabase {
13      get {
14        return DatabaseUtil.productionDatabase;
15      }
16      set {
17        DatabaseUtil.productionDatabase = value;
18      }
19    }
20   
21    /// <summary>
22    /// creates and returns a database connection, if possible
23    /// </summary>
24    /// <returns>database connection (could be null)</returns>
25    public static DataClassesDataContext createDataClassesDataContext() {
26      if (productionDatabase) {
27        return new Persistence.DataClassesDataContext(new Persistence.Properties.Settings().DatabaseConnectionString);
28      } else {
29        return new Persistence.DataClassesDataContext(new Persistence.Properties.Settings().DatabaseConnectionStringTesting);
30      }
31    }
32
33    /// <summary>
34    /// creates a new database out of the LINQ to SQL classes
35    /// </summary>
36    /// <param name="db">DataClassesDataContext</param>
37    public static void createDatabase(DataClassesDataContext db) {
38       
39      if (db.DatabaseExists()) {
40        Console.WriteLine("Deleting old database...");
41        db.DeleteDatabase();
42        Console.WriteLine("Deleted old database!");
43      }
44
45      Console.WriteLine("Creating new database...");
46      db.CreateDatabase();
47      Console.WriteLine("Created new database!");
48    }
49  }
50}
Note: See TracBrowser for help on using the repository browser.