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