/[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/13 02:21:43	78
+++ trunk/TSAdminTool/Database.cs	2012/07/13 22:37:40	87
@@ -8,6 +8,7 @@
 using System.Management;
 using log4net;
 using System.Security.Principal;
+using AnywhereTS.DBSupport;
 
 namespace AnywhereTS
 {
@@ -47,66 +48,62 @@
         }
         public bool SetupDatabase()
         {
+            Exception ErrorInfo = null;
             bool bContinue = false;
             // Create a connection to SQL Server
             
             Logging.ATSAdminLog.Debug("SetupDatabase() called ");
-            try
-            {                
-                sqlCon.ConnectionString = Properties.Settings.Default.atsConnectionString;                
-                using (log4net.NDC.Push(string.Format("ConnectionString={0}", Properties.Settings.Default.atsConnectionString)))
-                {
-                    Logging.ATSAdminLog.Debug(string.Format("Opening connection to {0} Database", ATSGlobals.strDatabaseName));
-                }
-                if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-            }
-            catch
+            #region Database Creation support
+            using (MsSqlConnector conn = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, ATSGlobals.strDatabaseName))
             {
-                //MessageBox.Show(string.Format("Fail to connect to SQL Server Instance: {0}\n", InstanceName) + sql_ex.Number.ToString() + " " + sql_ex.Message.ToString());
-                //return bContinue;
-                //bCreateDB = true;
                 try
                 {
-                    //using (log4net.NDC.Push(string.Format("SQL Statment={0}", SQLServerResources.CreateDatabase)))
-                    //{
-                        Logging.ATSAdminLog.Debug(string.Format("Creating Database {0}", ATSGlobals.strDatabaseName));
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
-                        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-                        RunScript(SQLServerResources.CreateDatabase);
-                        //RunScript(CreateDatabaseUserScript());
-                        sqlCon.Close();
-                    //}
-                    Logging.ATSAdminLog.Debug(string.Format("Created Database {0}", ATSGlobals.strDatabaseName));
-
-                    //using (log4net.NDC.Push(string.Format("SQL Statment={0}", SQLServerResources.CreateTables)))
-                    //{
-                        Logging.ATSAdminLog.Debug(string.Format("Creating Tables in {0} Database", ATSGlobals.strDatabaseName));
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
-                        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-                        RunScript(SQLServerResources.CreateTables);
-                        //sqlCon.Close();
-                    //}
-                    Logging.ATSAdminLog.Debug(string.Format("Created Tables in {0} Database", ATSGlobals.strDatabaseName));
+                    conn.CreateConnection(out ErrorInfo);
+                    conn.OpenConnection(out ErrorInfo);
                 }
-                catch (SqlException sql_ex1)
+                catch (Exception ex) 
                 {
-                    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())))
+                    try
                     {
-                        Logging.ATSAdminLog.Error(string.Format("Failed to open connection to {0} Database", ATSGlobals.strDatabaseName));
+                        conn.Dispose();
+                        using (MsSqlConnector conn1 = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, "master"))
+                        {
+                            try
+                            {
+                                conn1.CreateConnection(out ErrorInfo);
+                                conn1.OpenConnection(out ErrorInfo);
+
+                                // create datagbase
+                                Logging.ATSAdminLog.Info(string.Format("Creating Database {0}", ATSGlobals.strDatabaseName));
+                                RunScript(SQLServerResources.CreateDatabase, conn1, out ErrorInfo);
+                                Logging.ATSAdminLog.Info(string.Format("Created Database {0}", ATSGlobals.strDatabaseName));
+
+                                // create tables
+                                Logging.ATSAdminLog.Info(string.Format("Creating Tables in {0} Database", ATSGlobals.strDatabaseName));
+                                RunScript(SQLServerResources.CreateTables, conn1, out ErrorInfo);
+                                Logging.ATSAdminLog.Info(string.Format("Created Tables in {0} Database", ATSGlobals.strDatabaseName));
+                            }
+                            catch (Exception ex1)
+                            {
+                                if (ErrorInfo == null) { ErrorInfo = new Exception(ex1.Message, ex); }
+                            }
+                        }
+                    }
+                    catch (Exception ex1)
+                    {
+                        if (ErrorInfo == null) { ErrorInfo = new Exception(ex1.Message, ex); }
                     }
-                    return bContinue;
                 }
             }
