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

Contents of /trunk/TSAdminTool/Database.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 99 - (show annotations) (download)
Sat Jul 14 04:50:27 2012 UTC (7 years, 11 months ago) by william
File size: 32326 byte(s)
+ bulk commit

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

  ViewVC Help
Powered by ViewVC 1.1.22