/[AnywhereTS-MSSQL]/trunk/TSAdminTool/Database.cs
ViewVC logotype

Diff of /trunk/TSAdminTool/Database.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/TSAdminTool/Database.cs	2012/07/11 19:34:51	21
+++ trunk/TSAdminTool/Database.cs	2012/07/12 13:01:44	45
@@ -6,11 +6,13 @@
 using System.Windows.Forms;
 using System.Text.RegularExpressions;
 using System.Management;
+using log4net;
 
 namespace AnywhereTS
 {
     class DatabaseSupport
     {
+        
         enum VersionCheck
         {
             Failed = 0, 
@@ -25,7 +27,6 @@
         private SqlCommand sqlCmd = new SqlCommand();
 
         const string SQL_BROWSER_SERVICE_NAME = "SQLBrowser";
-
         public static string InstanceName
         {
             get
@@ -47,10 +48,16 @@
         {
             bool bContinue = false;
             // Create a connection to SQL Server
+            
+            Logging.ATSAdminLog.Debug("SetupDatabase() called ");
             try
-            {
-                sqlCon.ConnectionString = Properties.Settings.Default.atsConnectionString;
-                sqlCon.Open();
+            {                
+                sqlCon.ConnectionString = Properties.Settings.Default.atsConnectionString;                
+                using (log4net.NDC.Push(string.Format("ConnectionString={0}", Properties.Settings.Default.atsConnectionString)))
+                {
+                    Logging.ATSAdminLog.Debug("Opening connection to AnywhereTS Database");
+                }
+                if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
             }
             catch
             {
@@ -59,26 +66,35 @@
                 //bCreateDB = true;
                 try
                 {
-                    try
+                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateDatabase.ToString())))
                     {
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString);
-                        sqlCon.Open();
-                        RunScript("DROP AnywhereTS");
+                        Logging.ATSAdminLog.Debug("Creating Database AnywhereTS");
+                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                        RunScript(Resource1.CreateDatabase.ToString());
                         sqlCon.Close();
                     }
-                    catch { }
+                    Logging.ATSAdminLog.Debug("Created Database AnywhereTS");
 
-                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
-                    sqlCon.Open();
-                    RunScript(Resource1.CreateDatabase.ToString());
-                    sqlCon.Close();
-                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString);
-                    sqlCon.Open();
+                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateTables.ToString())))
+                    {
+                        Logging.ATSAdminLog.Debug("Creating Tables in AnywhereTS Database");
+                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                        RunScript(Resource1.CreateTables.ToString());
+                        //sqlCon.Close();
+                    }
+                    Logging.ATSAdminLog.Debug("Created Tables in AnywhereTS Database");
                 }
                 catch (SqlException sql_ex1)
                 {
                     SqlException sql_ex = (sql_ex1.GetBaseException() as SqlException);
                     MessageBox.Show(string.Format("Failed to connect to SQL Server Instance: {0}\n", InstanceName) + sql_ex.Number.ToString() + " " + sql_ex.Message.ToString());
+
+                    using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", sql_ex.Number.ToString(), sql_ex.Message, System.Environment.NewLine, sql_ex.ToString())))
+                    {
+                        Logging.ATSAdminLog.Error("Failed to open connection to AnywhereTS Database");
+                    }
                     return bContinue;
                 }
             }
@@ -87,7 +103,8 @@
             Version databaseVersion; // The current version of the database
             int chkVer = CheckVersion(out databaseVersion);
             VersionCheck verChk = (VersionCheck)chkVer;