-         
+            if (ErrorInfo != null) { return false; }
+            #endregion
+            #region Database version check
             // Now that you are connected to Express, check the database versions
             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()));
-            Logging.ATSAdminLog.DebugFormat("Version Check: {0} Version: {1}", verChk.ToString(), databaseVersion.ToString());
+            Logging.ATSAdminLog.InfoFormat("Database Version Check: {0} .... Database Version: {1}", verChk.ToString(), databaseVersion.ToString());
             switch (chkVer)
             {
                 case (int)VersionCheck.Equal:
@@ -121,54 +118,68 @@
                     }
                 case (int)VersionCheck.DatabaseIsOlder:
                     {
-                        switch (databaseVersion.ToString())
+                        using (MsSqlConnector conn = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, ATSGlobals.strDatabaseName))
                         {
-                            // 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(SQLServerResources.UpdateDatabase1);
-                                    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(SQLServerResources.UpdateDatabase2);
-                                    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(SQLServerResources.UpdateDatabase3);
-                                    Logging.ATSAdminLog.DebugFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.2", "1.0.0.3");
-                                    goto case "1.0.0.3"; // Continue and upgrade one more step
-                                }
-                            case "1.0.0.3":
-                                {   // Current database is version 1.0.0.3, update to 1.0.0.4
-                                    Logging.ATSAdminLog.DebugFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.3", "1.0.0.4");
-                                    bContinue = RunScript(SQLServerResources.UpdateDatabase4);
-                                    Logging.ATSAdminLog.DebugFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.4", "1.0.0.4");
-                                    break;
-                                } 
-                            default:
+                            try
+                            {
+                                conn.CreateConnection(out ErrorInfo);
+                                conn.OpenConnection(out ErrorInfo);
+                                #region database upggrade scripts
+                                switch (databaseVersion.ToString())
                                 {
-                                    //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;
+                                    // 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.InfoFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.0", "1.0.0.1");
+                                            bContinue = RunScript(SQLServerResources.UpdateDatabase1, conn, out ErrorInfo);
+                                            Logging.ATSAdminLog.InfoFormat("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.InfoFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.1", "1.0.0.2");
+                                            bContinue = RunScript(SQLServerResources.UpdateDatabase2, conn, out ErrorInfo);
+                                            Logging.ATSAdminLog.InfoFormat("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.InfoFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.2", "1.0.0.3");
+                                            bContinue = RunScript(SQLServerResources.UpdateDatabase3, conn, out ErrorInfo);
+                                            Logging.ATSAdminLog.InfoFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.2", "1.0.0.3");
+                                            goto case "1.0.0.3"; // Continue and upgrade one more step
+                                        }
+                                    case "1.0.0.3":
+                                        {   // Current database is version 1.0.0.3, update to 1.0.0.4
+                                            Logging.ATSAdminLog.InfoFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.3", "1.0.0.4");
+                                            bContinue = RunScript(SQLServerResources.UpdateDatabase4, conn, out ErrorInfo);
+                                            Logging.ATSAdminLog.InfoFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.4", "1.0.0.4");
+                                            break;
+                                        }
+                                    default:
+                                        {
+                                            //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;
+                                        }
                                 }
+#endregion
+                            }
+                            catch (Exception ex)
+                            {
+                                if (ErrorInfo == null) { ErrorInfo = ex; }
+                            }                            
                         }
                         break;
                     }
@@ -184,187 +195,200 @@
                     }
 
             }
+            #endregion
             sqlCon.Close();
             sqlCon.Dispose();
             sqlCmd.Connection.Close();
             sqlCmd.Connection.Dispose(); 
             return bContinue;
         }
-        
+
         // Run a SQL script (to create or update a database)
+        [Obsolete("RunScript(string strFile) is being replaced by RunScript(string strFile, MsSqlConnector con, out Exception ErrorInfo)", false)]
         public bool RunScript(string strFile)
         {
-            string[] strCommands;
-            strCommands = ParseScriptToCommands(strFile);
-            try
-            {
-                if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-                
-                sqlCmd.Connection = sqlCon;
-
-                foreach (string strCmd in strCommands)
-                {
-                    if (strCmd.Length > 0)
-                    {
-                        // Substitute database directory with the decided one.
-                        sqlCmd.CommandText = strCmd.Replace("[DataDir]",ProSupport.strDatabasePath);
-                        sqlCmd.ExecuteNonQuery();
-                    }
-                }
-            }
-            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 RunScript");
-                }
-                return false;
-            }
-
-            return true;
+            return false;
         }
