hytos / DTI_PID / APIDConverter / DB / Project_DB.cs @ a77303ca
이력 | 보기 | 이력해설 | 다운로드 (19.2 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Threading.Tasks; |
6 |
using System.Globalization; |
7 |
using System.Data.SQLite; |
8 |
using System.Data; |
9 |
using System.Data.SqlClient; |
10 |
using System.IO; |
11 |
using Newtonsoft.Json; |
12 |
using AVEVA.PID.CustomizationUtility; |
13 |
using AVEVA.PID.CustomizationUtility.Model; |
14 |
|
15 |
namespace AVEVA.PID.CustomizationUtility.DB |
16 |
{ |
17 |
public class Project_DB |
18 |
{ |
19 |
const string APID_SYMBOL_MAPPING_TABLE = "T_APID_SYMBOL_MAPPING"; |
20 |
|
21 |
const string LineProperties_TABLE = "LineProperties"; |
22 |
const string LineTypes_TABLE = "LineTypes"; |
23 |
const string SymbolType_TABLE = "SymbolType"; |
24 |
const string SymbolAttribute_TABLE = "SymbolAttribute"; |
25 |
const string Symbol_TABLE = "Symbol"; |
26 |
const string OPCRelations_TABLE = "OPCRelations"; |
27 |
|
28 |
private static SqlConnection GetSqlConnection() |
29 |
{ |
30 |
Project_Info projectInfo = Project_Info.GetInstance(); |
31 |
SqlConnection connection = null; |
32 |
try |
33 |
{ |
34 |
connection = new SqlConnection(string.Format(CultureInfo.CurrentCulture, |
35 |
@"Data Source = {0},{1}; Initial CataLog = {2}; User ID = {3}; Password = {4};", |
36 |
projectInfo.ServerIP, |
37 |
projectInfo.Port, |
38 |
System.IO.Path.GetFileName(projectInfo.DefaultPath), |
39 |
projectInfo.DBUser, |
40 |
projectInfo.DBPassword)); |
41 |
|
42 |
connection.Open(); |
43 |
} |
44 |
catch (Exception ex) |
45 |
{ |
46 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
47 |
if (connection != null) |
48 |
connection.Dispose(); |
49 |
connection = null; |
50 |
} |
51 |
|
52 |
return connection; |
53 |
} |
54 |
public static bool ConnTestAndCreateTable() |
55 |
{ |
56 |
bool result = false; |
57 |
Project_Info projectInfo = Project_Info.GetInstance(); |
58 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
59 |
{ |
60 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath))) |
61 |
{ |
62 |
try |
63 |
{ |
64 |
connection.Open(); |
65 |
if (connection.State == ConnectionState.Open) |
66 |
{ |
67 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
68 |
{ |
69 |
cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'"; |
70 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
71 |
using (DataTable dt = new DataTable()) |
72 |
{ |
73 |
dt.Load(dr); |
74 |
|
75 |
if (dt.Select(string.Format("NAME = '{0}'", APID_SYMBOL_MAPPING_TABLE)).Length == 0) |
76 |
{ |
77 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, APID_SYMBOL TEXT)", APID_SYMBOL_MAPPING_TABLE); |
78 |
cmd.ExecuteNonQuery(); |
79 |
} |
80 |
} |
81 |
} |
82 |
result = true; |
83 |
} |
84 |
connection.Close(); |
85 |
} |
86 |
catch (Exception ex) |
87 |
{ |
88 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
89 |
} |
90 |
finally |
91 |
{ |
92 |
connection.Dispose(); |
93 |
} |
94 |
} |
95 |
} |
96 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
97 |
{ |
98 |
using (SqlConnection connection = GetSqlConnection()) |
99 |
{ |
100 |
try |
101 |
{ |
102 |
if (connection != null && connection.State == ConnectionState.Open) |
103 |
{ |
104 |
using (SqlCommand cmd = connection.CreateCommand()) |
105 |
{ |
106 |
cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES"; |
107 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
108 |
using (DataTable dt = new DataTable()) |
109 |
{ |
110 |
dt.Load(dr); |
111 |
|
112 |
if (dt.Select(string.Format("NAME = '{0}'", APID_SYMBOL_MAPPING_TABLE)).Length == 0) |
113 |
{ |
114 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(MAX), APID_SYMBOL varchar(MAX))", APID_SYMBOL_MAPPING_TABLE); |
115 |
cmd.ExecuteNonQuery(); |
116 |
} |
117 |
|
118 |
} |
119 |
} |
120 |
result = true; |
121 |
} |
122 |
} |
123 |
catch (Exception ex) |
124 |
{ |
125 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
126 |
} |
127 |
finally |
128 |
{ |
129 |
if (connection != null) |
130 |
connection.Dispose(); |
131 |
} |
132 |
} |
133 |
} |
134 |
|
135 |
return result; |
136 |
} |
137 |
public static DataTable GetSymbolMappingTable() |
138 |
{ |
139 |
DataTable dt = new DataTable(); |
140 |
Project_Info projectInfo = Project_Info.GetInstance(); |
141 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
142 |
{ |
143 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
144 |
{ |
145 |
try |
146 |
{ |
147 |
connection.Open(); |
148 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
149 |
{ |
150 |
cmd.CommandText = string.Format(@" |
151 |
SELECT s.UID, s.Name, st.Type, sp.APID_SYMBOL FROM {1} as st, {0} as s |
152 |
LEFT OUTER JOIN {2} as sp |
153 |
ON s.UID = SP.UID |
154 |
WHERE s.SymbolType_UID = st.UID |
155 |
ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_SYMBOL_MAPPING_TABLE); |
156 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
157 |
dt.Load(dr); |
158 |
|
159 |
DataTable dtClone = dt.Clone(); |
160 |
dtClone.Columns["UID"].DataType = typeof(string); |
161 |
foreach (DataRow row in dt.Rows) |
162 |
{ |
163 |
dtClone.ImportRow(row); |
164 |
} |
165 |
dt.Dispose(); |
166 |
dt = dtClone; |
167 |
} |
168 |
connection.Close(); |
169 |
} |
170 |
catch (Exception ex) |
171 |
{ |
172 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
173 |
} |
174 |
finally |
175 |
{ |
176 |
connection.Dispose(); |
177 |
} |
178 |
} |
179 |
} |
180 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
181 |
{ |
182 |
using (SqlConnection connection = GetSqlConnection()) |
183 |
{ |
184 |
try |
185 |
{ |
186 |
if (connection != null && connection.State == ConnectionState.Open) |
187 |
{ |
188 |
using (SqlCommand cmd = connection.CreateCommand()) |
189 |
{ |
190 |
cmd.CommandText = string.Format(@" |
191 |
SELECT CONVERT(VARCHAR(255), s.UID) AS UID, s.Name, st.Type, sp.APID_SYMBOL FROM {1} as st, {0} as s |
192 |
LEFT OUTER JOIN {2} as sp |
193 |
ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID) |
194 |
WHERE s.SymbolType_UID = st.UID |
195 |
ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_SYMBOL_MAPPING_TABLE); |
196 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
197 |
dt.Load(dr); |
198 |
} |
199 |
connection.Close(); |
200 |
} |
201 |
} |
202 |
catch (Exception ex) |
203 |
{ |
204 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
205 |
} |
206 |
finally |
207 |
{ |
208 |
if (connection != null) |
209 |
connection.Dispose(); |
210 |
} |
211 |
} |
212 |
} |
213 |
|
214 |
return dt; |
215 |
} |
216 |
|
217 |
|
218 |
|
219 |
|
220 |
#region Only ID2 DB |
221 |
public static DataTable SelectID2SymbolTable() |
222 |
{ |
223 |
DataTable dt = new DataTable(); |
224 |
Project_Info projectInfo = Project_Info.GetInstance(); |
225 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
226 |
{ |
227 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
228 |
{ |
229 |
try |
230 |
{ |
231 |
connection.Open(); |
232 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
233 |
{ |
234 |
cmd.CommandText = @"SELECT * FROM Symbol"; |
235 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
236 |
dt.Load(dr); |
237 |
} |
238 |
connection.Close(); |
239 |
} |
240 |
catch (Exception ex) |
241 |
{ |
242 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
243 |
} |
244 |
finally |
245 |
{ |
246 |
connection.Dispose(); |
247 |
} |
248 |
} |
249 |
} |
250 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
251 |
{ |
252 |
using (SqlConnection connection = GetSqlConnection()) |
253 |
{ |
254 |
try |
255 |
{ |
256 |
if (connection != null && connection.State == ConnectionState.Open) |
257 |
{ |
258 |
using (SqlCommand cmd = connection.CreateCommand()) |
259 |
{ |
260 |
cmd.CommandText = @"SELECT * FROM Symbol"; |
261 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
262 |
dt.Load(dr); |
263 |
} |
264 |
connection.Close(); |
265 |
} |
266 |
} |
267 |
catch (Exception ex) |
268 |
{ |
269 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
270 |
} |
271 |
finally |
272 |
{ |
273 |
if (connection != null) |
274 |
connection.Dispose(); |
275 |
} |
276 |
} |
277 |
} |
278 |
|
279 |
return dt; |
280 |
} |
281 |
public static DataTable SelectSymbolType() |
282 |
{ |
283 |
DataTable dt = new DataTable(); |
284 |
Project_Info projectInfo = Project_Info.GetInstance(); |
285 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
286 |
{ |
287 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
288 |
{ |
289 |
try |
290 |
{ |
291 |
connection.Open(); |
292 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
293 |
{ |
294 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE); |
295 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
296 |
dt.Load(dr); |
297 |
} |
298 |
connection.Close(); |
299 |
} |
300 |
catch (Exception ex) |
301 |
{ |
302 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
303 |
} |
304 |
finally |
305 |
{ |
306 |
connection.Dispose(); |
307 |
} |
308 |
} |
309 |
} |
310 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
311 |
{ |
312 |
using (SqlConnection connection = GetSqlConnection()) |
313 |
{ |
314 |
try |
315 |
{ |
316 |
if (connection != null && connection.State == ConnectionState.Open) |
317 |
{ |
318 |
using (SqlCommand cmd = connection.CreateCommand()) |
319 |
{ |
320 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE); |
321 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
322 |
dt.Load(dr); |
323 |
} |
324 |
connection.Close(); |
325 |
} |
326 |
} |
327 |
catch (Exception ex) |
328 |
{ |
329 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
330 |
} |
331 |
finally |
332 |
{ |
333 |
if (connection != null) |
334 |
connection.Dispose(); |
335 |
} |
336 |
} |
337 |
} |
338 |
|
339 |
return dt; |
340 |
} |
341 |
public static DataTable SelectDrawings() |
342 |
{ |
343 |
DataTable dt = new DataTable(); |
344 |
Project_Info projectInfo = Project_Info.GetInstance(); |
345 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
346 |
{ |
347 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
348 |
{ |
349 |
try |
350 |
{ |
351 |
connection.Open(); |
352 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
353 |
{ |
354 |
cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings"); |
355 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
356 |
dt.Load(dr); |
357 |
} |
358 |
connection.Close(); |
359 |
} |
360 |
catch (Exception ex) |
361 |
{ |
362 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
363 |
} |
364 |
finally |
365 |
{ |
366 |
connection.Dispose(); |
367 |
} |
368 |
} |
369 |
} |
370 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
371 |
{ |
372 |
using (SqlConnection connection = GetSqlConnection()) |
373 |
{ |
374 |
try |
375 |
{ |
376 |
if (connection != null && connection.State == ConnectionState.Open) |
377 |
{ |
378 |
using (SqlCommand cmd = connection.CreateCommand()) |
379 |
{ |
380 |
cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings"); |
381 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
382 |
dt.Load(dr); |
383 |
} |
384 |
connection.Close(); |
385 |
} |
386 |
} |
387 |
catch (Exception ex) |
388 |
{ |
389 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
390 |
} |
391 |
finally |
392 |
{ |
393 |
if (connection != null) |
394 |
connection.Dispose(); |
395 |
} |
396 |
} |
397 |
} |
398 |
|
399 |
return dt; |
400 |
} |
401 |
#endregion |
402 |
|
403 |
#region AVEVA |
404 |
public static string GetAvevaConnectionString() |
405 |
{ |
406 |
string strConn = string.Empty; |
407 |
if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
408 |
{ |
409 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName); |
410 |
} |
411 |
else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
412 |
{ |
413 |
string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
414 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString); |
415 |
} |
416 |
|
417 |
return strConn; |
418 |
} |
419 |
|
420 |
public static string GetAvevaConnectionString_Reports() |
421 |
{ |
422 |
string strConn = string.Empty; |
423 |
if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
424 |
{ |
425 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName); |
426 |
} |
427 |
else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
428 |
{ |
429 |
string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
430 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString); |
431 |
} |
432 |
|
433 |
return strConn; |
434 |
} |
435 |
|
436 |
public static string GetAvevaConnectionString_Admin() |
437 |
{ |
438 |
string strConn = string.Empty; |
439 |
if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
440 |
{ |
441 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName); |
442 |
} |
443 |
else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
444 |
{ |
445 |
string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
446 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString); |
447 |
} |
448 |
|
449 |
return strConn; |
450 |
} |
451 |
|
452 |
public static DataTable SelectSymbolTable() |
453 |
{ |
454 |
DataTable dt = new DataTable(); |
455 |
|
456 |
using (SqlConnection connection = new SqlConnection()) |
457 |
{ |
458 |
connection.ConnectionString = GetAvevaConnectionString_Admin(); |
459 |
connection.Open(); |
460 |
using (SqlCommand cmd = connection.CreateCommand()) |
461 |
{ |
462 |
cmd.CommandText = string.Format("SELECT * FROM {0}", "SYMBOLS"); |
463 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
464 |
dt.Load(dr); |
465 |
} |
466 |
connection.Close(); |
467 |
} |
468 |
|
469 |
return dt; |
470 |
} |
471 |
|
472 |
#endregion |
473 |
|
474 |
} |
475 |
} |