/[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

revision 10 by william, Wed Jul 11 16:01:40 2012 UTC revision 18 by william, Wed Jul 11 17:45:48 2012 UTC
# Line 21  namespace AnywhereTS Line 21  namespace AnywhereTS
21          private SqlCommand sqlCmd = new SqlCommand();          private SqlCommand sqlCmd = new SqlCommand();
22    
23          const string SQL_BROWSER_SERVICE_NAME = "SQLBrowser";          const string SQL_BROWSER_SERVICE_NAME = "SQLBrowser";
24            
25            public static string InstanceName = Properties.Settings.Default.atsConnectionString.Substring(Properties.Settings.Default.atsConnectionString.IndexOf(@"\"), Properties.Settings.Default.atsConnectionString.IndexOf(@";Database")).Replace(@"\", "").Replace(@";", "").Replace(@"Database", "");
26          public bool SetupDatabase()          public bool SetupDatabase()
27          {          {
28              bool bContinue = false;              bool bContinue = false;
29                bool bCreateDB = false;
30              // Create a connection to SQL Server              // Create a connection to SQL Server
31              try              try
32              {              {
# Line 34  namespace AnywhereTS Line 35  namespace AnywhereTS
35              }              }
36              catch (SqlException sql_ex)              catch (SqlException sql_ex)
37              {              {
38                  MessageBox.Show("Fail to connect to SQL Server Express\n" + sql_ex.Number.ToString() + " " + sql_ex.Message.ToString());                  //MessageBox.Show(string.Format("Fail to connect to SQL Server Instance: {0}\n", InstanceName) + sql_ex.Number.ToString() + " " + sql_ex.Message.ToString());
39                  return bContinue;                  //return bContinue;
40                    //bCreateDB = true;
41                    try
42                    {
43                        sqlCon.ConnectionString = Properties.Settings.Default.atsConnectionString.Replace("AnywhereTS","master");
44                        sqlCon.Open();
45                        RunScript(Resource1.CreateDatabase.ToString());
46                        sqlCon.Close();
47                        sqlCon.ConnectionString = Properties.Settings.Default.atsConnectionString;
48                        sqlCon.Open();
49                    }
50                    catch (SqlException sql_ex1)
51                    {
52                        MessageBox.Show(string.Format("Fail to connect to SQL Server Instance: {0}\n", InstanceName) + sql_ex1.Number.ToString() + " " + sql_ex1.Message.ToString());
53                        return bContinue;
54                    }
55              }              }
56                    
57              // Now that you are connected to Express, check the database versions              // Now that you are connected to Express, check the database versions
58    
59              Version databaseVersion; // The current version of the database              Version databaseVersion; // The current version of the database
60    
61              switch (CheckVersion(out databaseVersion))              int chkVer = CheckVersion(out databaseVersion);
62                VersionCheck verChk = (VersionCheck)chkVer;
63                //MessageBox.Show(string.Format("Version Check: {0} Version: {1}", verChk.ToString(), databaseVersion.ToString()));
64    
65                switch (chkVer)
66              {              {
67                  case (int)VersionCheck.Equal:                  case (int)VersionCheck.Equal:
68                      {                      {
# Line 177  namespace AnywhereTS Line 197  namespace AnywhereTS
197                      //Drop the database                      //Drop the database
198                      sqlCmd = new SqlCommand("IF EXISTS(SELECT * FROM sysdatabases WHERE name='AnywhereTS')DROP DATABASE AnywhereTS", sqlCon);                      sqlCmd = new SqlCommand("IF EXISTS(SELECT * FROM sysdatabases WHERE name='AnywhereTS')DROP DATABASE AnywhereTS", sqlCon);
199                      strResult = (string)sqlCmd.ExecuteScalar();                      strResult = (string)sqlCmd.ExecuteScalar();
200                      return (int)VersionCheck.DatabaseNotFound;                                              return (int)VersionCheck.DatabaseNotFound;
201                  }                  }
202                  vDb = new Version(strResult);                  vDb = new Version(strResult);
203                        
# Line 265  namespace AnywhereTS Line 285  namespace AnywhereTS
285              }              }
286              catch              catch
287              {              {
288                  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.");
289              }              }
290          }          }
291    
# Line 284  namespace AnywhereTS Line 304  namespace AnywhereTS
304              {              {
305                  prot.Get();                  prot.Get();
306                  if ((string)prot.GetPropertyValue("ProtocolName") == "Np" &&  //Named pipes                  if ((string)prot.GetPropertyValue("ProtocolName") == "Np" &&  //Named pipes
307                      (string)prot.GetPropertyValue("InstanceName") == "SQLEXPRESS")                      (string)prot.GetPropertyValue("InstanceName") == InstanceName)
308                  {   // We found the named pipes protocol                  {   // We found the named pipes protocol
309                      if (!(bool)prot.GetPropertyValue("Enabled"))                      if (!(bool)prot.GetPropertyValue("Enabled"))
310                      {   // Named pipes not activated                      {   // Named pipes not activated
# Line 292  namespace AnywhereTS Line 312  namespace AnywhereTS
312                                
313                          // Check if user wants to restart SQL server                          // Check if user wants to restart SQL server
314                          DialogResult resultRights;                          DialogResult resultRights;
315                          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);
316                          if (resultRights == DialogResult.Yes)                          if (resultRights == DialogResult.Yes)
317                          {                          {
318                              // Restart the SQL server                              // Restart the SQL server
# Line 301  namespace AnywhereTS Line 321  namespace AnywhereTS
321                              foreach (ManagementObject svc in sqlServicesMan.GetInstances())                              foreach (ManagementObject svc in sqlServicesMan.GetInstances())
322                              {                              {
323                                  if ((uint)svc.GetPropertyValue("SqlServiceType") == sqlServerService &&                                  if ((uint)svc.GetPropertyValue("SqlServiceType") == sqlServerService &&
324                                      (string)svc.GetPropertyValue("ServiceName") == "MSSQL$SQLEXPRESS")                                      (string)svc.GetPropertyValue("ServiceName") == string.Format("MSSQL${0}", InstanceName))
325                                  {                                  {
326                                      svc.Get();                                      svc.Get();
327                                      if ((uint)svc.GetPropertyValue("State") != sqlServiceStopped)                                      if ((uint)svc.GetPropertyValue("State") != sqlServiceStopped)

Legend:
Removed from v.10  
changed lines
  Added in v.18

  ViewVC Help
Powered by ViewVC 1.1.22