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