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