hytos / DTI_PID / APIDConverter / DB / Project_DB.cs @ 9ff9a98d
이력 | 보기 | 이력해설 | 다운로드 (41.5 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 |
const string APID_OPC_MAPPING_TABLE = "T_APID_OPC_MAPPING"; |
21 |
|
22 |
const string LineProperties_TABLE = "LineProperties"; |
23 |
const string LineTypes_TABLE = "LineTypes"; |
24 |
const string SymbolType_TABLE = "SymbolType"; |
25 |
const string SymbolAttribute_TABLE = "SymbolAttribute"; |
26 |
const string Symbol_TABLE = "Symbol"; |
27 |
const string OPCRelations_TABLE = "OPCRelations"; |
28 |
|
29 |
#region Only ID2 DB |
30 |
private static SqlConnection GetSqlConnection() |
31 |
{ |
32 |
Project_Info projectInfo = Project_Info.GetInstance(); |
33 |
SqlConnection connection = null; |
34 |
try |
35 |
{ |
36 |
connection = new SqlConnection(string.Format(CultureInfo.CurrentCulture, |
37 |
@"Data Source = {0},{1}; Initial CataLog = {2}; User ID = {3}; Password = {4};", |
38 |
projectInfo.ServerIP, |
39 |
projectInfo.Port, |
40 |
System.IO.Path.GetFileName(projectInfo.DefaultPath), |
41 |
projectInfo.DBUser, |
42 |
projectInfo.DBPassword)); |
43 |
|
44 |
connection.Open(); |
45 |
} |
46 |
catch (Exception ex) |
47 |
{ |
48 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
49 |
if (connection != null) |
50 |
connection.Dispose(); |
51 |
connection = null; |
52 |
} |
53 |
|
54 |
return connection; |
55 |
} |
56 |
public static bool ConnTestAndCreateTable() |
57 |
{ |
58 |
bool result = false; |
59 |
Project_Info projectInfo = Project_Info.GetInstance(); |
60 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
61 |
{ |
62 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath))) |
63 |
{ |
64 |
try |
65 |
{ |
66 |
connection.Open(); |
67 |
if (connection.State == ConnectionState.Open) |
68 |
{ |
69 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
70 |
{ |
71 |
cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'"; |
72 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
73 |
using (DataTable dt = new DataTable()) |
74 |
{ |
75 |
dt.Load(dr); |
76 |
|
77 |
if (dt.Select(string.Format("NAME = '{0}'", APID_SYMBOL_MAPPING_TABLE)).Length == 0) |
78 |
{ |
79 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, APID_SYMBOL TEXT, DATA1 TEXT)", APID_SYMBOL_MAPPING_TABLE); |
80 |
cmd.ExecuteNonQuery(); |
81 |
} |
82 |
if (dt.Select(string.Format("NAME = '{0}'", APID_OPC_MAPPING_TABLE)).Length == 0) |
83 |
{ |
84 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, IN_SYMBOL TEXT, OUT_SYMBOL TEXT)", APID_OPC_MAPPING_TABLE); |
85 |
cmd.ExecuteNonQuery(); |
86 |
} |
87 |
} |
88 |
} |
89 |
result = true; |
90 |
} |
91 |
connection.Close(); |
92 |
} |
93 |
catch (Exception ex) |
94 |
{ |
95 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
96 |
} |
97 |
finally |
98 |
{ |
99 |
connection.Dispose(); |
100 |
} |
101 |
} |
102 |
} |
103 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
104 |
{ |
105 |
using (SqlConnection connection = GetSqlConnection()) |
106 |
{ |
107 |
try |
108 |
{ |
109 |
if (connection != null && connection.State == ConnectionState.Open) |
110 |
{ |
111 |
using (SqlCommand cmd = connection.CreateCommand()) |
112 |
{ |
113 |
cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES"; |
114 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
115 |
using (DataTable dt = new DataTable()) |
116 |
{ |
117 |
dt.Load(dr); |
118 |
|
119 |
if (dt.Select(string.Format("NAME = '{0}'", APID_SYMBOL_MAPPING_TABLE)).Length == 0) |
120 |
{ |
121 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(MAX), APID_SYMBOL varchar(MAX), DATA1 varchar(255))", APID_SYMBOL_MAPPING_TABLE); |
122 |
cmd.ExecuteNonQuery(); |
123 |
} |
124 |
if (dt.Select(string.Format("NAME = '{0}'", APID_OPC_MAPPING_TABLE)).Length == 0) |
125 |
{ |
126 |
cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, IN_SYMBOL varchar(MAX), OUT_SYMBOL varchar(MAX))", APID_OPC_MAPPING_TABLE); |
127 |
cmd.ExecuteNonQuery(); |
128 |
} |
129 |
} |
130 |
} |
131 |
result = true; |
132 |
} |
133 |
} |
134 |
catch (Exception ex) |
135 |
{ |
136 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
137 |
} |
138 |
finally |
139 |
{ |
140 |
if (connection != null) |
141 |
connection.Dispose(); |
142 |
} |
143 |
} |
144 |
} |
145 |
|
146 |
return result; |
147 |
} |
148 |
public static DataTable GetSymbolMappingTable() |
149 |
{ |
150 |
DataTable dt = new DataTable(); |
151 |
Project_Info projectInfo = Project_Info.GetInstance(); |
152 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
153 |
{ |
154 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
155 |
{ |
156 |
try |
157 |
{ |
158 |
connection.Open(); |
159 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
160 |
{ |
161 |
cmd.CommandText = string.Format(@" |
162 |
SELECT s.UID, s.Name, st.Type, sp.APID_SYMBOL, sp.DATA1 FROM {1} as st, {0} as s |
163 |
LEFT OUTER JOIN {2} as sp |
164 |
ON s.UID = SP.UID |
165 |
WHERE s.SymbolType_UID = st.UID |
166 |
AND st.Type != 'Instrument OPC''s' AND st.Type != 'Piping OPC''s' |
167 |
ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_SYMBOL_MAPPING_TABLE); |
168 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
169 |
dt.Load(dr); |
170 |
|
171 |
DataTable dtClone = dt.Clone(); |
172 |
dtClone.Columns["UID"].DataType = typeof(string); |
173 |
foreach (DataRow row in dt.Rows) |
174 |
{ |
175 |
dtClone.ImportRow(row); |
176 |
} |
177 |
dt.Dispose(); |
178 |
dt = dtClone; |
179 |
} |
180 |
connection.Close(); |
181 |
} |
182 |
catch (Exception ex) |
183 |
{ |
184 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
185 |
} |
186 |
finally |
187 |
{ |
188 |
connection.Dispose(); |
189 |
} |
190 |
} |
191 |
} |
192 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
193 |
{ |
194 |
using (SqlConnection connection = GetSqlConnection()) |
195 |
{ |
196 |
try |
197 |
{ |
198 |
if (connection != null && connection.State == ConnectionState.Open) |
199 |
{ |
200 |
using (SqlCommand cmd = connection.CreateCommand()) |
201 |
{ |
202 |
cmd.CommandText = string.Format(@" |
203 |
SELECT CONVERT(VARCHAR(255), s.UID) AS UID, s.Name, st.Type, sp.APID_SYMBOL, sp.DATA1 FROM {1} as st, {0} as s |
204 |
LEFT OUTER JOIN {2} as sp |
205 |
ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID) |
206 |
WHERE s.SymbolType_UID = st.UID |
207 |
AND st.Type != 'Instrument OPC''s' AND st.Type != 'Piping OPC''s' |
208 |
ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_SYMBOL_MAPPING_TABLE); |
209 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
210 |
dt.Load(dr); |
211 |
} |
212 |
connection.Close(); |
213 |
} |
214 |
} |
215 |
catch (Exception ex) |
216 |
{ |
217 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
218 |
} |
219 |
finally |
220 |
{ |
221 |
if (connection != null) |
222 |
connection.Dispose(); |
223 |
} |
224 |
} |
225 |
} |
226 |
|
227 |
return dt; |
228 |
} |
229 |
public static DataTable GetOPCMappingTable() |
230 |
{ |
231 |
DataTable dt = new DataTable(); |
232 |
Project_Info projectInfo = Project_Info.GetInstance(); |
233 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
234 |
{ |
235 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
236 |
{ |
237 |
try |
238 |
{ |
239 |
connection.Open(); |
240 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
241 |
{ |
242 |
cmd.CommandText = string.Format(@" |
243 |
SELECT s.UID, s.Name, st.Type, sp.IN_SYMBOL, sp.OUT_SYMBOL FROM {1} as st, {0} as s |
244 |
LEFT OUTER JOIN {2} as sp |
245 |
ON s.UID = SP.UID |
246 |
WHERE s.SymbolType_UID = st.UID |
247 |
AND (st.Type = 'Instrument OPC''s' OR st.Type = 'Piping OPC''s') |
248 |
ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_OPC_MAPPING_TABLE); |
249 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
250 |
dt.Load(dr); |
251 |
} |
252 |
connection.Close(); |
253 |
} |
254 |
catch (Exception ex) |
255 |
{ |
256 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
257 |
} |
258 |
finally |
259 |
{ |
260 |
connection.Dispose(); |
261 |
} |
262 |
} |
263 |
} |
264 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
265 |
{ |
266 |
using (SqlConnection connection = GetSqlConnection()) |
267 |
{ |
268 |
try |
269 |
{ |
270 |
if (connection != null && connection.State == ConnectionState.Open) |
271 |
{ |
272 |
using (SqlCommand cmd = connection.CreateCommand()) |
273 |
{ |
274 |
cmd.CommandText = string.Format(@" |
275 |
SELECT CONVERT(VARCHAR(255), s.UID) AS UID, s.Name, st.Type, sp.IN_SYMBOL, sp.OUT_SYMBOL FROM {1} as st, {0} as s |
276 |
LEFT OUTER JOIN {2} as sp |
277 |
ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID) |
278 |
WHERE s.SymbolType_UID = st.UID |
279 |
AND (st.Type = 'Instrument OPC''s' OR st.Type = 'Piping OPC''s') |
280 |
ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_OPC_MAPPING_TABLE); |
281 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
282 |
dt.Load(dr); |
283 |
} |
284 |
connection.Close(); |
285 |
} |
286 |
} |
287 |
catch (Exception ex) |
288 |
{ |
289 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
290 |
} |
291 |
finally |
292 |
{ |
293 |
if (connection != null) |
294 |
connection.Dispose(); |
295 |
} |
296 |
} |
297 |
} |
298 |
|
299 |
return dt; |
300 |
} |
301 |
public static DataTable GetLineMappingTable() |
302 |
{ |
303 |
DataTable dt = new DataTable(); |
304 |
Project_Info projectInfo = Project_Info.GetInstance(); |
305 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
306 |
{ |
307 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
308 |
{ |
309 |
try |
310 |
{ |
311 |
connection.Open(); |
312 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
313 |
{ |
314 |
cmd.CommandText = string.Format(@" |
315 |
SELECT l.UID, l.Name, sp.APID_SYMBOL, sp.DATA1 FROM {0} as l |
316 |
LEFT OUTER JOIN {1} as sp |
317 |
ON l.UID = SP.UID ;", LineTypes_TABLE, APID_SYMBOL_MAPPING_TABLE); |
318 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
319 |
dt.Load(dr); |
320 |
} |
321 |
connection.Close(); |
322 |
} |
323 |
catch (Exception ex) |
324 |
{ |
325 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
326 |
} |
327 |
finally |
328 |
{ |
329 |
connection.Dispose(); |
330 |
} |
331 |
} |
332 |
} |
333 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
334 |
{ |
335 |
using (SqlConnection connection = GetSqlConnection()) |
336 |
{ |
337 |
try |
338 |
{ |
339 |
if (connection != null && connection.State == ConnectionState.Open) |
340 |
{ |
341 |
using (SqlCommand cmd = connection.CreateCommand()) |
342 |
{ |
343 |
cmd.CommandText = string.Format(@" |
344 |
SELECT l.UID, l.Name, sp.APID_SYMBOL, sp.DATA1 FROM {0} as l |
345 |
LEFT OUTER JOIN {1} as sp |
346 |
ON l.UID = SP.UID ;", LineTypes_TABLE, APID_SYMBOL_MAPPING_TABLE); |
347 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
348 |
dt.Load(dr); |
349 |
} |
350 |
connection.Close(); |
351 |
} |
352 |
} |
353 |
catch (Exception ex) |
354 |
{ |
355 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
356 |
} |
357 |
finally |
358 |
{ |
359 |
if (connection != null) |
360 |
connection.Dispose(); |
361 |
} |
362 |
} |
363 |
} |
364 |
|
365 |
return dt; |
366 |
} |
367 |
public static DataTable SelectID2SymbolTable() |
368 |
{ |
369 |
DataTable dt = new DataTable(); |
370 |
Project_Info projectInfo = Project_Info.GetInstance(); |
371 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
372 |
{ |
373 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
374 |
{ |
375 |
try |
376 |
{ |
377 |
connection.Open(); |
378 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
379 |
{ |
380 |
cmd.CommandText = @"SELECT * FROM Symbol"; |
381 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
382 |
dt.Load(dr); |
383 |
} |
384 |
connection.Close(); |
385 |
} |
386 |
catch (Exception ex) |
387 |
{ |
388 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
389 |
} |
390 |
finally |
391 |
{ |
392 |
connection.Dispose(); |
393 |
} |
394 |
} |
395 |
} |
396 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
397 |
{ |
398 |
using (SqlConnection connection = GetSqlConnection()) |
399 |
{ |
400 |
try |
401 |
{ |
402 |
if (connection != null && connection.State == ConnectionState.Open) |
403 |
{ |
404 |
using (SqlCommand cmd = connection.CreateCommand()) |
405 |
{ |
406 |
cmd.CommandText = @"SELECT * FROM Symbol"; |
407 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
408 |
dt.Load(dr); |
409 |
} |
410 |
connection.Close(); |
411 |
} |
412 |
} |
413 |
catch (Exception ex) |
414 |
{ |
415 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
416 |
} |
417 |
finally |
418 |
{ |
419 |
if (connection != null) |
420 |
connection.Dispose(); |
421 |
} |
422 |
} |
423 |
} |
424 |
|
425 |
return dt; |
426 |
} |
427 |
public static DataTable SelectSymbolType() |
428 |
{ |
429 |
DataTable dt = new DataTable(); |
430 |
Project_Info projectInfo = Project_Info.GetInstance(); |
431 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
432 |
{ |
433 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
434 |
{ |
435 |
try |
436 |
{ |
437 |
connection.Open(); |
438 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
439 |
{ |
440 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE); |
441 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
442 |
dt.Load(dr); |
443 |
} |
444 |
connection.Close(); |
445 |
} |
446 |
catch (Exception ex) |
447 |
{ |
448 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
449 |
} |
450 |
finally |
451 |
{ |
452 |
connection.Dispose(); |
453 |
} |
454 |
} |
455 |
} |
456 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
457 |
{ |
458 |
using (SqlConnection connection = GetSqlConnection()) |
459 |
{ |
460 |
try |
461 |
{ |
462 |
if (connection != null && connection.State == ConnectionState.Open) |
463 |
{ |
464 |
using (SqlCommand cmd = connection.CreateCommand()) |
465 |
{ |
466 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE); |
467 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
468 |
dt.Load(dr); |
469 |
} |
470 |
connection.Close(); |
471 |
} |
472 |
} |
473 |
catch (Exception ex) |
474 |
{ |
475 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
476 |
} |
477 |
finally |
478 |
{ |
479 |
if (connection != null) |
480 |
connection.Dispose(); |
481 |
} |
482 |
} |
483 |
} |
484 |
|
485 |
return dt; |
486 |
} |
487 |
public static DataTable SelectDrawings() |
488 |
{ |
489 |
DataTable dt = new DataTable(); |
490 |
Project_Info projectInfo = Project_Info.GetInstance(); |
491 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
492 |
{ |
493 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
494 |
{ |
495 |
try |
496 |
{ |
497 |
connection.Open(); |
498 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
499 |
{ |
500 |
cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings"); |
501 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
502 |
dt.Load(dr); |
503 |
} |
504 |
connection.Close(); |
505 |
} |
506 |
catch (Exception ex) |
507 |
{ |
508 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
509 |
} |
510 |
finally |
511 |
{ |
512 |
connection.Dispose(); |
513 |
} |
514 |
} |
515 |
} |
516 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
517 |
{ |
518 |
using (SqlConnection connection = GetSqlConnection()) |
519 |
{ |
520 |
try |
521 |
{ |
522 |
if (connection != null && connection.State == ConnectionState.Open) |
523 |
{ |
524 |
using (SqlCommand cmd = connection.CreateCommand()) |
525 |
{ |
526 |
cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings"); |
527 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
528 |
dt.Load(dr); |
529 |
} |
530 |
connection.Close(); |
531 |
} |
532 |
} |
533 |
catch (Exception ex) |
534 |
{ |
535 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
536 |
} |
537 |
finally |
538 |
{ |
539 |
if (connection != null) |
540 |
connection.Dispose(); |
541 |
} |
542 |
} |
543 |
} |
544 |
|
545 |
return dt; |
546 |
} |
547 |
public static bool InsertSymbolMapping(List<Tuple<string, string, string, string>> datas) |
548 |
{ |
549 |
Project_Info projectInfo = Project_Info.GetInstance(); |
550 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
551 |
{ |
552 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
553 |
{ |
554 |
try |
555 |
{ |
556 |
connection.Open(); |
557 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
558 |
{ |
559 |
try |
560 |
{ |
561 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
562 |
{ |
563 |
foreach (var item in datas) |
564 |
{ |
565 |
cmd.Parameters.Clear(); |
566 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, APID_SYMBOL, DATA1) VALUES (@UID, @NAME, @APID_SYMBOL, @DATA1)", APID_SYMBOL_MAPPING_TABLE); |
567 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
568 |
cmd.Parameters.AddWithValue("@NAME", item.Item2); |
569 |
cmd.Parameters.AddWithValue("@APID_SYMBOL", item.Item3); |
570 |
cmd.Parameters.AddWithValue("@DATA1", item.Item4); |
571 |
cmd.ExecuteNonQuery(); |
572 |
} |
573 |
} |
574 |
transaction.Commit(); |
575 |
connection.Close(); |
576 |
} |
577 |
catch (Exception ex) |
578 |
{ |
579 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
580 |
transaction.Rollback(); |
581 |
return false; |
582 |
} |
583 |
finally |
584 |
{ |
585 |
transaction.Dispose(); |
586 |
} |
587 |
} |
588 |
} |
589 |
catch (Exception ex) |
590 |
{ |
591 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
592 |
return false; |
593 |
} |
594 |
finally |
595 |
{ |
596 |
connection.Dispose(); |
597 |
} |
598 |
} |
599 |
} |
600 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
601 |
{ |
602 |
using (SqlConnection connection = GetSqlConnection()) |
603 |
{ |
604 |
try |
605 |
{ |
606 |
if (connection != null && connection.State == ConnectionState.Open) |
607 |
{ |
608 |
using (SqlCommand cmd = connection.CreateCommand()) |
609 |
{ |
610 |
foreach (var item in datas) |
611 |
{ |
612 |
cmd.Parameters.Clear(); |
613 |
cmd.CommandText = string.Format(@" |
614 |
IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}') |
615 |
UPDATE {0} SET NAME = @NAME, APID_SYMBOL = @APID_SYMBOL, DATA1 = @DATA1 WHERE UID = @UID |
616 |
ELSE |
617 |
INSERT INTO {0} (UID, NAME, APID_SYMBOL, DATA1) VALUES (@UID, @NAME, @APID_SYMBOL, @DATA1)", APID_SYMBOL_MAPPING_TABLE, item.Item1); |
618 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
619 |
if (string.IsNullOrEmpty(item.Item2)) |
620 |
cmd.Parameters.AddWithValue("@NAME", DBNull.Value); |
621 |
else |
622 |
cmd.Parameters.AddWithValue("@NAME", item.Item2); |
623 |
if (string.IsNullOrEmpty(item.Item3)) |
624 |
cmd.Parameters.AddWithValue("@APID_SYMBOL", DBNull.Value); |
625 |
else |
626 |
cmd.Parameters.AddWithValue("@APID_SYMBOL", item.Item3); |
627 |
if (string.IsNullOrEmpty(item.Item4)) |
628 |
cmd.Parameters.AddWithValue("@DATA1", DBNull.Value); |
629 |
else |
630 |
cmd.Parameters.AddWithValue("@DATA1", item.Item4); |
631 |
|
632 |
cmd.ExecuteNonQuery(); |
633 |
} |
634 |
} |
635 |
connection.Close(); |
636 |
} |
637 |
} |
638 |
catch (Exception ex) |
639 |
{ |
640 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
641 |
return false; |
642 |
} |
643 |
finally |
644 |
{ |
645 |
if (connection != null) |
646 |
connection.Dispose(); |
647 |
} |
648 |
} |
649 |
} |
650 |
|
651 |
return true; |
652 |
} |
653 |
public static bool InsertOPCMapping(List<Tuple<string, string, string>> datas) |
654 |
{ |
655 |
Project_Info projectInfo = Project_Info.GetInstance(); |
656 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
657 |
{ |
658 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
659 |
{ |
660 |
try |
661 |
{ |
662 |
connection.Open(); |
663 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
664 |
{ |
665 |
try |
666 |
{ |
667 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
668 |
{ |
669 |
foreach (var item in datas) |
670 |
{ |
671 |
cmd.Parameters.Clear(); |
672 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, IN_SYMBOL, OUT_SYMBOL) VALUES (@UID, @IN_SYMBOL, @OUT_SYMBOL)", APID_OPC_MAPPING_TABLE); |
673 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
674 |
cmd.Parameters.AddWithValue("@IN_SYMBOL", item.Item2); |
675 |
cmd.Parameters.AddWithValue("@OUT_SYMBOL", item.Item3); |
676 |
cmd.ExecuteNonQuery(); |
677 |
} |
678 |
} |
679 |
transaction.Commit(); |
680 |
connection.Close(); |
681 |
} |
682 |
catch (Exception ex) |
683 |
{ |
684 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
685 |
transaction.Rollback(); |
686 |
return false; |
687 |
} |
688 |
finally |
689 |
{ |
690 |
transaction.Dispose(); |
691 |
} |
692 |
} |
693 |
} |
694 |
catch (Exception ex) |
695 |
{ |
696 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
697 |
return false; |
698 |
} |
699 |
finally |
700 |
{ |
701 |
connection.Dispose(); |
702 |
} |
703 |
} |
704 |
} |
705 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
706 |
{ |
707 |
using (SqlConnection connection = GetSqlConnection()) |
708 |
{ |
709 |
try |
710 |
{ |
711 |
if (connection != null && connection.State == ConnectionState.Open) |
712 |
{ |
713 |
using (SqlCommand cmd = connection.CreateCommand()) |
714 |
{ |
715 |
foreach (var item in datas) |
716 |
{ |
717 |
cmd.Parameters.Clear(); |
718 |
cmd.CommandText = string.Format(@" |
719 |
IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}') |
720 |
UPDATE {0} SET IN_SYMBOL = @IN_SYMBOL, OUT_SYMBOL = @OUT_SYMBOL WHERE UID = @UID |
721 |
ELSE |
722 |
INSERT INTO {0} (UID, IN_SYMBOL, OUT_SYMBOL) VALUES (@UID, @IN_SYMBOL, @OUT_SYMBOL)", APID_OPC_MAPPING_TABLE, item.Item1); |
723 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
724 |
if (string.IsNullOrEmpty(item.Item2)) |
725 |
cmd.Parameters.AddWithValue("@IN_SYMBOL", DBNull.Value); |
726 |
else |
727 |
cmd.Parameters.AddWithValue("@IN_SYMBOL", item.Item2); |
728 |
if (string.IsNullOrEmpty(item.Item3)) |
729 |
cmd.Parameters.AddWithValue("@OUT_SYMBOL", DBNull.Value); |
730 |
else |
731 |
cmd.Parameters.AddWithValue("@OUT_SYMBOL", item.Item3); |
732 |
|
733 |
cmd.ExecuteNonQuery(); |
734 |
} |
735 |
} |
736 |
connection.Close(); |
737 |
} |
738 |
} |
739 |
catch (Exception ex) |
740 |
{ |
741 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
742 |
return false; |
743 |
} |
744 |
finally |
745 |
{ |
746 |
if (connection != null) |
747 |
connection.Dispose(); |
748 |
} |
749 |
} |
750 |
} |
751 |
|
752 |
return true; |
753 |
} |
754 |
#endregion |
755 |
|
756 |
#region AVEVA |
757 |
public static string GetAvevaConnectionString() |
758 |
{ |
759 |
string strConn = string.Empty; |
760 |
if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
761 |
{ |
762 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName); |
763 |
} |
764 |
else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
765 |
{ |
766 |
string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
767 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString); |
768 |
} |
769 |
|
770 |
return strConn; |
771 |
} |
772 |
public static string GetAvevaConnectionString_Reports() |
773 |
{ |
774 |
string strConn = string.Empty; |
775 |
if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
776 |
{ |
777 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName); |
778 |
} |
779 |
else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
780 |
{ |
781 |
string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
782 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString); |
783 |
} |
784 |
|
785 |
return strConn; |
786 |
} |
787 |
public static string GetAvevaConnectionString_Admin() |
788 |
{ |
789 |
string strConn = string.Empty; |
790 |
if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
791 |
{ |
792 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName); |
793 |
} |
794 |
else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
795 |
{ |
796 |
string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
797 |
strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString); |
798 |
} |
799 |
|
800 |
return strConn; |
801 |
} |
802 |
public static string GetDirectiveValue(string name) |
803 |
{ |
804 |
string result = null; |
805 |
|
806 |
using (SqlConnection connection = new SqlConnection()) |
807 |
{ |
808 |
connection.ConnectionString = GetAvevaConnectionString_Admin(); |
809 |
connection.Open(); |
810 |
using (SqlCommand cmd = connection.CreateCommand()) |
811 |
{ |
812 |
cmd.CommandText = string.Format("select DirectiveValue from ProjectDirectives where DirectiveName = '{0}'", name); |
813 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
814 |
{ |
815 |
if (dr.Read()) |
816 |
result = dr.GetString(0); |
817 |
} |
818 |
} |
819 |
connection.Close(); |
820 |
} |
821 |
|
822 |
return result; |
823 |
} |
824 |
public static DataTable GetDrawingTemplate() |
825 |
{ |
826 |
DataTable dt = new DataTable(); |
827 |
|
828 |
using (SqlConnection connection = new SqlConnection()) |
829 |
{ |
830 |
connection.ConnectionString = GetAvevaConnectionString_Reports(); |
831 |
connection.Open(); |
832 |
using (SqlCommand cmd = connection.CreateCommand()) |
833 |
{ |
834 |
cmd.CommandText = "select ID, Name,IsDWG,DateCreation,MachineName,FutureUse1,FutureUse2 from DrawingTemplates where IsDWG=0"; |
835 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
836 |
dt.Load(dr); |
837 |
} |
838 |
connection.Close(); |
839 |
} |
840 |
|
841 |
return dt; |
842 |
} |
843 |
public static DataTable SelectStandardSymbolTable() |
844 |
{ |
845 |
DataTable dt = new DataTable(); |
846 |
|
847 |
using (SqlConnection connection = new SqlConnection()) |
848 |
{ |
849 |
connection.ConnectionString = GetAvevaConnectionString_Admin(); |
850 |
connection.Open(); |
851 |
using (SqlCommand cmd = connection.CreateCommand()) |
852 |
{ |
853 |
cmd.CommandText = string.Format(@" |
854 |
SELECT ss.ID, ss.Symbol_Name, sps.Relative_Path FROM {0} as ss |
855 |
LEFT OUTER JOIN {1} as sps |
856 |
ON ss.Relative_Path = sps.ID |
857 |
ORDER BY sps.ID ASC |
858 |
;", "stdSymbols", "StdPrjFolStructure"); |
859 |
|
860 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
861 |
dt.Load(dr); |
862 |
} |
863 |
connection.Close(); |
864 |
} |
865 |
|
866 |
return dt; |
867 |
} |
868 |
public static DataTable SelectStandardSymbolStructureTable() |
869 |
{ |
870 |
DataTable dt = new DataTable(); |
871 |
|
872 |
using (SqlConnection connection = new SqlConnection()) |
873 |
{ |
874 |
connection.ConnectionString = GetAvevaConnectionString_Admin(); |
875 |
connection.Open(); |
876 |
using (SqlCommand cmd = connection.CreateCommand()) |
877 |
{ |
878 |
cmd.CommandText = string.Format(@" |
879 |
SELECT * FROM {0} |
880 |
;", "StdPrjFolStructure"); |
881 |
|
882 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
883 |
dt.Load(dr); |
884 |
} |
885 |
connection.Close(); |
886 |
} |
887 |
|
888 |
return dt; |
889 |
} |
890 |
public static DataTable SelectStandardSymbolImageTable() |
891 |
{ |
892 |
DataTable dt = new DataTable(); |
893 |
|
894 |
using (SqlConnection connection = new SqlConnection()) |
895 |
{ |
896 |
connection.ConnectionString = GetAvevaConnectionString_Admin(); |
897 |
connection.Open(); |
898 |
using (SqlCommand cmd = connection.CreateCommand()) |
899 |
{ |
900 |
cmd.CommandText = string.Format(@" |
901 |
SELECT ss.Symbol_Name, ss.Pic_Binary, sps.Relative_Path FROM {0} as ss |
902 |
LEFT OUTER JOIN {1} as sps |
903 |
ON ss.Relative_Path = sps.ID |
904 |
ORDER BY sps.ID ASC |
905 |
;", "stdSymbols", "StdPrjFolStructure"); |
906 |
|
907 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
908 |
dt.Load(dr); |
909 |
} |
910 |
connection.Close(); |
911 |
} |
912 |
|
913 |
return dt; |
914 |
} |
915 |
public static DataTable SelectDrawingTable() |
916 |
{ |
917 |
DataTable dt = new DataTable(); |
918 |
|
919 |
using (SqlConnection connection = new SqlConnection()) |
920 |
{ |
921 |
connection.ConnectionString = GetAvevaConnectionString_Reports(); |
922 |
connection.Open(); |
923 |
using (SqlCommand cmd = connection.CreateCommand()) |
924 |
{ |
925 |
cmd.CommandText = string.Format(@" |
926 |
SELECT DrawingId, XDNLABEL, XDSLABEL FROM {0} |
927 |
;", "SynchroniseDetails"); |
928 |
|
929 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
930 |
dt.Load(dr); |
931 |
} |
932 |
connection.Close(); |
933 |
} |
934 |
|
935 |
return dt; |
936 |
} |
937 |
public static DataTable SelectSymbolTable() |
938 |
{ |
939 |
DataTable dt = new DataTable(); |
940 |
|
941 |
using (SqlConnection connection = new SqlConnection()) |
942 |
{ |
943 |
connection.ConnectionString = GetAvevaConnectionString_Admin(); |
944 |
connection.Open(); |
945 |
using (SqlCommand cmd = connection.CreateCommand()) |
946 |
{ |
947 |
cmd.CommandText = string.Format(@" |
948 |
SELECT * FROM {0} |
949 |
;", "SYMBOLS"); |
950 |
|
951 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
952 |
dt.Load(dr); |
953 |
} |
954 |
connection.Close(); |
955 |
} |
956 |
|
957 |
return dt; |
958 |
} |
959 |
#endregion |
960 |
|
961 |
} |
962 |
} |