/[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/12 16:21:35	51
+++ trunk/TSAdminTool/Database.cs	2012/07/13 00:43:28	74
@@ -7,6 +7,7 @@
 using System.Text.RegularExpressions;
 using System.Management;
 using log4net;
+using System.Security.Principal;
 
 namespace AnywhereTS
 {
@@ -55,7 +56,7 @@
                 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");
+                    Logging.ATSAdminLog.Debug(string.Format("Opening connection to {0} Database", ATSGlobals.strDatabaseName));
                 }
                 if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
             }
@@ -66,25 +67,26 @@
                 //bCreateDB = true;
                 try
                 {
-                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateDatabase.ToString())))
+                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateDatabase.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName))))
                     {
-                        Logging.ATSAdminLog.Debug("Creating Database AnywhereTS");
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                        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(Resource1.CreateDatabase.ToString());
+                        RunScript(Resource1.CreateDatabase.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName));
+                        //RunScript(CreateDatabaseUserScript());
                         sqlCon.Close();
                     }
-                    Logging.ATSAdminLog.Debug("Created Database AnywhereTS");
+                    Logging.ATSAdminLog.Debug(string.Format("Created Database {0}", ATSGlobals.strDatabaseName));
 
-                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateTables.ToString())))
+                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateTables.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName))))
                     {
-                        Logging.ATSAdminLog.Debug("Creating Tables in AnywhereTS Database");
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                        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(Resource1.CreateTables.ToString());
+                        RunScript(Resource1.CreateTables.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName));
                         //sqlCon.Close();
                     }
-                    Logging.ATSAdminLog.Debug("Created Tables in AnywhereTS Database");
+                    Logging.ATSAdminLog.Debug(string.Format("Created Tables in {0} Database", ATSGlobals.strDatabaseName));
                 }
                 catch (SqlException sql_ex1)
                 {
@@ -93,7 +95,7 @@
 
                     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");
+                        Logging.ATSAdminLog.Error(string.Format("Failed to open connection to {0} Database", ATSGlobals.strDatabaseName));
                     }
                     return bContinue;
                 }
@@ -125,24 +127,31 @@
                             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());
+                                    bContinue = RunScript(Resource1.UpdateDatabase1.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName));
                                     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());
+                                    bContinue = RunScript(Resource1.UpdateDatabase2.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName));
                                     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());
+                                    bContinue = RunScript(Resource1.UpdateDatabase3.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName));
                                     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(Resource1.UpdateDatabase4.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName));
+                                    Logging.ATSAdminLog.DebugFormat("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)");
@@ -215,8 +224,46 @@
 
             return true;
         }
-        
-        
+
+
+        private string CreateDatabaseUserScript()
+        {
+            //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;
+        }
+
         // Check the version of the datbase
         public int CheckVersion(out Version vDb)
         {
@@ -230,51 +277,52 @@
 
                 //Verify that the AnywhereTS database exists
 
-                using (log4net.NDC.Push(string.Format("SQL STATMENT={0}", "select count(*) from master..sysdatabases where name='AnywhereTS'")))
+                using (log4net.NDC.Push(string.Format("SQL STATMENT={0}", "select count(*) from master..sysdatabases where name='{0}'", ATSGlobals.strDatabaseName)))
                 {
-                    Logging.ATSAdminLog.Debug("Getting Coount of AnywhereTS databases");
+                    Logging.ATSAdminLog.Debug(string.Format("Getting Coount of {0} databases", ATSGlobals.strDatabaseName));
                     if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
-                    sqlCmd = new SqlCommand("select count(*) from master..sysdatabases where name='AnywhereTS'", sqlCon);
+                    sqlCmd = new SqlCommand(string.Format("select count(*) from master..sysdatabases where name='{0}'", ATSGlobals.strDatabaseName), sqlCon);
                     strResult = sqlCmd.ExecuteScalar().ToString();
                 }
-               Logging.ATSAdminLog.DebugFormat("AnywhereTS databases Count={0}", strResult);
+                Logging.ATSAdminLog.DebugFormat(string.Format("{0} databases Count={1}", ATSGlobals.strDatabaseName, strResult));
 
                 if (strResult == "0")
                 {
 
 
-                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateDatabase.ToString())))
+                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateDatabase.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName))))
                     {
-                        Logging.ATSAdminLog.Debug("Creating Database AnywhereTS in CheckVersion()");
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                        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(Resource1.CreateDatabase.ToString());
+                        RunScript(Resource1.CreateDatabase.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName));
+                        //RunScript(CreateDatabaseUserScript());
                         sqlCon.Close();
                     }
-                    Logging.ATSAdminLog.Debug("Created Database AnywhereTS in CheckVersion()");
+                    Logging.ATSAdminLog.Debug(string.Format("Created Database {0} in CheckVersion()",ATSGlobals.strDatabaseName));
 
-                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateTables.ToString())))
+                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateTables.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName))))
                     {
-                        Logging.ATSAdminLog.Debug("Creating Tables in AnywhereTS Database in CheckVersion()");
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                        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(Resource1.CreateTables.ToString());
+                        RunScript(Resource1.CreateTables.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName));
                         sqlCon.Close();
                     }
-                    Logging.ATSAdminLog.Debug("Created Tables in AnywhereTS Database in CheckVersion()");
+                    Logging.ATSAdminLog.Debug(string.Format("Created Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
                 }
                 try
                 {
-                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", "SELECT value from AnywhereTS..AppInfo where property='version'")))
+                    using (log4net.NDC.Push(string.Format("SQL Statment={0}", "SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName)))
                     {
-                        Logging.ATSAdminLog.Debug("Getting AnywhereTS Database Version#1");
-                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                        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("SELECT value from AnywhereTS..AppInfo where property='version'", sqlCon);
+                        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("AnywhereTS database version={0}", strResult);
+                    Logging.ATSAdminLog.DebugFormat("{0} database version={1}",ATSGlobals.strDatabaseName, strResult);
                 }
                 catch(SqlException ex)
                 {
@@ -286,26 +334,26 @@
                     try
                     {
 
-                        using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateTables.ToString())))
+                        using (log4net.NDC.Push(string.Format("SQL Statment={0}", Resource1.CreateTables.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName))))
                         {
-                            Logging.ATSAdminLog.Debug("Creating Tables in AnywhereTS Database in CheckVersion()");
-                            sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                            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(Resource1.CreateTables.ToString());
+                            RunScript(Resource1.CreateTables.ToString().Replace("AnywhereTS", ATSGlobals.strDatabaseName));
                             sqlCon.Close();
                         }
-                        Logging.ATSAdminLog.Debug("Created Tables in AnywhereTS Database in CheckVersion()");
+                        Logging.ATSAdminLog.Debug(string.Format("Created Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
 
-                        using (log4net.NDC.Push(string.Format("SQL Statment={0}", "SELECT value from AnywhereTS..AppInfo where property='version'")))
+                        using (log4net.NDC.Push(string.Format("SQL Statment={0}", "SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName)))
                         {
-                            Logging.ATSAdminLog.Debug("Getting AnywhereTS Database Version#2");
-                            sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                            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("SELECT value from AnywhereTS..AppInfo where property='version'", sqlCon);
+                            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("AnywhereTS database version={0}", strResult);
+                        Logging.ATSAdminLog.DebugFormat("{0} database version={1}",ATSGlobals.strDatabaseName, strResult);
                     }
                     catch(SqlException ex1)
                     {

 

  ViewVC Help
Powered by ViewVC 1.1.22