/[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:07:00	19
+++ trunk/TSAdminTool/Database.cs	2012/07/12 10:52:05	33
@@ -6,15 +6,21 @@
 using System.Windows.Forms;
 using System.Text.RegularExpressions;
 using System.Management;
+using log4net;
 
 namespace AnywhereTS
 {
     class DatabaseSupport
     {
+        private readonly ILog logger = LogManager.GetLogger("AnywhereTS.ATSAmdinLogger"); 
         enum VersionCheck
         {
-            Failed = 0, Equal, DatabaseIsMoreNew,
-            DatabaseIsOlder, DatabaseNotFound
+            Failed = 0, 
+            Equal, 
+            DatabaseIsMoreNew,
+            DatabaseIsOlder, 
+            //DatabaseNotFound,
+            DatabaseCreated,
         };
 
         private SqlConnection sqlCon = new SqlConnection();
@@ -43,6 +49,7 @@
         {
             bool bContinue = false;
             // Create a connection to SQL Server
+            logger.Debug("SetupDatabase() called ");
             try
             {
                 sqlCon.ConnectionString = Properties.Settings.Default.atsConnectionString;
@@ -55,12 +62,16 @@
                 //bCreateDB = true;
                 try
                 {
-                    sqlCon.ConnectionString = Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master");
+                    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 = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
                     sqlCon.Open();
+                    RunScript(Resource1.CreateTables.ToString());
+                    sqlCon.Close();
+                    //sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString);
+                    //sqlCon.Open();
                 }
                 catch (SqlException sql_ex1)
                 {
@@ -74,7 +85,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 +133,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 +192,45 @@
                 //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();
+                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                    sqlCon.Open();
+                    RunScript(Resource1.CreateTables.ToString());
+                    sqlCon.Close();
                 }
-
                 try
                 {
+                    sqlCon.Close();
+                    sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString);
+                    sqlCon.Open();
                     sqlCmd = new SqlCommand("SELECT value from AnywhereTS..AppInfo where property='version'", sqlCon);
                     strResult = (string)sqlCmd.ExecuteScalar();
+                    sqlCon.Close();
                 }
                 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;
+                {
+                    // the database exists, but one or more tables are missing
+                    try
+                    {
+                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS", "master"));
+                        sqlCon.Open();
+                        RunScript(Resource1.CreateTables.ToString());
+                        sqlCon.Close();
+                        sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString);
+                        sqlCon.Open();
+                        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 +265,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