/[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 16:48:23	17
+++ 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();
@@ -22,28 +28,65 @@
 
         const string SQL_BROWSER_SERVICE_NAME = "SQLBrowser";
 
-        public static string InstanceName = Properties.Settings.Default.atsConnectionString.Substring(Properties.Settings.Default.atsConnectionString.IndexOf(@"\"), Properties.Settings.Default.atsConnectionString.IndexOf(@";")).Replace(@"\", "").Replace(@";", "");
+        public static string InstanceName
+        {
+            get
+            {
+                bool start = false;
+                bool end = false;
+                List<char> pChars = new List<char>();
+                foreach (char c in Properties.Settings.Default.atsConnectionString.ToCharArray())
+                {
+                    if (c == '\\') { start = true; continue; }
+                    if (c == ';') { end = true; }
+                    if (end) break;
+                    if (start) { pChars.Add(c); }
+                }
+                return new string(pChars.ToArray());
+            }
+        }
         public bool SetupDatabase()
         {
             bool bContinue = false;
-
             // Create a connection to SQL Server
+            logger.Debug("SetupDatabase() called ");
             try
             {
                 sqlCon.ConnectionString = Properties.Settings.Default.atsConnectionString;
                 sqlCon.Open();
             }
-            catch (SqlException sql_ex)
+            catch
             {
-                MessageBox.Show(string.Format("Fail to connect to SQL Server Instance: {0}\n", InstanceName) + sql_ex.Number.ToString() + " " + sql_ex.Message.ToString());
-                return bContinue;
+                //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
+                {
+                    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();
+                    //sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString);
+                    //sqlCon.Open();
+                }
+                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());
+                    return bContinue;
+                }
             }
          
             // Now that you are connected to Express, check the database versions
-
             Version databaseVersion; // The current version of the database
-
-            switch (CheckVersion(out databaseVersion))
+            int chkVer = CheckVersion(out databaseVersion);
+            VersionCheck verChk = (VersionCheck)chkVer;
+            MessageBox.Show(string.Format("Version Check: {0} Version: {1}", verChk.ToString(), databaseVersion.ToString()));
+            switch (chkVer)
             {
                 case (int)VersionCheck.Equal:
                     {
@@ -90,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;
@@ -161,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);
           
@@ -213,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