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

Contents of /trunk/TSAdminTool/Database.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 119 - (show annotations) (download)
Sat Jul 14 09:08:55 2012 UTC (9 years, 2 months ago) by william
File size: 33328 byte(s)
+ bulk commit again

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

  ViewVC Help
Powered by ViewVC 1.1.22