hytos / DTI_PID / APIDConverter / DB / Project_DB.cs @ ecd39237
이력 | 보기 | 이력해설 | 다운로드 (36.7 KB)
1 | 53a4ebb8 | gaqhf | 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 | a77303ca | gaqhf | const string APID_SYMBOL_MAPPING_TABLE = "T_APID_SYMBOL_MAPPING"; |
20 | a03cde1e | gaqhf | const string APID_OPC_MAPPING_TABLE = "T_APID_OPC_MAPPING"; |
21 | 53a4ebb8 | gaqhf | |
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 | 9ee9b61d | gaqhf | #region Only ID2 DB |
30 | 53a4ebb8 | gaqhf | 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 | 465c8b6e | gaqhf | Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
49 | 53a4ebb8 | gaqhf | 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 | a77303ca | gaqhf | 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 | 9dab7146 | gaqhf | cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, APID_SYMBOL TEXT, DATA1 TEXT)", APID_SYMBOL_MAPPING_TABLE); |
80 | a77303ca | gaqhf | cmd.ExecuteNonQuery(); |
81 | } |
||
82 | a03cde1e | gaqhf | if (dt.Select(string.Format("NAME = '{0}'", APID_OPC_MAPPING_TABLE)).Length == 0) |
83 | { |
||
84 | dd624cbd | gaqhf | cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, IN_SYMBOL TEXT, OUT_SYMBOL TEXT, FlowType INT)", APID_OPC_MAPPING_TABLE); |
85 | a03cde1e | gaqhf | cmd.ExecuteNonQuery(); |
86 | } |
||
87 | a77303ca | gaqhf | } |
88 | 53a4ebb8 | gaqhf | } |
89 | result = true; |
||
90 | } |
||
91 | connection.Close(); |
||
92 | } |
||
93 | catch (Exception ex) |
||
94 | { |
||
95 | 465c8b6e | gaqhf | Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
96 | 53a4ebb8 | gaqhf | } |
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 | a77303ca | gaqhf | 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 | 9dab7146 | gaqhf | 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 | a77303ca | gaqhf | cmd.ExecuteNonQuery(); |
123 | } |
||
124 | a03cde1e | gaqhf | if (dt.Select(string.Format("NAME = '{0}'", APID_OPC_MAPPING_TABLE)).Length == 0) |
125 | { |
||
126 | dd624cbd | gaqhf | cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, IN_SYMBOL varchar(MAX), OUT_SYMBOL varchar(MAX), FlowType INT)", APID_OPC_MAPPING_TABLE); |
127 | a03cde1e | gaqhf | cmd.ExecuteNonQuery(); |
128 | } |
||
129 | a77303ca | gaqhf | } |
130 | 53a4ebb8 | gaqhf | } |
131 | result = true; |
||
132 | } |
||
133 | } |
||
134 | catch (Exception ex) |
||
135 | { |
||
136 | 465c8b6e | gaqhf | Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
137 | 53a4ebb8 | gaqhf | } |
138 | finally |
||
139 | { |
||
140 | if (connection != null) |
||
141 | connection.Dispose(); |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | |||
146 | return result; |
||
147 | } |
||
148 | a77303ca | gaqhf | 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 | 9dab7146 | gaqhf | SELECT s.UID, s.Name, st.Type, sp.APID_SYMBOL, sp.DATA1 FROM {1} as st, {0} as s |
163 | a77303ca | gaqhf | LEFT OUTER JOIN {2} as sp |
164 | ON s.UID = SP.UID |
||
165 | WHERE s.SymbolType_UID = st.UID |
||
166 | 4aba6868 | gaqhf | AND st.Type != 'Instrument OPC''s' AND st.Type != 'Piping OPC''s' |
167 | a77303ca | gaqhf | 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 | 9dab7146 | gaqhf | 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 | a77303ca | gaqhf | 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 | 4aba6868 | gaqhf | AND st.Type != 'Instrument OPC''s' AND st.Type != 'Piping OPC''s' |
208 | a77303ca | gaqhf | 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 | dd624cbd | gaqhf | 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, sp.FlowType 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, sp.FlowType 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 | 9ee9b61d | gaqhf | 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 | 9dab7146 | gaqhf | SELECT l.UID, l.Name, sp.APID_SYMBOL, sp.DATA1 FROM {0} as l |
316 | 9ee9b61d | gaqhf | 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 | 9dab7146 | gaqhf | SELECT l.UID, l.Name, sp.APID_SYMBOL, sp.DATA1 FROM {0} as l |
345 | 9ee9b61d | gaqhf | 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 | 465c8b6e | gaqhf | |
365 | 9ee9b61d | gaqhf | return dt; |
366 | } |
||
367 | 465c8b6e | gaqhf | 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 | d327a608 | gaqhf | 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 | 465c8b6e | gaqhf | |
485 | d327a608 | gaqhf | return dt; |
486 | } |
||
487 | c7db500b | gaqhf | 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 | 9dab7146 | gaqhf | public static bool InsertSymbolMapping(List<Tuple<string, string, string, string>> datas) |
548 | c8da68ce | gaqhf | { |
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 | 9dab7146 | gaqhf | 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 | c8da68ce | gaqhf | cmd.Parameters.AddWithValue("@UID", item.Item1); |
568 | cmd.Parameters.AddWithValue("@NAME", item.Item2); |
||
569 | cmd.Parameters.AddWithValue("@APID_SYMBOL", item.Item3); |
||
570 | 9dab7146 | gaqhf | cmd.Parameters.AddWithValue("@DATA1", item.Item4); |
571 | c8da68ce | gaqhf | 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 | 9dab7146 | gaqhf | UPDATE {0} SET NAME = @NAME, APID_SYMBOL = @APID_SYMBOL, DATA1 = @DATA1 WHERE UID = @UID |
616 | c8da68ce | gaqhf | ELSE |
617 | 9dab7146 | gaqhf | INSERT INTO {0} (UID, NAME, APID_SYMBOL, DATA1) VALUES (@UID, @NAME, @APID_SYMBOL, @DATA1)", APID_SYMBOL_MAPPING_TABLE, item.Item1); |
618 | c8da68ce | gaqhf | 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 | 9dab7146 | gaqhf | if (string.IsNullOrEmpty(item.Item4)) |
628 | cmd.Parameters.AddWithValue("@DATA1", DBNull.Value); |
||
629 | else |
||
630 | cmd.Parameters.AddWithValue("@DATA1", item.Item4); |
||
631 | |||
632 | c8da68ce | gaqhf | 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 | a77303ca | gaqhf | #endregion |
654 | e9ed6b7b | gaqhf | |
655 | #region AVEVA |
||
656 | 534ca9c5 | gaqhf | public static string GetAvevaConnectionString() |
657 | e9ed6b7b | gaqhf | { |
658 | string strConn = string.Empty; |
||
659 | if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
||
660 | { |
||
661 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName); |
||
662 | } |
||
663 | else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
||
664 | { |
||
665 | string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
||
666 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString); |
||
667 | } |
||
668 | |||
669 | return strConn; |
||
670 | } |
||
671 | 534ca9c5 | gaqhf | public static string GetAvevaConnectionString_Reports() |
672 | e9ed6b7b | gaqhf | { |
673 | string strConn = string.Empty; |
||
674 | if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
||
675 | { |
||
676 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName); |
||
677 | } |
||
678 | else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
||
679 | { |
||
680 | string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
||
681 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString); |
||
682 | } |
||
683 | |||
684 | return strConn; |
||
685 | } |
||
686 | 534ca9c5 | gaqhf | public static string GetAvevaConnectionString_Admin() |
687 | e9ed6b7b | gaqhf | { |
688 | string strConn = string.Empty; |
||
689 | if (Utilities.strSQLWinAuthentication.ToUpper() == "YES") |
||
690 | { |
||
691 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName); |
||
692 | } |
||
693 | else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO") |
||
694 | { |
||
695 | string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword; |
||
696 | strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString); |
||
697 | } |
||
698 | |||
699 | return strConn; |
||
700 | } |
||
701 | 59184d1b | gaqhf | public static string GetDirectiveValue(string name) |
702 | { |
||
703 | string result = null; |
||
704 | |||
705 | using (SqlConnection connection = new SqlConnection()) |
||
706 | { |
||
707 | connection.ConnectionString = GetAvevaConnectionString_Admin(); |
||
708 | connection.Open(); |
||
709 | using (SqlCommand cmd = connection.CreateCommand()) |
||
710 | { |
||
711 | cmd.CommandText = string.Format("select DirectiveValue from ProjectDirectives where DirectiveName = '{0}'", name); |
||
712 | using (SqlDataReader dr = cmd.ExecuteReader()) |
||
713 | { |
||
714 | if (dr.Read()) |
||
715 | result = dr.GetString(0); |
||
716 | } |
||
717 | } |
||
718 | connection.Close(); |
||
719 | } |
||
720 | |||
721 | return result; |
||
722 | } |
||
723 | public static DataTable GetDrawingTemplate() |
||
724 | { |
||
725 | DataTable dt = new DataTable(); |
||
726 | |||
727 | using (SqlConnection connection = new SqlConnection()) |
||
728 | { |
||
729 | connection.ConnectionString = GetAvevaConnectionString_Reports(); |
||
730 | connection.Open(); |
||
731 | using (SqlCommand cmd = connection.CreateCommand()) |
||
732 | { |
||
733 | cmd.CommandText = "select ID, Name,IsDWG,DateCreation,MachineName,FutureUse1,FutureUse2 from DrawingTemplates where IsDWG=0"; |
||
734 | using (SqlDataReader dr = cmd.ExecuteReader()) |
||
735 | dt.Load(dr); |
||
736 | } |
||
737 | connection.Close(); |
||
738 | } |
||
739 | |||
740 | return dt; |
||
741 | } |
||
742 | 56cd4954 | gaqhf | public static DataTable SelectStandardSymbolTable() |
743 | e9ed6b7b | gaqhf | { |
744 | DataTable dt = new DataTable(); |
||
745 | |||
746 | using (SqlConnection connection = new SqlConnection()) |
||
747 | { |
||
748 | connection.ConnectionString = GetAvevaConnectionString_Admin(); |
||
749 | connection.Open(); |
||
750 | using (SqlCommand cmd = connection.CreateCommand()) |
||
751 | { |
||
752 | 56cd4954 | gaqhf | cmd.CommandText = string.Format(@" |
753 | SELECT ss.ID, ss.Symbol_Name, sps.Relative_Path FROM {0} as ss |
||
754 | LEFT OUTER JOIN {1} as sps |
||
755 | ON ss.Relative_Path = sps.ID |
||
756 | ORDER BY sps.ID ASC |
||
757 | ;", "stdSymbols", "StdPrjFolStructure"); |
||
758 | |||
759 | using (SqlDataReader dr = cmd.ExecuteReader()) |
||
760 | dt.Load(dr); |
||
761 | } |
||
762 | connection.Close(); |
||
763 | } |
||
764 | |||
765 | return dt; |
||
766 | } |
||
767 | public static DataTable SelectStandardSymbolStructureTable() |
||
768 | { |
||
769 | DataTable dt = new DataTable(); |
||
770 | |||
771 | using (SqlConnection connection = new SqlConnection()) |
||
772 | { |
||
773 | connection.ConnectionString = GetAvevaConnectionString_Admin(); |
||
774 | connection.Open(); |
||
775 | using (SqlCommand cmd = connection.CreateCommand()) |
||
776 | { |
||
777 | cmd.CommandText = string.Format(@" |
||
778 | SELECT * FROM {0} |
||
779 | ;", "StdPrjFolStructure"); |
||
780 | |||
781 | using (SqlDataReader dr = cmd.ExecuteReader()) |
||
782 | dt.Load(dr); |
||
783 | } |
||
784 | connection.Close(); |
||
785 | } |
||
786 | |||
787 | return dt; |
||
788 | } |
||
789 | public static DataTable SelectStandardSymbolImageTable() |
||
790 | { |
||
791 | DataTable dt = new DataTable(); |
||
792 | |||
793 | using (SqlConnection connection = new SqlConnection()) |
||
794 | { |
||
795 | connection.ConnectionString = GetAvevaConnectionString_Admin(); |
||
796 | connection.Open(); |
||
797 | using (SqlCommand cmd = connection.CreateCommand()) |
||
798 | { |
||
799 | cmd.CommandText = string.Format(@" |
||
800 | SELECT ss.Symbol_Name, ss.Pic_Binary, sps.Relative_Path FROM {0} as ss |
||
801 | LEFT OUTER JOIN {1} as sps |
||
802 | ON ss.Relative_Path = sps.ID |
||
803 | ORDER BY sps.ID ASC |
||
804 | ;", "stdSymbols", "StdPrjFolStructure"); |
||
805 | |||
806 | e9ed6b7b | gaqhf | using (SqlDataReader dr = cmd.ExecuteReader()) |
807 | dt.Load(dr); |
||
808 | } |
||
809 | connection.Close(); |
||
810 | } |
||
811 | |||
812 | return dt; |
||
813 | } |
||
814 | 128c844f | gaqhf | public static DataTable SelectDrawingTable() |
815 | { |
||
816 | DataTable dt = new DataTable(); |
||
817 | e9ed6b7b | gaqhf | |
818 | 128c844f | gaqhf | using (SqlConnection connection = new SqlConnection()) |
819 | { |
||
820 | connection.ConnectionString = GetAvevaConnectionString_Reports(); |
||
821 | connection.Open(); |
||
822 | using (SqlCommand cmd = connection.CreateCommand()) |
||
823 | { |
||
824 | cmd.CommandText = string.Format(@" |
||
825 | SELECT DrawingId, XDNLABEL, XDSLABEL FROM {0} |
||
826 | ;", "SynchroniseDetails"); |
||
827 | |||
828 | using (SqlDataReader dr = cmd.ExecuteReader()) |
||
829 | dt.Load(dr); |
||
830 | } |
||
831 | connection.Close(); |
||
832 | } |
||
833 | |||
834 | return dt; |
||
835 | } |
||
836 | 495bb8f5 | gaqhf | public static DataTable SelectSymbolTable() |
837 | { |
||
838 | DataTable dt = new DataTable(); |
||
839 | |||
840 | using (SqlConnection connection = new SqlConnection()) |
||
841 | { |
||
842 | connection.ConnectionString = GetAvevaConnectionString_Admin(); |
||
843 | connection.Open(); |
||
844 | using (SqlCommand cmd = connection.CreateCommand()) |
||
845 | { |
||
846 | cmd.CommandText = string.Format(@" |
||
847 | SELECT * FROM {0} |
||
848 | ;", "SYMBOLS"); |
||
849 | |||
850 | using (SqlDataReader dr = cmd.ExecuteReader()) |
||
851 | dt.Load(dr); |
||
852 | } |
||
853 | connection.Close(); |
||
854 | } |
||
855 | |||
856 | return dt; |
||
857 | } |
||
858 | e9ed6b7b | gaqhf | #endregion |
859 | |||
860 | 53a4ebb8 | gaqhf | } |
861 | } |