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

Annotation of /trunk/TSAdminTool/Database.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 88 - (hide annotations) (download)
Fri Jul 13 22:42:09 2012 UTC (9 years ago) by william
File size: 32377 byte(s)

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

  ViewVC Help
Powered by ViewVC 1.1.22