/[AnywhereTS-MSSQL]/trunk/TSAdminTool/Database.cs
ViewVC logotype

Contents of /trunk/TSAdminTool/Database.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 90 - (show annotations) (download)
Fri Jul 13 22:58:13 2012 UTC (8 years, 6 months ago) by william
File size: 31779 byte(s)

1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Data;
5 using System.Data.SqlClient;
6 using System.Windows.Forms;
7 using System.Text.RegularExpressions;
8 using System.Management;
9 using log4net;
10 using System.Security.Principal;
11 using AnywhereTS.DBSupport;
12
13 namespace AnywhereTS
14 {
15 class DatabaseSupport
16 {
17
18 enum VersionCheck
19 {
20 Failed = 0,
21 Equal,
22 DatabaseIsMoreNew,
23 DatabaseIsOlder,
24 //DatabaseNotFound,
25 DatabaseCreated,
26 };
27
28 private SqlConnection sqlCon = new SqlConnection();
29 private SqlCommand sqlCmd = new SqlCommand();
30
31 const string SQL_BROWSER_SERVICE_NAME = "SQLBrowser";
32 public static string InstanceName { get { return Properties.Settings.Default.DBInstance; } }
33 public bool SetupDatabase()
34 {
35 Exception ErrorInfo = null;
36 bool bContinue = false;
37 // Create a connection to SQL Server
38
39 Logging.ATSAdminLog.Debug("SetupDatabase() called ");
40 #region Database Creation support
41 using (MsSqlConnector conn = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, ATSGlobals.strDatabaseName))
42 {
43 try
44 {
45 conn.CreateConnection(out ErrorInfo);
46 conn.OpenConnection(out ErrorInfo);
47 }
48 catch (Exception ex)
49 {
50 try
51 {
52 conn.Dispose();
53 using (MsSqlConnector conn1 = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, "master"))
54 {
55 try
56 {
57 conn1.CreateConnection(out ErrorInfo);
58 conn1.OpenConnection(out ErrorInfo);
59
60 // create datagbase
61 Logging.ATSAdminLog.Info(string.Format("Creating Database {0}", ATSGlobals.strDatabaseName));
62 RunScript(SQLServerResources.CreateDatabase, conn1, out ErrorInfo);
63 Logging.ATSAdminLog.Info(string.Format("Created Database {0}", ATSGlobals.strDatabaseName));
64
65 // create tables
66 Logging.ATSAdminLog.Info(string.Format("Creating Tables in {0} Database", ATSGlobals.strDatabaseName));
67 RunScript(SQLServerResources.CreateTables, conn1, out ErrorInfo);
68 Logging.ATSAdminLog.Info(string.Format("Created Tables in {0} Database", ATSGlobals.strDatabaseName));
69 }
70 catch (Exception ex1)
71 {
72 if (ErrorInfo == null) { ErrorInfo = new Exception(ex1.Message, ex); }
73 }
74 }
75 }
76 catch (Exception ex1)
77 {
78 if (ErrorInfo == null) { ErrorInfo = new Exception(ex1.Message, ex); }
79 }
80 }
81 }
82 if (ErrorInfo != null) { return false; }
83 #endregion
84 #region Database version check
85 // Now that you are connected to Express, check the database versions
86 Version databaseVersion; // The current version of the database
87 int chkVer = CheckVersion(out databaseVersion);
88 VersionCheck verChk = (VersionCheck)chkVer;
89 //MessageBox.Show(string.Format("Version Check: {0} Version: {1}", verChk.ToString(), databaseVersion.ToString()));
90 Logging.ATSAdminLog.InfoFormat("Database Version Check: {0} .... Database Version: {1}", verChk.ToString(), databaseVersion.ToString());
91 switch (chkVer)
92 {
93 case (int)VersionCheck.Equal:
94 {
95 bContinue = true;
96 break;
97 }
98 case (int)VersionCheck.Failed:
99 {
100 bContinue = false;
101 break;
102 }
103 case (int)VersionCheck.DatabaseIsOlder:
104 {
105 using (MsSqlConnector conn = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, ATSGlobals.strDatabaseName))
106 {
107 try
108 {
109 conn.CreateConnection(out ErrorInfo);
110 conn.OpenConnection(out ErrorInfo);
111 #region database upggrade scripts
112 switch (databaseVersion.ToString())
113 {
114 // Run the apropriate upgdrade script(s)
115 case "1.0.0.0":
116 { // Current database is version 1.0.0.0, update to 1.0.0.1
117 Logging.ATSAdminLog.InfoFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.0", "1.0.0.1");
118 bContinue = RunScript(SQLServerResources.UpdateDatabase1, conn, out ErrorInfo);
119 Logging.ATSAdminLog.InfoFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.0", "1.0.0.1");
120 goto case "1.0.0.1"; // Continue and upgrade one more step
121 }
122 case "1.0.0.1":
123 { // Current database is version 1.0.0.1, update to 1.0.0.2
124 Logging.ATSAdminLog.InfoFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.1", "1.0.0.2");
125 bContinue = RunScript(SQLServerResources.UpdateDatabase2, conn, out ErrorInfo);
126 Logging.ATSAdminLog.InfoFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.2", "1.0.0.2");
127 goto case "1.0.0.2"; // Continue and upgrade one more step
128 }
129 case "1.0.0.2":
130 { // Current database is version 1.0.0.2, update to 1.0.0.3
131 Logging.ATSAdminLog.InfoFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.2", "1.0.0.3");
132 bContinue = RunScript(SQLServerResources.UpdateDatabase3, conn, out ErrorInfo);
133 Logging.ATSAdminLog.InfoFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.2", "1.0.0.3");
134 goto case "1.0.0.3"; // Continue and upgrade one more step
135 }
136 case "1.0.0.3":
137 { // Current database is version 1.0.0.3, update to 1.0.0.4
138 Logging.ATSAdminLog.InfoFormat("Upgrading Database from version: {0} to version: {1}", "1.0.0.3", "1.0.0.4");
139 bContinue = RunScript(SQLServerResources.UpdateDatabase4, conn, out ErrorInfo);
140 Logging.ATSAdminLog.InfoFormat("Upgraded Database from version: {0} to version: {1}", "1.0.0.4", "1.0.0.4");
141 break;
142 }
143 default:
144 {
145 //MessageBox.Show("Error: Not able to upgrade database (51188)");
146 if (databaseVersion == new Version(0, 0, 0, 0))
147 {
148 string format = string.Format("Database version is {0}, this should have been auto upgraded to {1}", databaseVersion.ToString(), ATSGlobals.strDatabaseVersion);
149 Logging.ATSAdminLog.Fatal(format);
150 MessageBox.Show(format);
151 }
152 else
153 {
154 string format = string.Format("Failed to upgrade Database from version: {0} to version: {1}", databaseVersion.ToString(), ATSGlobals.strDatabaseVersion);
155 Logging.ATSAdminLog.Fatal(format);
156 MessageBox.Show(format);
157 }
158 break;
159 }
160 }
161 #endregion
162 }
163 catch (Exception ex)
164 {
165 if (ErrorInfo == null) { ErrorInfo = ex; }
166 }
167 }
168 break;
169 }
170 case (int)VersionCheck.DatabaseIsMoreNew:
171 {
172 bContinue = false;
173 break;
174 }
175 default:
176 {
177 bContinue = false;
178 break;
179 }
180
181 }
182 #endregion
183 sqlCon.Close();
184 sqlCon.Dispose();
185 sqlCmd.Connection.Close();
186 sqlCmd.Connection.Dispose();
187 return bContinue;
188 }
189
190 // Run a SQL script (to create or update a database)
191 [Obsolete("RunScript(string strFile) is being replaced by RunScript(string strFile, MsSqlConnector con, out Exception ErrorInfo)", false)]
192 public bool RunScript(string strFile)
193 {
194 return false;
195 }
196 public bool RunScript(string strFile, MsSqlConnector con, out Exception ErrorInfo)
197 {
198 ErrorInfo = null;
199 string cmd = strFile.Replace("[DataDir]", ProSupport.strDatabasePath);
200 con.RunScript(cmd, out ErrorInfo);
201 return false;
202 }
203 // Check the version of the datbase
204 public int CheckVersion(out Version vDb)
205 {
206 Exception ErrorInfo = null;
207 //Get Version information from application
208 Version v=new Version(ATSGlobals.strDatabaseVersion);
209 vDb = new Version("0.0.0.0"); // Assign a default value for version
210 try
211 {
212
213 //string strResult;
214
215 int db_count = -1;
216 //Verify that the AnywhereTS database exists
217 #region Get Database Count
218 using (MsSqlConnector conn = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, ATSGlobals.strDatabaseName))
219 {
220 try
221 {
222 conn.CreateConnection(out ErrorInfo);
223 conn.OpenConnection(out ErrorInfo);
224
225 //Logging.ATSAdminLog.Debug(string.Format("Getting Coount of {0} databases", ATSGlobals.strDatabaseName));
226
227 SqlCommand cmd = conn.CreateCommandInstance(string.Format("select count(*) from master..sysdatabases where name='{0}'", ATSGlobals.strDatabaseName), new List<SqlParameter>(), out ErrorInfo);
228 db_count = Convert.ToInt32(cmd.ExecuteScalar());
229
230 Logging.ATSAdminLog.Info(string.Format("Found {0} databases named {1}", db_count, ATSGlobals.strDatabaseName));
231 }
232 catch (Exception ex)
233 {
234 if (ErrorInfo == null)
235 {
236 ErrorInfo = ex;
237 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
238 {
239 Logging.DatabaseLog.Error(string.Format("Failed to get count of databases named: {0}{1}", System.Environment.NewLine, ATSGlobals.strDatabaseName));
240 }
241 }
242 else
243 {
244 ErrorInfo = ex.GetBaseException();
245 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ErrorInfo.GetType().Name, ErrorInfo.Message, System.Environment.NewLine, ErrorInfo.ToString())))
246 {
247 Logging.DatabaseLog.Error(string.Format("Failed to get count of databases named: {0}{1}", System.Environment.NewLine, ATSGlobals.strDatabaseName));
248 }
249 }
250 }
251 }
252 #endregion
253 if (db_count == -1)
254 {
255 return (int)VersionCheck.Failed;
256 }
257 else if (db_count == 0)
258 {
259 #region Database Creation support
260 using (MsSqlConnector conn = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, ATSGlobals.strDatabaseName))
261 {
262 try
263 {
264 conn.CreateConnection(out ErrorInfo);
265 conn.OpenConnection(out ErrorInfo);
266 }
267 catch (Exception ex)
268 {
269 try
270 {
271 conn.Dispose();
272 using (MsSqlConnector conn1 = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, "master"))
273 {
274 try
275 {
276 conn1.CreateConnection(out ErrorInfo);
277 conn1.OpenConnection(out ErrorInfo);
278
279 // create datagbase
280 Logging.ATSAdminLog.Info(string.Format("Creating Database {0}", ATSGlobals.strDatabaseName));
281 RunScript(SQLServerResources.CreateDatabase, conn1, out ErrorInfo);
282 Logging.ATSAdminLog.Info(string.Format("Created Database {0}", ATSGlobals.strDatabaseName));
283
284 // create tables
285 Logging.ATSAdminLog.Info(string.Format("Creating Tables in {0} Database", ATSGlobals.strDatabaseName));
286 RunScript(SQLServerResources.CreateTables, conn1, out ErrorInfo);
287 Logging.ATSAdminLog.Info(string.Format("Created Tables in {0} Database", ATSGlobals.strDatabaseName));
288 }
289 catch (Exception ex1)
290 {
291 if (ErrorInfo == null) { ErrorInfo = new Exception(ex1.Message, ex); }
292 }
293 }
294 }
295 catch (Exception ex1)
296 {
297 if (ErrorInfo == null) { ErrorInfo = new Exception(ex1.Message, ex); }
298 }
299 }
300 }
301 if (ErrorInfo != null) { return (int)VersionCheck.Failed; }
302 #endregion
303 }
304
305
306 #region Get Database Version
307 using (MsSqlConnector conn = new MsSqlConnector(Properties.Settings.Default.DBServer, Properties.Settings.Default.DBInstance, ATSGlobals.strDatabaseName))
308 {
309 try
310 {
311 conn.CreateConnection(out ErrorInfo);
312 conn.OpenConnection(out ErrorInfo);
313
314 //Logging.ATSAdminLog.Debug(string.Format("Getting Coount of {0} databases", ATSGlobals.strDatabaseName));
315
316 SqlCommand cmd = conn.CreateCommandInstance(string.Format("SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName), new List<SqlParameter>(), out ErrorInfo);
317 string version = (string)cmd.ExecuteScalar();
318 vDb = new Version(version);
319 Logging.ATSAdminLog.Info(string.Format("Database {0} is at Version: {1}", ATSGlobals.strDatabaseName, version));
320 }
321 catch (Exception ex)
322 {
323 if (ErrorInfo == null)
324 {
325 ErrorInfo = ex;
326 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ex.GetType().Name, ex.Message, System.Environment.NewLine, ex.ToString())))
327 {
328 Logging.DatabaseLog.Error(string.Format("Failed to get count of databases named: {0}{1}", System.Environment.NewLine, ATSGlobals.strDatabaseName));
329 }
330 }
331 else
332 {
333 ErrorInfo = ex.GetBaseException();
334 using (log4net.NDC.Push(string.Format("{0}: MESSAGE={1}{2}Diagnostics:{2}{3}", ErrorInfo.GetType().Name, ErrorInfo.Message, System.Environment.NewLine, ErrorInfo.ToString())))
335 {
336 Logging.DatabaseLog.Error(string.Format("Failed to get count of databases named: {0}{1}", System.Environment.NewLine, ATSGlobals.strDatabaseName));
337 }
338 }
339 }
340 }
341 #endregion
342
343 #region old-code might still need
344 //try
345 //{
346 // //using (log4net.NDC.Push(string.Format("SQL Statment={0}", "SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName)))
347 // //{
348 // Logging.ATSAdminLog.Debug(string.Format("Getting {0} Database Version#1", ATSGlobals.strDatabaseName));
349 // sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
350 // if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
351 // sqlCmd = new SqlCommand(string.Format("SELECT value from {0}..AppInfo where property='version'",ATSGlobals.strDatabaseName), sqlCon);
352 // strResult = (string)sqlCmd.ExecuteScalar();
353 // sqlCon.Close();
354 // //}
355 // Logging.ATSAdminLog.DebugFormat("{0} database version={1}",ATSGlobals.strDatabaseName, strResult);
356 //}
357 //catch(SqlException ex)
358 //{
359 // // the database exists, but one or more tables are missing
360 // using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex.Number.ToString(), ex.Message, System.Environment.NewLine, ex.ToString())))
361 // {
362 // Logging.ATSAdminLog.Error("Failed to get database version");
363 // }
364 // try
365 // {
366 // //using (log4net.NDC.Push(string.Format("SQL Statment={0}", SQLServerResources.CreateTables)))
367 // //{
368 // Logging.ATSAdminLog.Debug(string.Format("Creating Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
369 // sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
370 // if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
371 // RunScript(SQLServerResources.CreateTables);
372 // sqlCon.Close();
373 // //}
374 // Logging.ATSAdminLog.Debug(string.Format("Created Tables in {0} Database in CheckVersion()", ATSGlobals.strDatabaseName));
375 // using (log4net.NDC.Push(string.Format("SQL Statment={0}", string.Format("SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName))))
376 // {
377 // Logging.ATSAdminLog.Debug(string.Format("Getting {0} Database Version#2", ATSGlobals.strDatabaseName));
378 // sqlCon = new SqlConnection(Properties.Settings.Default.atsConnectionString.Replace(ATSGlobals.strDatabaseName, "master"));
379 // if (sqlCon.State != ConnectionState.Open) sqlCon.Open();
380 // sqlCmd = new SqlCommand(string.Format("SELECT value from {0}..AppInfo where property='version'", ATSGlobals.strDatabaseName), sqlCon);
381 // strResult = (string)sqlCmd.ExecuteScalar();
382 // sqlCon.Close();
383 // }
384 // Logging.ATSAdminLog.DebugFormat("{0} database version={1}",ATSGlobals.strDatabaseName, strResult);
385 // }
386 // catch(SqlException ex1)
387 // {
388 // using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", ex1.Number.ToString(), ex1.Message, System.Environment.NewLine, ex1.ToString())))
389 // {
390 // Logging.ATSAdminLog.Error("Failed to get database version");
391 // }
392 // return (int)VersionCheck.Failed;
393 // }
394 //}
395 #endregion
396
397
398 sqlCon.Close();
399
400 if (vDb == v)
401 return (int)VersionCheck.Equal;
402
403 if (vDb > v)
404 return (int)VersionCheck.DatabaseIsMoreNew;
405
406 else
407 return (int)VersionCheck.DatabaseIsOlder;
408
409 }
410 catch (SqlException sql_ex)
411 {
412 MessageBox.Show(sql_ex.Number.ToString() + " " + sql_ex.Message.ToString());
413 using (log4net.NDC.Push(string.Format("SqlException: ID={0} MESSAGE={1}{2}Diagnostics:{2}{3}", sql_ex.Number.ToString(), sql_ex.Message, System.Environment.NewLine, sql_ex.ToString())))
414 {
415 Logging.ATSAdminLog.Error("Failed to check database version");
416 }
417 return (int)VersionCheck.Failed;
418 }
419 catch (Exception system_ex)
420 {
421 MessageBox.Show(system_ex.Message.ToString());
422 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", system_ex.Message, System.Environment.NewLine, system_ex.ToString())))
423 {
424 Logging.ATSAdminLog.Error("Failed to check database version");
425 }
426 return (int)VersionCheck.Failed;
427 }
428 }
429
430 public string[] ParseScriptToCommands(string strScript)
431 {
432 string[] commands;
433 commands = Regex.Split(strScript, "GO\r\n", RegexOptions.IgnoreCase);
434 return commands;
435 }
436
437
438 [Obsolete("SetDatabaseRights() has been deperecated - user rights are assigned via SQL Server")]
439 public static void SetDatabaseRights()
440 {
441 try
442 {
443 // Add an access control entry to the database file.
444 ProSupport.GrantRWaccessForRemoteDesktopUsers(ProSupport.strDatabasePath + @"\" + ProSupport.strDatabaseFilename);
445 ProSupport.GrantRWaccessForRemoteDesktopUsers(ProSupport.strDatabasePath + @"\" + ProSupport.strDatabaseFilename2);
446 }
447 catch (Exception e)
448 {
449 MessageBox.Show("Cannot set access rights for users to the database. Do you have the sufficient rights? Application will abort. Error: " + e.Message);
450 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
451 {
452 Logging.ATSAdminLog.Fatal("Cannot set access rights for users to the database.");
453 }
454 Application.Exit();
455 return;
456 }
457 }
458
459 public static void StartSQLbrowserService()
460 {
461 System.ServiceProcess.ServiceController srvController = new System.ServiceProcess.ServiceController(SQL_BROWSER_SERVICE_NAME);
462 try
463 {
464 // Check that the SQL browser service is not already running
465 if (srvController.Status != System.ServiceProcess.ServiceControllerStatus.Running)
466 { // Service not running, start it.
467 srvController.Start();
468 srvController.WaitForStatus(System.ServiceProcess.ServiceControllerStatus.Running, TimeSpan.FromSeconds(50));
469 }
470 }
471
472 catch (Exception e)
473 {
474 MessageBox.Show("Could not start the SQL Browser service (13078). Error:" + e.Message);
475 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
476 {
477 Logging.ATSAdminLog.Error("Could not start the SQL Browser service (13078).");
478 }
479 }
480 }
481
482 // Configure the SQL browser service to autostart
483 public static void AutostartSQLbrowserService()
484 {
485 try
486 {
487 //construct the management path
488 string path = "Win32_Service.Name='" + SQL_BROWSER_SERVICE_NAME + "'";
489 using (ManagementObject service = new ManagementObject(new ManagementPath(path)))
490 {
491 object[] parameters = new object[1];
492 parameters[0] = "Automatic";
493 service.InvokeMethod("ChangeStartMode", parameters);
494
495 }
496 }
497 catch(Exception e)
498 {
499 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.");
500 using (log4net.NDC.Push(string.Format("SqlException: MESSAGE={0}{1}Diagnostics:{1}{2}", e.Message, System.Environment.NewLine, e.ToString())))
501 {
502 Logging.ATSAdminLog.Fatal("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.");
503 }
504 }
505 }
506
507 // Enable named pipes on the SQL Express server
508 public static bool EnableNamedPipes()
509 {
510 ManagementScope manScope = new ManagementScope(@"\\.\root\Microsoft\SqlServer\ComputerManagement");
511 ManagementClass sqlServicesMan = new ManagementClass(manScope, new ManagementPath("SqlService"), null);
512 ManagementClass serverProtocolsMan = new ManagementClass(manScope, new ManagementPath("ServerNetworkProtocol"), null);
513 bool restarted = false; // Indicating if restart of SQL server was performed
514
515 sqlServicesMan.Get();
516 serverProtocolsMan.Get();
517
518 foreach (ManagementObject prot in serverProtocolsMan.GetInstances())
519 {
520 prot.Get();
521 if ((string)prot.GetPropertyValue("ProtocolName") == "Np" && //Named pipes
522 (string)prot.GetPropertyValue("InstanceName") == InstanceName)
523 { // We found the named pipes protocol
524 if (!(bool)prot.GetPropertyValue("Enabled"))
525 { // Named pipes not activated
526 prot.InvokeMethod("SetEnable", null); // Activate named pipes
527
528 // Check if user wants to restart SQL server
529 DialogResult resultRights;
530 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);
531 if (resultRights == DialogResult.Yes)
532 {
533 // Restart the SQL server
534 const uint sqlServerService = 1;
535 const uint sqlServiceStopped = 1;
536 foreach (ManagementObject svc in sqlServicesMan.GetInstances())
537 {
538 if ((uint)svc.GetPropertyValue("SqlServiceType") == sqlServerService &&
539 (string)svc.GetPropertyValue("ServiceName") == string.Format("MSSQL${0}", InstanceName))
540 {
541 svc.Get();
542 if ((uint)svc.GetPropertyValue("State") != sqlServiceStopped)
543 {
544 svc.InvokeMethod("StopService", null);
545 }
546 svc.InvokeMethod("StartService", null);
547 restarted = true;
548 } // end if
549 }
550 }
551 } // end if 'named pipes protool'
552 }
553 } // foreach
554
555 return restarted;
556 }
557 }
558 }

  ViewVC Help
Powered by ViewVC 1.1.22