프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / DB / Project_DB.cs @ 7a56b228

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