-            MessageBox.Show(string.Format("Version Check: {0} Version: {1}", verChk.ToString(), databaseVersion.ToString()));
+            //MessageBox.Show(string.Format("Version Check: {0} Version: {1}", verChk.ToString(), databaseVersion.ToString()));
+            Logging.ATSAdminLog.DebugFormat("Version Check: {0} Version: {1}", verChk.ToString(), databaseVersion.ToString());
             switch (chkVer)
             {
                 case (int)VersionCheck.Equal:
@@ -107,24 +124,40 @@
                             // Run the apropriate upgdrade script(s)
                             case "1.0.0.0":
                                 {   // Current database is version 1.0.0.0, update to 1.0.0.1
+                                    Logging.ATSAdminLog.DebugFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.0", "1.0.0.1");
                                     bContinue = RunScript(Resource1.UpdateDatabase1.ToString());
+                                    Logging.ATSAdminLog.DebugFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.0", "1.0.0.1");
                                     goto case "1.0.0.1"; // Continue and upgrade one more step
                                 }
                             case "1.0.0.1":
                                 {   // Current database is version 1.0.0.1, update to 1.0.0.2
+                                    Logging.ATSAdminLog.DebugFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.1", "1.0.0.2");
                                     bContinue = RunScript(Resource1.UpdateDatabase2.ToString());
+                                    Logging.ATSAdminLog.DebugFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.2", "1.0.0.2");
                                     goto case "1.0.0.2"; // Continue and upgrade one more step
                                 }
                             case "1.0.0.2":
                                 {   // Current database is version 1.0.0.2, update to 1.0.0.3
+                                    Logging.ATSAdminLog.DebugFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.2", "1.0.0.3");
                                     bContinue = RunScript(Resource1.UpdateDatabase3.ToString());
+                                    Logging.ATSAdminLog.DebugFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.2", "1.0.0.3");
                                     break;
-                                }
-
-                            
+                                }                            
                             default:
                                 {
-                                    MessageBox.Show("Error: Not able to upgrade database (51188)");
+                                    //MessageBox.Show("Error: Not able to upgrade database (51188)");
+                                    if (databaseVersion == new Version(0, 0, 0, 0))
+                                    {
+                                        string format = string.Format("Database version is {0}, this should have been auto upgraded to {1}", databaseVersion.ToString(), ATSGlobals.strDatabaseVersion);
+                                        Logging.ATSAdminLog.Fatal(format);
+                                        MessageBox.Show(format);
+                                    }
+                                    else
+                                    {
+                                        string format = string.Format("Failed to upgrade Database from version: {0} to version: {1}", databaseVersion.ToString(), ATSGlobals.strDatabaseVersion);
+                                        Logging.ATSAdminLog.Fatal(format);
+                                        MessageBox.Show(format);
+                                    }                                    
                                     break;
                                 }
                         }
@@ -192,58 +225,92 @@
                 string strResult;
 
                 //Verify that the AnywhereTS database exists
-                sqlCmd = new SqlCommand("select count(*) from master..sysdatabases where name='AnywhereTS'", sqlCon);
-                strResult = sqlCmd.ExecuteScalar().ToString();
 
-                if (strResult == "0")
+                using (log4net.NDC.Push(string.Format("SQL STATMENT={0}", "select count(*) from master..sysdatabases where name='AnywhereTS'")))
                 {
-
-                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
-                    sqlCon.Open();
-                    RunScript(Resource1.CreateDatabase.ToString());
-                    sqlCon.Close();
-                }
-                else
-                {
-                    sqlCon.Close();
-                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
-                    sqlCon.Open();
-                    RunScript("DROP DATABASE AnywhereTS");
-                    sqlCon.Close();
-                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
-                    sqlCon.Open();
-                    RunScript(Resource1.CreateDatabase.ToString());
-                    sqlCon.Close();
+                    Logging.ATSAdminLog.Debug("Getting Coount of AnywhereTS databases");
+                    if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                    sqlCmd = new SqlCommand("select count(*) from master..sysdatabases where name='AnywhereTS'", sqlCon);
+                    strResult = sqlCmd.ExecuteScalar().ToString();
                 }
+               Logging.ATSAdminLog.DebugFormat("AnywhereTS databases Count={0}", strResult);
 
