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