-
-
-        private string CreateDatabaseUserScript()
+        public bool RunScript(string strFile, MsSqlConnector con, out Exception ErrorInfo)
         {
-            //Logging.ATSAdminLog.Debug("CreateDatabaseUserScript() called");
-            //Logging.ATSAdminLog.Debug("Creating Database User Update Script");
-            //StringBuilder builder = new StringBuilder();
-            /*             
-                -- Create user windows Authentication
-                CREATE LOGIN [YourDomainName\JohnJacobs] FROM WINDOWS
-                WITH DEFAULT_DATABASE = [YourDatabaseHere];
-                GO
-                -- Now add user to database
-                USE YourDatabaseHere;
-                CREATE USER JohnJacobs FOR LOGIN [YourDomainName\JohnJacobs];
-             */
-            //builder.AppendLine("-- Create user windows Authentication");
-            ////builder.AppendLine(string.Format("CREATE LOGIN [{0}] FROM WINDOWS", GetImpersonatedUser()));
-            ////builder.AppendLine(string.Format("WITH DEFAULT_DATABASE = [{0}];", "AnywhereTS"));
-            //builder.AppendLine("GO");
-            //builder.AppendLine(string.Format(" -- Now add user to database"));
-            //builder.AppendLine(string.Format(" USE {0};", "AnywhereTS"));
-            //builder.AppendLine(string.Format(" CREATE USER [{0}] FOR LOGIN [{0}]",WindowsIdentity.GetCurrent().Name));
-            // WindowsIdentity.GetCurrent().Name.Substring(WindowsIdentity.GetCurrent().Name.LastIndexOf(@"\")+1)
-            //builder.AppendLine("GO");
-            //builder.AppendLine(string.Format(" USE {0};", "AnywhereTS"));
-            //builder.AppendLine(string.Format("exec sp_changedbowner [{0}]", WindowsIdentity.GetCurrent().Name));
-            //builder.AppendLine(string.Format("exec sp_changeobjectowner [{0}]", WindowsIdentity.GetCurrent().Name));
-            //using (log4net.NDC.Push(string.Format("CreateDatabaseUserScript() called")))
-            //{
-            //    using (log4net.NDC.Push(string.Format("script={0}", builder.ToString())))
-            //    {
-            //        Logging.ATSAdminLog.Debug("Created Database User Update Script");
-            //    }
-            //}
-            //return builder.ToString();
-
-            return string.Empty;
+            ErrorInfo = null;
+            string cmd = strFile.Replace("[DataDir]", ProSupport.strDatabasePath);
+            con.RunScript(cmd, out ErrorInfo);
+            return false;
         }
-
         // Check the version of the datbase
         public int CheckVersion(out Version vDb)
         {
+            Exception ErrorInfo = null;
             //Get Version information from application
             Version v=new Version(ATSGlobals.strDatabaseVersion);
             vDb = new Version("0.0.0.0"); // Assign a default value for version
             try
             {
 
-                string strResult;
+                //string strResult;
 
+                int db_count = -1;
                 //Verify that the AnywhereTS database exists
+                #region Get Database Count
+                using (MsSqlConnector conn = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, ATSGlobals.strDatabaseName))
+                {
+                    try
+                    {
+                        conn.CreateConnection(out ErrorInfo);
+                        conn.OpenConnection(out ErrorInfo);
+
+                        //Logging.ATSAdminLog.Debug(string.Format("Getting Coount of {0} databases", ATSGlobals.strDatabaseName));
 
-                using (log4net.NDC.Push(string.Format("SQL STATMENT={0}", string.Format("select count(*) from master..sysdatabases where name='{0}'", ATSGlobals.strDatabaseName))))
+                        SqlCommand cmd = conn.CreateCommandInstance(string.Format("select count(*) from master..sysdatabases where name='{0}'", ATSGlobals.strDatabaseName), new List<SqlParameter>(), out ErrorInfo);
+                        db_count = Convert.ToInt32(cmd.ExecuteScalar());
+
+                        Logging.ATSAdminLog.Info(string.Format("Found {0} databases named {1}", db_count, ATSGlobals.strDatabaseName));
+                    }
+                    catch (Exception ex)
+                    {
+                        if (ErrorInfo == null)
+                        {
+                            ErrorInfo = ex;
+                            using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
+                            {
+                                Logging.DatabaseLog.Error(string.Format("Failed to get count of databases named: {0}{1}", System.Environment.NewLine, ATSGlobals.strDatabaseName));
+                            }
+                        }
+                        else
+                        {
+                            ErrorInfo = ex.GetBaseException();
+                            using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ErrorInfo.GetType().Name, ErrorInfo.Message, System.Environment.NewLine, ErrorInfo.ToString())))
+                            {
+                                Logging.DatabaseLog.Error(string.Format("Failed to get count of databases named: {0}{1}", System.Environment.NewLine, ATSGlobals.strDatabaseName));
+                            }
+                        }
+                    }
+                }
+                #endregion
+                if (db_count == -1)
                 {
-                    Logging.ATSAdminLog.Debug(string.Format("Getting Coount of {0} databases", ATSGlobals.strDatabaseName));
-                    if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-                    sqlCmd = new SqlCommand(string.Format("select count(*) from master..sysdatabases where name='{0}'", ATSGlobals.strDatabaseName), sqlCon);
-                    strResult = sqlCmd.ExecuteScalar().ToString();
+                    return (int)VersionCheck.Failed;
                 }