-                try
-                {
-                    sqlCon.Close();
-                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString);
-                    sqlCmd = new SqlCommand("SELECT value from AnywhereTS..AppInfo where property='version'", sqlCon);
-                    strResult = (string)sqlCmd.ExecuteScalar();
-                }
-                catch
+                if (strResult == "0")
                 {
-                    // database exists, but does not have one or more expected tables
-                    try
+
+
+                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateDatabase.ToString())))
                     {
-                        sqlCon.Close();
+                        Logging.ATSAdminLog.Debug("Creating Database AnywhereTS in CheckVersion()");
                         sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
-                        sqlCon.Open();
-                        RunScript("DROP DATABASE AnywhereTS");
+                        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                        RunScript(Resource1.CreateDatabase.ToString());
                         sqlCon.Close();
+                    }
+                    Logging.ATSAdminLog.Debug("Created Database AnywhereTS in CheckVersion()");
+
+                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateTables.ToString())))
+                    {
+                        Logging.ATSAdminLog.Debug("Creating Tables in AnywhereTS Database in CheckVersion()");
                         sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
-                        sqlCmd.Connection = sqlCon;
-                        sqlCmd.CommandText = Resource1.CreateDatabase.ToString();
-                        sqlCmd.ExecuteNonQuery();
+                        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                        RunScript(Resource1.CreateTables.ToString());
                         sqlCon.Close();
-
+                    }
+                    Logging.ATSAdminLog.Debug("Created Tables in AnywhereTS Database in CheckVersion()");
+                }
+                try
+                {
+                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", "SELECT value from AnywhereTS..AppInfo where property='version'")))
+                    {
+                        Logging.ATSAdminLog.Debug("Gettting AnywhereTS Database Version");
                         sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString);
+                        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
                         sqlCmd = new SqlCommand("SELECT value from AnywhereTS..AppInfo where property='version'", sqlCon);
                         strResult = (string)sqlCmd.ExecuteScalar();
+                        sqlCon.Close();
+                    }
+                    Logging.ATSAdminLog.DebugFormat("AnywhereTS database version={0}", strResult);
+                }
+                catch(SqlException ex)
+                {
+                    // the database exists, but one or more tables are missing
+                    using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString())))
+                    {
+                        Logging.ATSAdminLog.Error("Failed to get database version");
+                    }
+                    try
+                    {
+
+                        using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateTables.ToString())))
+                        {
+                            Logging.ATSAdminLog.Debug("Creating Tables in AnywhereTS Database in CheckVersion()");
+                            sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                            if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                            RunScript(Resource1.CreateTables.ToString());
+                            sqlCon.Close();
+                        }
+                        Logging.ATSAdminLog.Debug("Created Tables in AnywhereTS Database in CheckVersion()");
+
+                        using (log4net.NDC.Push(string.Format("SQL Statment={0}", "SELECT value from AnywhereTS..AppInfo where property='version'")))
+                        {
+                            Logging.ATSAdminLog.Debug("Gettting AnywhereTS Database Version");
+                            sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString);
+                            if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                            sqlCmd = new SqlCommand("SELECT value from AnywhereTS..AppInfo where property='version'", sqlCon);
+                            strResult = (string)sqlCmd.ExecuteScalar();
+                            sqlCon.Close();
+                        }
+                        Logging.ATSAdminLog.DebugFormat("AnywhereTS database version={0}", strResult);
+                    }
+                    catch(SqlException ex1)
+                    {
+                        using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex1.Number.ToString(), ex1.Message, System.Environment.NewLine, ex1.ToString())))
+                        {
+                            Logging.ATSAdminLog.Error("Failed to get database version");
+                        }
+                        return (int)VersionCheck.Failed;
                     }
-                    catch { return (int)VersionCheck.Failed; }
                 }
                 vDb = new Version(strResult);
           
@@ -262,11 +329,19 @@
             catch (SqlException sql_ex)
             {
                 MessageBox.Show(sql_ex.Number.ToString() + " " + sql_ex.Message.ToString());
+                using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", sql_ex.Number.ToString(), sql_ex.Message, System.Environment.NewLine, sql_ex.ToString())))
+                {
+                    Logging.ATSAdminLog.Error("Failed to check database version");
+                }
                 return (int)VersionCheck.Failed;
             }
             catch (Exception system_ex)
             {
                 MessageBox.Show(system_ex.Message.ToString());
+                using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", system_ex.Message, System.Environment.NewLine, system_ex.ToString())))
+                {
+                    Logging.ATSAdminLog.Error("Failed to check database version");
+                }
                 return (int)VersionCheck.Failed;
             }
         }

 

  ViewVC Help
Powered by ViewVC 1.1.22