/* Thor Server 2.0 Project - Codenamed "Stockholm" Copyright 2008 - 2009 Joe Hegarty This file is part of The Thor Server 2.0 Project - Codenamed "Stockholm". The Thor Server 2.0 Project - Codenamed "Stockholm" is free software: you can redistribute it and/or modify it under the terms of the Microsoft Public License The Thor Server Project is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Microsoft Public License for more details. You should have received a copy of the Microsoft Public License along with The Thor Server Project. If not, see http://www.opensource.org/licenses/ms-pl.html */ using System; using System.Linq; using StockholmLibrary.Util; namespace StockholmLibrary.Data { //------------------------------------ public class DataManager { private const int EXPECTED_DATABASE_VERSION = 1; private string _connString; //********************************************** public StockholmDbDataContext Database { get { System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(_connString); return new StockholmDbDataContext(connection); } } //********************************************** public DataManager(string databaseServer, string databaseName, string databaseUser, string databasePassword) { _connString = String.Format("Data Source={0};Initial Catalog={1};User Id={2};Password={3};", databaseServer, databaseName, databaseUser, databasePassword); try { System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(_connString); StockholmDbDataContext db = new StockholmDbDataContext(connection); var version = from p in db.Settings where p.DatabaseVersion == EXPECTED_DATABASE_VERSION select p.DatabaseVersion; if (version.Count() == 1) { Logging.LogEvent("DataManager", "Database connection opened successfully", Logging.ELogLevel.INFO); } else { Logging.LogEvent("DataManager", "Wrong database version", Logging.ELogLevel.ERROR); Stockholm.Get().IsRunning = false; } } catch(Exception e) { Logging.LogEvent("DataManager", "Failed to open database connection." + e.ToString(), Logging.ELogLevel.ERROR); Stockholm.Get().IsRunning = false; } } } }