-                Logging.ATSAdminLog.DebugFormat(string.Format("{0} databases Count={1}", ATSGlobals.strDatabaseName, strResult));
-
-                if (strResult == "0")
+                else if (db_count == 0)
                 {
 
-
+                    #region database does not exist
                     //using (log4net.NDC.Push(string.Format("SQL Statment={0}", SQLServerResources.CreateDatabase)))
                     //{
-                        Logging.ATSAdminLog.Debug(string.Format("Creating Database {0} in CheckVersion()", ATSGlobals.strDatabaseName));
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
-                        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-                        RunScript(SQLServerResources.CreateDatabase);
-                        //RunScript(CreateDatabaseUserScript());
-                        sqlCon.Close();
+                    Logging.ATSAdminLog.Debug(string.Format("Creating Database {0} in CheckVersion()", ATSGlobals.strDatabaseName));
+                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
+                    if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                    RunScript(SQLServerResources.CreateDatabase);
+                    //RunScript(CreateDatabaseUserScript());
+                    sqlCon.Close();
                     //}
-                    Logging.ATSAdminLog.Debug(string.Format("Created Database {0} in CheckVersion()",ATSGlobals.strDatabaseName));
+                    Logging.ATSAdminLog.Debug(string.Format("Created Database {0} in CheckVersion()", ATSGlobals.strDatabaseName));
 
                     //using (log4net.NDC.Push(string.Format("SQL Statment={0}", SQLServerResources.CreateTables)))
                     //{
-                        Logging.ATSAdminLog.Debug(string.Format("Creating Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
-                        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-                        RunScript(SQLServerResources.CreateTables);
-                        sqlCon.Close();
+                    Logging.ATSAdminLog.Debug(string.Format("Creating Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
+                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
+                    if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                    RunScript(SQLServerResources.CreateTables);
+                    sqlCon.Close();
                     //}
                     Logging.ATSAdminLog.Debug(string.Format("Created Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
+                    #endregion
                 }
-                try
-                {
-                    //using (log4net.NDC.Push(string.Format("SQL Statment={0}", "SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName)))
-                    //{
-                        Logging.ATSAdminLog.Debug(string.Format("Getting {0} Database Version#1", ATSGlobals.strDatabaseName));
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
-                        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-                        sqlCmd = new SqlCommand(string.Format("SELECT value from {0}..AppInfo where property='version'",ATSGlobals.strDatabaseName), sqlCon);
-                        strResult = (string)sqlCmd.ExecuteScalar();
-                        sqlCon.Close();
-                    //}
-                    Logging.ATSAdminLog.DebugFormat("{0} database version={1}",ATSGlobals.strDatabaseName, strResult);
-                }
-                catch(SqlException ex)
+
+                
+                #region Get Database Version
+                using (MsSqlConnector conn = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, ATSGlobals.strDatabaseName))
                 {
-                    // 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
                     {
+                        conn.CreateConnection(out ErrorInfo);
+                        conn.OpenConnection(out ErrorInfo);
 
-                        //using (log4net.NDC.Push(string.Format("SQL Statment={0}", SQLServerResources.CreateTables)))
-                        //{
-                            Logging.ATSAdminLog.Debug(string.Format("Creating Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
-                            sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
-                            if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-                            RunScript(SQLServerResources.CreateTables);
-                            sqlCon.Close();
-                        //}
-                        Logging.ATSAdminLog.Debug(string.Format("Created Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
+                        //Logging.ATSAdminLog.Debug(string.Format("Getting Coount of {0} databases", ATSGlobals.strDatabaseName));
 
-                        using (log4net.NDC.Push(string.Format("SQL Statment={0}", string.Format("SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName))))
-                        {
-                            Logging.ATSAdminLog.Debug(string.Format("Getting {0} Database Version#2", ATSGlobals.strDatabaseName));
-                            sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
-                            if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-                            sqlCmd = new SqlCommand(string.Format("SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName), sqlCon);
-                            strResult = (string)sqlCmd.ExecuteScalar();
-                            sqlCon.Close();
-                        }
-                        Logging.ATSAdminLog.DebugFormat("{0} database version={1}",ATSGlobals.strDatabaseName, strResult);
+                        SqlCommand cmd = conn.CreateCommandInstance(string.Format("SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName), new List<SqlParameter>(), out ErrorInfo);
+                        string version = (string)cmd.ExecuteScalar();
+                        vDb = new Version(version);
+                        Logging.ATSAdminLog.Info(string.Format("Database {0} is at Version: {1}", ATSGlobals.strDatabaseName, version));
                     }
-                    catch(SqlException ex1)
+                    catch (Exception ex)
                     {
-                        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())))
+                        if (ErrorInfo == null)
                         {
-                            Logging.ATSAdminLog.Error("Failed to get database version");
+                            ErrorInfo = ex;
+                            using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
+                            {
+                                Logging.DatabaseLog.Error(string.Format("Failed to get count of databases named: {0}{1}", System.Environment.NewLine, ATSGlobals.strDatabaseName));
+                            }
+                        }
+                        else
+                        {
+                            ErrorInfo = ex.GetBaseException();
+                            using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ErrorInfo.GetType().Name, ErrorInfo.Message, System.Environment.NewLine, ErrorInfo.ToString())))
+                            {
+                                Logging.DatabaseLog.Error(string.Format("Failed to get count of databases named: {0}{1}", System.Environment.NewLine, ATSGlobals.strDatabaseName));
+                            }
                         }
-                        return (int)VersionCheck.Failed;
                     }
                 }
-                vDb = new Version(strResult);
+                #endregion
+
+                #region old-code might still need
+                //try
+                //{
+                //    //using (log4net.NDC.Push(string.Format("SQL Statment={0}", "SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName)))
+                //    //{
+                //        Logging.ATSAdminLog.Debug(string.Format("Getting {0} Database Version#1", ATSGlobals.strDatabaseName));
+                //        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
+                //        if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                //        sqlCmd = new SqlCommand(string.Format("SELECT value from {0}..AppInfo where property='version'",ATSGlobals.strDatabaseName), sqlCon);
+                //        strResult = (string)sqlCmd.ExecuteScalar();
+                //        sqlCon.Close();
+                //    //}
+                //    Logging.ATSAdminLog.DebugFormat("{0} database version={1}",ATSGlobals.strDatabaseName, 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}", SQLServerResources.CreateTables)))
+                //        //{
+                //            Logging.ATSAdminLog.Debug(string.Format("Creating Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
+                //            sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
+                //            if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                //            RunScript(SQLServerResources.CreateTables);
+                //            sqlCon.Close();
+                //        //}
+                //        Logging.ATSAdminLog.Debug(string.Format("Created Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
+                //        using (log4net.NDC.Push(string.Format("SQL Statment={0}", string.Format("SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName))))
+                //        {
+                //            Logging.ATSAdminLog.Debug(string.Format("Getting {0} Database Version#2", ATSGlobals.strDatabaseName));
+                //            sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
+                //            if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
+                //            sqlCmd = new SqlCommand(string.Format("SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName), sqlCon);
+                //            strResult = (string)sqlCmd.ExecuteScalar();
+                //            sqlCon.Close();
+                //        }
+                //        Logging.ATSAdminLog.DebugFormat("{0} database version={1}",ATSGlobals.strDatabaseName, 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;
+                //    }
+                //}
+                #endregion
+                
           
                 sqlCon.Close();
 

 

  ViewVC Help
Powered by ViewVC 1.1.22