/[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:01:40	10
+++ trunk/TSAdminTool/Database.cs	2012/07/11 17:45:48	18
@@ -21,11 +21,12 @@
         private SqlCommand sqlCmd = new SqlCommand();
 
         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(@";Database")).Replace(@"\", "").Replace(@";", "").Replace(@"Database", "");
         public bool SetupDatabase()
         {
             bool bContinue = false;
-
+            bool bCreateDB = false;
             // Create a connection to SQL Server
             try
             {
@@ -34,15 +35,34 @@
             }
             catch (SqlException sql_ex)
             {
-                MessageBox.Show("Fail to connect to SQL Server Express\n" + 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.ConnectionString = Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS","master");
+                    sqlCon.Open();
+                    RunScript(Resource1.CreateDatabase.ToString());
+                    sqlCon.Close();
+                    sqlCon.ConnectionString = Properties.Settings.Default.atsConnectionString;
+                    sqlCon.Open();
+                }
+                catch (SqlException sql_ex1)
+                {
+                    MessageBox.Show(string.Format("Fail to connect to SQL Server Instance: {0}\n", InstanceName) + sql_ex1.Number.ToString() + " " + sql_ex1.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:
                     {
@@ -177,7 +197,7 @@
                     //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;                        
+                    return (int)VersionCheck.DatabaseNotFound;
                 }
                 vDb = new Version(strResult);
           
@@ -265,7 +285,7 @@
             }
             catch
             {
-                MessageBox.Show("Error, could not configure SQL Browser service (24888). Please check that SQL Server Express is intalled and that you are logged in with sufficient rights to configure services. Then retry the operation.");
+                MessageBox.Show("Error, could not configure SQL Browser service (24888). Please check that SQL Server is intalled and that you are logged in with sufficient rights to configure services. Then retry the operation.");
             }
         }
 
@@ -284,7 +304,7 @@
             {
                 prot.Get();
                 if ((string)prot.GetPropertyValue("ProtocolName") == "Np" &&  //Named pipes
-                    (string)prot.GetPropertyValue("InstanceName") == "SQLEXPRESS")
+                    (string)prot.GetPropertyValue("InstanceName") == InstanceName)
                 {   // We found the named pipes protocol
                     if (!(bool)prot.GetPropertyValue("Enabled"))
                     {   // Named pipes not activated
@@ -292,7 +312,7 @@
               
                         // Check if user wants to restart SQL server
                         DialogResult resultRights;
-                        resultRights = MessageBox.Show("In order for users to use the AnywhereTS control panel, the SQL Express service on this computer need to be restarted. This operation might take up to 60 seconds. Do you want restart the SQL Express service now?", "AnywhereTS - Restart SQL Express (This operation might take up to 60 seconds!)", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
+                        resultRights = MessageBox.Show(string.Format("In order for users to use the AnywhereTS control panel, the {0} service on this computer need to be restarted. This operation might take up to 60 seconds. Do you want restart the {0} service now?", InstanceName), string.Format("AnywhereTS - Restart {0} (This operation might take up to 60 seconds!)", InstanceName), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1);
                         if (resultRights == DialogResult.Yes)
                         {
                             // Restart the SQL server
@@ -301,7 +321,7 @@
                             foreach (ManagementObject svc in sqlServicesMan.GetInstances())
                             {
                                 if ((uint)svc.GetPropertyValue("SqlServiceType") == sqlServerService &&
-                                    (string)svc.GetPropertyValue("ServiceName") == "MSSQL$SQLEXPRESS")
+                                    (string)svc.GetPropertyValue("ServiceName") == string.Format("MSSQL${0}", InstanceName))
                                 {
                                     svc.Get();
                                     if ((uint)svc.GetPropertyValue("State") != sqlServiceStopped)

 

  ViewVC Help
Powered by ViewVC 1.1.22