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

Annotation of /trunk/TSAdminTool/Database.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 176 - (hide annotations) (download)
Mon Jul 16 11:49:13 2012 UTC (7 years, 11 months ago) by william
File size: 35500 byte(s)
uncomment code and add #if HAVE_FRMCONFIGMODE

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

  ViewVC Help
Powered by ViewVC 1.1.22