/[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 18:48:42	20
+++ trunk/TSAdminTool/Database.cs	2012/07/11 19:34:51	21
@@ -13,8 +13,12 @@
     {
         enum VersionCheck
         {
-            Failed = 0, Equal, DatabaseIsMoreNew,
-            DatabaseIsOlder, DatabaseNotFound
+            Failed = 0, 
+            Equal, 
+            DatabaseIsMoreNew,
+            DatabaseIsOlder, 
+            //DatabaseNotFound,
+            DatabaseCreated,
         };
 
         private SqlConnection sqlCon = new SqlConnection();
@@ -55,7 +59,16 @@
                 //bCreateDB = true;
                 try
                 {
-                    sqlCon.ConnectionString = Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master");
+                    try
+                    {
+                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString);
+                        sqlCon.Open();
+                        RunScript("DROP AnywhereTS");
+                        sqlCon.Close();
+                    }
+                    catch { }
+
+                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
                     sqlCon.Open();
                     RunScript(Resource1.CreateDatabase.ToString());
                     sqlCon.Close();
@@ -74,7 +87,7 @@
             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()));
             switch (chkVer)
             {
                 case (int)VersionCheck.Equal:
@@ -122,18 +135,6 @@
                         bContinue = false;
                         break;
                     }
-                case (int)VersionCheck.DatabaseNotFound:
-                    {
-                        //Run the creation script
-                        bContinue = RunScript(Resource1.CreateDatabase.ToString());
-                        if (bContinue)
-                        {
-                            // Set up file access rights for remote desktop users
-                            SetDatabaseRights(); 
-                        }
-
-                        break;
-                    }
                 default:
                     {
                         bContinue = false;
@@ -193,24 +194,56 @@
                 //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")
                 {
 
-                    return (int)VersionCheck.DatabaseNotFound;
+                    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();
                 }
 
                 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
-                {   // Assume we could not open the physical file 
-                    //Drop the database
-                    sqlCmd = new SqlCommand("IF EXISTS(SELECT * FROM sysdatabases WHERE name='AnywhereTS')DROP DATABASE AnywhereTS", sqlCon);
-                    strResult = (string)sqlCmd.ExecuteScalar();
-                    return (int)VersionCheck.DatabaseNotFound;
+                {
+                    // database exists, but does not have one or more expected tables
+                    try
+                    {
+                        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"));
+                        sqlCmd.Connection = sqlCon;
+                        sqlCmd.CommandText = Resource1.CreateDatabase.ToString();
+                        sqlCmd.ExecuteNonQuery();
+                        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 { return (int)VersionCheck.Failed; }
                 }
                 vDb = new Version(strResult);
           
@@ -245,7 +278,8 @@
             return commands;
         }
 
-     
+
+        [Obsolete("SetDatabaseRights() has been deperecated - user rights are assigned via SQL Server")]
         public static void SetDatabaseRights()
         {
             try

 

  ViewVC Help
Powered by ViewVC 1.1.22