프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / DB / Project_DB.cs @ e75b5c63

이력 | 보기 | 이력해설 | 다운로드 (70.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 7a56b228 gaqhf
        const string APID_ATTRIBUTE_MAPPING_TABLE = "T_APID_ATTRIBUTE_MAPPING";
22 faba1fc7 gaqhf
        const string APID_ATTRIBUTE_TABLE = "T_APID_ATTRIBUTE";
23 53a4ebb8 gaqhf
24
        const string LineProperties_TABLE = "LineProperties";
25
        const string LineTypes_TABLE = "LineTypes";
26
        const string SymbolType_TABLE = "SymbolType";
27
        const string SymbolAttribute_TABLE = "SymbolAttribute";
28
        const string Symbol_TABLE = "Symbol";
29
        const string OPCRelations_TABLE = "OPCRelations";
30
31 9ee9b61d gaqhf
        #region Only ID2 DB
32 53a4ebb8 gaqhf
        private static SqlConnection GetSqlConnection()
33
        {
34
            Project_Info projectInfo = Project_Info.GetInstance();
35
            SqlConnection connection = null;
36
            try
37
            {
38
                connection = new SqlConnection(string.Format(CultureInfo.CurrentCulture,
39
                    @"Data Source = {0},{1}; Initial CataLog = {2}; User ID = {3}; Password = {4};",
40
                    projectInfo.ServerIP,
41
                    projectInfo.Port,
42
                    System.IO.Path.GetFileName(projectInfo.DefaultPath),
43
                    projectInfo.DBUser,
44
                    projectInfo.DBPassword));
45
46
                connection.Open();
47
            }
48
            catch (Exception ex)
49
            {
50 465c8b6e gaqhf
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
51 53a4ebb8 gaqhf
                if (connection != null)
52
                    connection.Dispose();
53
                connection = null;
54
            }
55
56
            return connection;
57
        }
58
        public static bool ConnTestAndCreateTable()
59
        {
60
            bool result = false;
61
            Project_Info projectInfo = Project_Info.GetInstance();
62
            if (projectInfo.DBType == ID2DB_Type.SQLite)
63
            {
64
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath)))
65
                {
66
                    try
67
                    {
68
                        connection.Open();
69
                        if (connection.State == ConnectionState.Open)
70
                        {
71
                            using (SQLiteCommand cmd = connection.CreateCommand())
72
                            {
73 a77303ca gaqhf
                                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
74
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
75
                                using (DataTable dt = new DataTable())
76
                                {
77
                                    dt.Load(dr);
78
79
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_SYMBOL_MAPPING_TABLE)).Length == 0)
80
                                    {
81 9dab7146 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, APID_SYMBOL TEXT, DATA1 TEXT)", APID_SYMBOL_MAPPING_TABLE);
82 a77303ca gaqhf
                                        cmd.ExecuteNonQuery();
83
                                    }
84 a03cde1e gaqhf
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_OPC_MAPPING_TABLE)).Length == 0)
85
                                    {
86 0c281fe6 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, IN_SYMBOL TEXT, OUT_SYMBOL TEXT)", APID_OPC_MAPPING_TABLE);
87 a03cde1e gaqhf
                                        cmd.ExecuteNonQuery();
88
                                    }
89 7a56b228 gaqhf
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
90
                                    {
91 faba1fc7 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, APID_ATTRIBUTE TEXT, APID_ATTRIBUTE_TYPE TEXT)", APID_ATTRIBUTE_MAPPING_TABLE);
92
                                        cmd.ExecuteNonQuery();
93
                                    }
94
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_ATTRIBUTE_TABLE)).Length == 0)
95
                                    {
96 ad983925 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (LEVEL1 TEXT, LEVEL2 TEXT, ATTRIBUTE_KEY TEXT, ATTRIBUTE_NAME TEXT)", APID_ATTRIBUTE_TABLE);
97 7a56b228 gaqhf
                                        cmd.ExecuteNonQuery();
98
                                    }
99 a77303ca gaqhf
                                }
100 53a4ebb8 gaqhf
                            }
101
                            result = true;
102
                        }
103
                        connection.Close();
104
                    }
105
                    catch (Exception ex)
106
                    {
107 465c8b6e gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
108 53a4ebb8 gaqhf
                    }
109
                    finally
110
                    {
111
                        connection.Dispose();
112
                    }
113
                }
114
            }
115
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
116
            {
117
                using (SqlConnection connection = GetSqlConnection())
118
                {
119
                    try
120
                    {
121
                        if (connection != null && connection.State == ConnectionState.Open)
122
                        {
123
                            using (SqlCommand cmd = connection.CreateCommand())
124
                            {
125 a77303ca gaqhf
                                cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES";
126
                                using (SqlDataReader dr = cmd.ExecuteReader())
127
                                using (DataTable dt = new DataTable())
128
                                {
129
                                    dt.Load(dr);
130
131
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_SYMBOL_MAPPING_TABLE)).Length == 0)
132
                                    {
133 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);
134 a77303ca gaqhf
                                        cmd.ExecuteNonQuery();
135
                                    }
136 a03cde1e gaqhf
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_OPC_MAPPING_TABLE)).Length == 0)
137
                                    {
138 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);
139 a03cde1e gaqhf
                                        cmd.ExecuteNonQuery();
140
                                    }
141 7a56b228 gaqhf
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
142
                                    {
143
                                        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);
144
                                        cmd.ExecuteNonQuery();
145
                                    }
146 faba1fc7 gaqhf
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_ATTRIBUTE_TABLE)).Length == 0)
147
                                    {
148
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (LEVEL1 varchar(255), LEVEL2 varchar(255), ATTRIBUTE_KEY varchar(255), ATTRIBUTE_NAME varchar(255))", APID_ATTRIBUTE_TABLE);
149
                                        cmd.ExecuteNonQuery();
150
                                    }
151 a77303ca gaqhf
                                }
152 53a4ebb8 gaqhf
                            }
153
                            result = true;
154
                        }
155
                    }
156
                    catch (Exception ex)
157
                    {
158 465c8b6e gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
159 53a4ebb8 gaqhf
                    }
160
                    finally
161
                    {
162
                        if (connection != null)
163
                            connection.Dispose();
164
                    }
165
                }
166
            }
167
168
            return result;
169
        }
170 a77303ca gaqhf
        public static DataTable GetSymbolMappingTable()
171
        {
172
            DataTable dt = new DataTable();
173
            Project_Info projectInfo = Project_Info.GetInstance();
174
            if (projectInfo.DBType == ID2DB_Type.SQLite)
175
            {
176
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
177
                {
178
                    try
179
                    {
180
                        connection.Open();
181
                        using (SQLiteCommand cmd = connection.CreateCommand())
182
                        {
183
                            cmd.CommandText = string.Format(@"
184 9dab7146 gaqhf
                            SELECT s.UID, s.Name, st.Type, sp.APID_SYMBOL, sp.DATA1 FROM {1} as st, {0} as s 
185 a77303ca gaqhf
                                LEFT OUTER JOIN {2} as sp 
186
                                    ON s.UID = SP.UID 
187
                            WHERE s.SymbolType_UID = st.UID 
188 4aba6868 gaqhf
                            AND st.Type != 'Instrument OPC''s' AND st.Type != 'Piping OPC''s' 
189 a77303ca gaqhf
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_SYMBOL_MAPPING_TABLE);
190
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
191
                                dt.Load(dr);
192
193
                            DataTable dtClone = dt.Clone();
194
                            dtClone.Columns["UID"].DataType = typeof(string);
195
                            foreach (DataRow row in dt.Rows)
196
                            {
197
                                dtClone.ImportRow(row);
198
                            }
199
                            dt.Dispose();
200
                            dt = dtClone;
201
                        }
202
                        connection.Close();
203
                    }
204
                    catch (Exception ex)
205
                    {
206
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
207
                    }
208
                    finally
209
                    {
210
                        connection.Dispose();
211
                    }
212
                }
213
            }
214
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
215
            {
216
                using (SqlConnection connection = GetSqlConnection())
217
                {
218
                    try
219
                    {
220
                        if (connection != null && connection.State == ConnectionState.Open)
221
                        {
222
                            using (SqlCommand cmd = connection.CreateCommand())
223
                            {
224
                                cmd.CommandText = string.Format(@"
225 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 
226 a77303ca gaqhf
                                LEFT OUTER JOIN {2} as sp 
227
                                    ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID)
228
                            WHERE s.SymbolType_UID = st.UID 
229 4aba6868 gaqhf
                            AND st.Type != 'Instrument OPC''s' AND st.Type != 'Piping OPC''s' 
230 a77303ca gaqhf
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_SYMBOL_MAPPING_TABLE);
231
                                using (SqlDataReader dr = cmd.ExecuteReader())
232
                                    dt.Load(dr);
233
                            }
234
                            connection.Close();
235
                        }
236
                    }
237
                    catch (Exception ex)
238
                    {
239
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
240
                    }
241
                    finally
242
                    {
243
                        if (connection != null)
244
                            connection.Dispose();
245
                    }
246
                }
247
            }
248
249
            return dt;
250
        }
251 dd624cbd gaqhf
        public static DataTable GetOPCMappingTable()
252
        {
253
            DataTable dt = new DataTable();
254
            Project_Info projectInfo = Project_Info.GetInstance();
255
            if (projectInfo.DBType == ID2DB_Type.SQLite)
256
            {
257
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
258
                {
259
                    try
260
                    {
261
                        connection.Open();
262
                        using (SQLiteCommand cmd = connection.CreateCommand())
263
                        {
264
                            cmd.CommandText = string.Format(@"
265 0c281fe6 gaqhf
                            SELECT s.UID, s.Name, st.Type, sp.IN_SYMBOL, sp.OUT_SYMBOL FROM {1} as st, {0} as s 
266 dd624cbd gaqhf
                                LEFT OUTER JOIN {2} as sp 
267
                                    ON s.UID = SP.UID 
268
                            WHERE s.SymbolType_UID = st.UID 
269
                            AND (st.Type = 'Instrument OPC''s' OR st.Type = 'Piping OPC''s') 
270
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_OPC_MAPPING_TABLE);
271
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
272
                                dt.Load(dr);
273
                        }
274
                        connection.Close();
275
                    }
276
                    catch (Exception ex)
277
                    {
278
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
279
                    }
280
                    finally
281
                    {
282
                        connection.Dispose();
283
                    }
284
                }
285
            }
286
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
287
            {
288
                using (SqlConnection connection = GetSqlConnection())
289
                {
290
                    try
291
                    {
292
                        if (connection != null && connection.State == ConnectionState.Open)
293
                        {
294
                            using (SqlCommand cmd = connection.CreateCommand())
295
                            {
296
                                cmd.CommandText = string.Format(@"
297 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 
298 dd624cbd gaqhf
                                LEFT OUTER JOIN {2} as sp 
299
                                    ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID)
300
                            WHERE s.SymbolType_UID = st.UID 
301
                            AND (st.Type = 'Instrument OPC''s' OR st.Type = 'Piping OPC''s') 
302
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_OPC_MAPPING_TABLE);
303
                                using (SqlDataReader dr = cmd.ExecuteReader())
304
                                    dt.Load(dr);
305
                            }
306
                            connection.Close();
307
                        }
308
                    }
309
                    catch (Exception ex)
310
                    {
311
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
312
                    }
313
                    finally
314
                    {
315
                        if (connection != null)
316
                            connection.Dispose();
317
                    }
318
                }
319
            }
320
321
            return dt;
322
        }
323 9ee9b61d gaqhf
        public static DataTable GetLineMappingTable()
324
        {
325
            DataTable dt = new DataTable();
326
            Project_Info projectInfo = Project_Info.GetInstance();
327
            if (projectInfo.DBType == ID2DB_Type.SQLite)
328
            {
329
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
330
                {
331
                    try
332
                    {
333
                        connection.Open();
334
                        using (SQLiteCommand cmd = connection.CreateCommand())
335
                        {
336
                            cmd.CommandText = string.Format(@"
337 9dab7146 gaqhf
                            SELECT l.UID, l.Name, sp.APID_SYMBOL, sp.DATA1 FROM {0} as l 
338 9ee9b61d gaqhf
                                LEFT OUTER JOIN {1} as sp 
339
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, APID_SYMBOL_MAPPING_TABLE);
340
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
341
                                dt.Load(dr);
342
                        }
343
                        connection.Close();
344
                    }
345
                    catch (Exception ex)
346
                    {
347
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
348
                    }
349
                    finally
350
                    {
351
                        connection.Dispose();
352
                    }
353
                }
354
            }
355
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
356
            {
357
                using (SqlConnection connection = GetSqlConnection())
358
                {
359
                    try
360
                    {
361
                        if (connection != null && connection.State == ConnectionState.Open)
362
                        {
363
                            using (SqlCommand cmd = connection.CreateCommand())
364
                            {
365
                                cmd.CommandText = string.Format(@"
366 9dab7146 gaqhf
                            SELECT l.UID, l.Name, sp.APID_SYMBOL, sp.DATA1 FROM {0} as l 
367 9ee9b61d gaqhf
                                LEFT OUTER JOIN {1} as sp 
368
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, APID_SYMBOL_MAPPING_TABLE);
369
                                using (SqlDataReader dr = cmd.ExecuteReader())
370
                                    dt.Load(dr);
371
                            }
372
                            connection.Close();
373
                        }
374
                    }
375
                    catch (Exception ex)
376
                    {
377
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
378
                    }
379
                    finally
380
                    {
381
                        if (connection != null)
382
                            connection.Dispose();
383
                    }
384
                }
385
            }
386 465c8b6e gaqhf
387 9ee9b61d gaqhf
            return dt;
388
        }
389 4622d687 gaqhf
        public static DataTable GetID2SymbolTable()
390 465c8b6e gaqhf
        {
391
            DataTable dt = new DataTable();
392
            Project_Info projectInfo = Project_Info.GetInstance();
393
            if (projectInfo.DBType == ID2DB_Type.SQLite)
394
            {
395
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
396
                {
397
                    try
398
                    {
399
                        connection.Open();
400
                        using (SQLiteCommand cmd = connection.CreateCommand())
401
                        {
402
                            cmd.CommandText = @"SELECT * FROM Symbol";
403
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
404
                                dt.Load(dr);
405
                        }
406
                        connection.Close();
407
                    }
408
                    catch (Exception ex)
409
                    {
410
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
411
                    }
412
                    finally
413
                    {
414
                        connection.Dispose();
415
                    }
416
                }
417
            }
418
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
419
            {
420
                using (SqlConnection connection = GetSqlConnection())
421
                {
422
                    try
423
                    {
424
                        if (connection != null && connection.State == ConnectionState.Open)
425
                        {
426
                            using (SqlCommand cmd = connection.CreateCommand())
427
                            {
428
                                cmd.CommandText = @"SELECT * FROM Symbol";
429
                                using (SqlDataReader dr = cmd.ExecuteReader())
430
                                    dt.Load(dr);
431
                            }
432
                            connection.Close();
433
                        }
434
                    }
435
                    catch (Exception ex)
436
                    {
437
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
438
                    }
439
                    finally
440
                    {
441
                        if (connection != null)
442
                            connection.Dispose();
443
                    }
444
                }
445
            }
446
447
            return dt;
448
        }
449 4622d687 gaqhf
        public static DataTable GetSymbolType()
450 d327a608 gaqhf
        {
451
            DataTable dt = new DataTable();
452
            Project_Info projectInfo = Project_Info.GetInstance();
453
            if (projectInfo.DBType == ID2DB_Type.SQLite)
454
            {
455
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
456
                {
457
                    try
458
                    {
459
                        connection.Open();
460
                        using (SQLiteCommand cmd = connection.CreateCommand())
461
                        {
462
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
463
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
464
                                dt.Load(dr);
465
                        }
466
                        connection.Close();
467
                    }
468
                    catch (Exception ex)
469
                    {
470
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
471
                    }
472
                    finally
473
                    {
474
                        connection.Dispose();
475
                    }
476
                }
477
            }
478
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
479
            {
480
                using (SqlConnection connection = GetSqlConnection())
481
                {
482
                    try
483
                    {
484
                        if (connection != null && connection.State == ConnectionState.Open)
485
                        {
486
                            using (SqlCommand cmd = connection.CreateCommand())
487
                            {
488
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
489
                                using (SqlDataReader dr = cmd.ExecuteReader())
490
                                    dt.Load(dr);
491
                            }
492
                            connection.Close();
493
                        }
494
                    }
495
                    catch (Exception ex)
496
                    {
497
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
498
                    }
499
                    finally
500
                    {
501
                        if (connection != null)
502
                            connection.Dispose();
503
                    }
504
                }
505
            }
506 465c8b6e gaqhf
507 d327a608 gaqhf
            return dt;
508
        }
509 4622d687 gaqhf
        public static DataTable GetDrawings()
510 c7db500b gaqhf
        {
511
            DataTable dt = new DataTable();
512
            Project_Info projectInfo = Project_Info.GetInstance();
513
            if (projectInfo.DBType == ID2DB_Type.SQLite)
514
            {
515
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
516
                {
517
                    try
518
                    {
519
                        connection.Open();
520
                        using (SQLiteCommand cmd = connection.CreateCommand())
521
                        {
522
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
523
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
524
                                dt.Load(dr);
525
                        }
526
                        connection.Close();
527
                    }
528
                    catch (Exception ex)
529
                    {
530
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
531
                    }
532
                    finally
533
                    {
534
                        connection.Dispose();
535
                    }
536
                }
537
            }
538
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
539
            {
540
                using (SqlConnection connection = GetSqlConnection())
541
                {
542
                    try
543
                    {
544
                        if (connection != null && connection.State == ConnectionState.Open)
545
                        {
546
                            using (SqlCommand cmd = connection.CreateCommand())
547
                            {
548
                                cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
549
                                using (SqlDataReader dr = cmd.ExecuteReader())
550
                                    dt.Load(dr);
551
                            }
552
                            connection.Close();
553
                        }
554
                    }
555
                    catch (Exception ex)
556
                    {
557
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
558
                    }
559
                    finally
560
                    {
561
                        if (connection != null)
562
                            connection.Dispose();
563
                    }
564
                }
565
            }
566
567
            return dt;
568
        }
569 9dab7146 gaqhf
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, string>> datas)
570 c8da68ce gaqhf
        {
571
            Project_Info projectInfo = Project_Info.GetInstance();
572
            if (projectInfo.DBType == ID2DB_Type.SQLite)
573
            {
574
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
575
                {
576
                    try
577
                    {
578
                        connection.Open();
579
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
580
                        {
581
                            try
582
                            {
583
                                using (SQLiteCommand cmd = connection.CreateCommand())
584
                                {
585
                                    foreach (var item in datas)
586
                                    {
587
                                        cmd.Parameters.Clear();
588 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);
589 c8da68ce gaqhf
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
590
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
591
                                        cmd.Parameters.AddWithValue("@APID_SYMBOL", item.Item3);
592 9dab7146 gaqhf
                                        cmd.Parameters.AddWithValue("@DATA1", item.Item4);
593 c8da68ce gaqhf
                                        cmd.ExecuteNonQuery();
594
                                    }
595
                                }
596
                                transaction.Commit();
597
                                connection.Close();
598
                            }
599
                            catch (Exception ex)
600
                            {
601
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
602
                                transaction.Rollback();
603
                                return false;
604
                            }
605
                            finally
606
                            {
607
                                transaction.Dispose();
608
                            }
609
                        }
610
                    }
611
                    catch (Exception ex)
612
                    {
613
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
614
                        return false;
615
                    }
616
                    finally
617
                    {
618
                        connection.Dispose();
619
                    }
620
                }
621
            }
622
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
623
            {
624
                using (SqlConnection connection = GetSqlConnection())
625
                {
626
                    try
627
                    {
628
                        if (connection != null && connection.State == ConnectionState.Open)
629
                        {
630
                            using (SqlCommand cmd = connection.CreateCommand())
631
                            {
632
                                foreach (var item in datas)
633
                                {
634
                                    cmd.Parameters.Clear();
635
                                    cmd.CommandText = string.Format(@"
636
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
637 9dab7146 gaqhf
                                        UPDATE {0} SET NAME = @NAME, APID_SYMBOL = @APID_SYMBOL, DATA1 = @DATA1 WHERE UID = @UID
638 c8da68ce gaqhf
                                    ELSE
639 9dab7146 gaqhf
                                        INSERT INTO {0} (UID, NAME, APID_SYMBOL, DATA1) VALUES (@UID, @NAME, @APID_SYMBOL, @DATA1)", APID_SYMBOL_MAPPING_TABLE, item.Item1);
640 c8da68ce gaqhf
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
641
                                    if (string.IsNullOrEmpty(item.Item2))
642
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
643
                                    else
644
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
645
                                    if (string.IsNullOrEmpty(item.Item3))
646
                                        cmd.Parameters.AddWithValue("@APID_SYMBOL", DBNull.Value);
647
                                    else
648
                                        cmd.Parameters.AddWithValue("@APID_SYMBOL", item.Item3);
649 9dab7146 gaqhf
                                    if (string.IsNullOrEmpty(item.Item4))
650
                                        cmd.Parameters.AddWithValue("@DATA1", DBNull.Value);
651
                                    else
652
                                        cmd.Parameters.AddWithValue("@DATA1", item.Item4);
653
654 c8da68ce gaqhf
                                    cmd.ExecuteNonQuery();
655
                                }
656
                            }
657
                            connection.Close();
658
                        }
659
                    }
660
                    catch (Exception ex)
661
                    {
662
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
663
                        return false;
664
                    }
665
                    finally
666
                    {
667
                        if (connection != null)
668
                            connection.Dispose();
669
                    }
670
                }
671
            }
672
673
            return true;
674
        }
675 0c281fe6 gaqhf
        public static bool InsertOPCMapping(List<Tuple<string, string, string>> datas)
676
        {
677
            Project_Info projectInfo = Project_Info.GetInstance();
678
            if (projectInfo.DBType == ID2DB_Type.SQLite)
679
            {
680
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
681
                {
682
                    try
683
                    {
684
                        connection.Open();
685
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
686
                        {
687
                            try
688
                            {
689
                                using (SQLiteCommand cmd = connection.CreateCommand())
690
                                {
691
                                    foreach (var item in datas)
692
                                    {
693
                                        cmd.Parameters.Clear();
694
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, IN_SYMBOL, OUT_SYMBOL) VALUES (@UID, @IN_SYMBOL, @OUT_SYMBOL)", APID_OPC_MAPPING_TABLE);
695
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
696
                                        cmd.Parameters.AddWithValue("@IN_SYMBOL", item.Item2);
697
                                        cmd.Parameters.AddWithValue("@OUT_SYMBOL", item.Item3);
698
                                        cmd.ExecuteNonQuery();
699
                                    }
700
                                }
701
                                transaction.Commit();
702
                                connection.Close();
703
                            }
704
                            catch (Exception ex)
705
                            {
706
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
707
                                transaction.Rollback();
708
                                return false;
709
                            }
710
                            finally
711
                            {
712
                                transaction.Dispose();
713
                            }
714
                        }
715
                    }
716
                    catch (Exception ex)
717
                    {
718
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
719
                        return false;
720
                    }
721
                    finally
722
                    {
723
                        connection.Dispose();
724
                    }
725
                }
726
            }
727
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
728
            {
729
                using (SqlConnection connection = GetSqlConnection())
730
                {
731
                    try
732
                    {
733
                        if (connection != null && connection.State == ConnectionState.Open)
734
                        {
735
                            using (SqlCommand cmd = connection.CreateCommand())
736
                            {
737
                                foreach (var item in datas)
738
                                {
739
                                    cmd.Parameters.Clear();
740
                                    cmd.CommandText = string.Format(@"
741
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
742
                                        UPDATE {0} SET IN_SYMBOL = @IN_SYMBOL, OUT_SYMBOL = @OUT_SYMBOL WHERE UID = @UID
743
                                    ELSE
744
                                        INSERT INTO {0} (UID, IN_SYMBOL, OUT_SYMBOL) VALUES (@UID, @IN_SYMBOL, @OUT_SYMBOL)", APID_OPC_MAPPING_TABLE, item.Item1);
745
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
746
                                    if (string.IsNullOrEmpty(item.Item2))
747
                                        cmd.Parameters.AddWithValue("@IN_SYMBOL", DBNull.Value);
748
                                    else
749
                                        cmd.Parameters.AddWithValue("@IN_SYMBOL", item.Item2);
750
                                    if (string.IsNullOrEmpty(item.Item3))
751
                                        cmd.Parameters.AddWithValue("@OUT_SYMBOL", DBNull.Value);
752
                                    else
753
                                        cmd.Parameters.AddWithValue("@OUT_SYMBOL", item.Item3);
754
755
                                    cmd.ExecuteNonQuery();
756 ee781888 gaqhf
                                }
757
                            }
758
                            connection.Close();
759
                        }
760
                    }
761
                    catch (Exception ex)
762
                    {
763
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
764
                        return false;
765
                    }
766
                    finally
767
                    {
768
                        if (connection != null)
769
                            connection.Dispose();
770
                    }
771
                }
772
            }
773
774
            return true;
775
        }
776
        public static bool InsertAttributeMapping(List<Tuple<string, string, string>> datas)
777
        {
778
            Project_Info projectInfo = Project_Info.GetInstance();
779
            if (projectInfo.DBType == ID2DB_Type.SQLite)
780
            {
781
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
782
                {
783
                    try
784
                    {
785
                        connection.Open();
786
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
787
                        {
788
                            try
789
                            {
790
                                using (SQLiteCommand cmd = connection.CreateCommand())
791
                                {
792
                                    foreach (var item in datas)
793
                                    {
794
                                        cmd.Parameters.Clear();
795
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, APID_ATTRIBUTE, APID_ATTRIBUTE_TYPE) VALUES (@UID, @APID_ATTRIBUTE, @APID_ATTRIBUTE_TYPE)", APID_ATTRIBUTE_MAPPING_TABLE);
796
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
797
                                        cmd.Parameters.AddWithValue("@APID_ATTRIBUTE", item.Item2);
798
                                        cmd.Parameters.AddWithValue("@APID_ATTRIBUTE_TYPE", item.Item3);
799
                                        cmd.ExecuteNonQuery();
800
                                    }
801
                                }
802
                                transaction.Commit();
803
                                connection.Close();
804
                            }
805
                            catch (Exception ex)
806
                            {
807
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
808
                                transaction.Rollback();
809
                                return false;
810
                            }
811
                            finally
812
                            {
813
                                transaction.Dispose();
814
                            }
815
                        }
816
                    }
817
                    catch (Exception ex)
818
                    {
819
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
820
                        return false;
821
                    }
822
                    finally
823
                    {
824
                        connection.Dispose();
825
                    }
826
                }
827
            }
828
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
829
            {
830
                using (SqlConnection connection = GetSqlConnection())
831
                {
832
                    try
833
                    {
834
                        if (connection != null && connection.State == ConnectionState.Open)
835
                        {
836
                            using (SqlCommand cmd = connection.CreateCommand())
837
                            {
838
                                foreach (var item in datas)
839
                                {
840
                                    cmd.Parameters.Clear();
841
                                    cmd.CommandText = string.Format(@"
842
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
843
                                        UPDATE {0} SET APID_ATTRIBUTE = @APID_ATTRIBUTE, APID_ATTRIBUTE_TYPE = @APID_ATTRIBUTE_TYPE WHERE UID = @UID
844
                                    ELSE
845
                                        INSERT INTO {0} (UID, APID_ATTRIBUTE, APID_ATTRIBUTE_TYPE) VALUES (@UID, @APID_ATTRIBUTE, @APID_ATTRIBUTE_TYPE)", APID_ATTRIBUTE_MAPPING_TABLE, item.Item1);
846
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
847
                                    if (string.IsNullOrEmpty(item.Item2))
848
                                        cmd.Parameters.AddWithValue("@APID_ATTRIBUTE", DBNull.Value);
849
                                    else
850
                                        cmd.Parameters.AddWithValue("@APID_ATTRIBUTE", item.Item2);
851
                                    if (string.IsNullOrEmpty(item.Item3))
852
                                        cmd.Parameters.AddWithValue("@APID_ATTRIBUTE_TYPE", DBNull.Value);
853
                                    else
854
                                        cmd.Parameters.AddWithValue("@APID_ATTRIBUTE_TYPE", item.Item3);
855
856
                                    cmd.ExecuteNonQuery();
857 0c281fe6 gaqhf
                                }
858
                            }
859
                            connection.Close();
860
                        }
861
                    }
862
                    catch (Exception ex)
863
                    {
864
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
865
                        return false;
866
                    }
867
                    finally
868
                    {
869
                        if (connection != null)
870
                            connection.Dispose();
871
                    }
872
                }
873
            }
874
875
            return true;
876
        }
877 faba1fc7 gaqhf
        public static bool InsertAPIDAttribute(List<Tuple<string, string, string, string>> datas)
878
        {
879
            Project_Info projectInfo = Project_Info.GetInstance();
880
            if (projectInfo.DBType == ID2DB_Type.SQLite)
881
            {
882
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
883
                {
884
                    try
885
                    {
886
                        connection.Open();
887
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
888
                        {
889
                            try
890
                            {
891
                                using (SQLiteCommand cmd = connection.CreateCommand())
892
                                {
893
                                    foreach (var item in datas)
894
                                    {
895
                                        cmd.Parameters.Clear();
896
                                        cmd.CommandText = string.Format(@"
897 ad983925 gaqhf
                                        DELETE FROM {0} WHERE LEVEL1 = @LEVEL1 AND LEVEL2 = @LEVEL2 AND ATTRIBUTE_KEY = @ATTRIBUTE_KEY",
898
                                            APID_ATTRIBUTE_TABLE, item.Item1, item.Item2, item.Item3);
899
                                        cmd.Parameters.AddWithValue("@LEVEL1", item.Item1);
900
                                        cmd.Parameters.AddWithValue("@LEVEL2", item.Item2);
901
                                        cmd.Parameters.AddWithValue("@ATTRIBUTE_KEY", item.Item3);
902
                                        cmd.ExecuteNonQuery();
903
904
                                        cmd.Parameters.Clear();
905
                                        cmd.CommandText = string.Format(@"
906 20dd244c gaqhf
                                        INSERT OR REPLACE INTO {0} (LEVEL1, LEVEL2, ATTRIBUTE_KEY, ATTRIBUTE_NAME) VALUES (@LEVEL1, @LEVEL2, @ATTRIBUTE_KEY, @ATTRIBUTE_NAME)",
907 faba1fc7 gaqhf
                                            APID_ATTRIBUTE_TABLE, item.Item1, item.Item2, item.Item3);
908
                                        cmd.Parameters.AddWithValue("@LEVEL1", item.Item1);
909
                                        cmd.Parameters.AddWithValue("@LEVEL2", item.Item2);
910
                                        cmd.Parameters.AddWithValue("@ATTRIBUTE_KEY", item.Item3);
911
                                        cmd.Parameters.AddWithValue("@ATTRIBUTE_NAME", item.Item4);
912
                                        cmd.ExecuteNonQuery();
913
                                    }
914
                                }
915
                                transaction.Commit();
916
                                connection.Close();
917
                            }
918
                            catch (Exception ex)
919
                            {
920
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
921
                                transaction.Rollback();
922
                                return false;
923
                            }
924
                            finally
925
                            {
926
                                transaction.Dispose();
927
                            }
928
                        }
929
                    }
930
                    catch (Exception ex)
931
                    {
932
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
933
                        return false;
934
                    }
935
                    finally
936
                    {
937
                        connection.Dispose();
938
                    }
939
                }
940
            }
941
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
942
            {
943
                using (SqlConnection connection = GetSqlConnection())
944
                {
945
                    try
946
                    {
947
                        if (connection != null && connection.State == ConnectionState.Open)
948
                        {
949
                            using (SqlCommand cmd = connection.CreateCommand())
950
                            {
951
                                foreach (var item in datas)
952
                                {
953
                                    cmd.Parameters.Clear();
954
                                    cmd.CommandText = string.Format(@"
955
                                    IF EXISTS (SELECT * FROM {0} WHERE LEVEL1 = '{1}' AND LEVEL2 = '{2}' AND ATTRIBUTE_KEY = '{3}')
956
                                        UPDATE {0} SET ATTRIBUTE_NAME = @ATTRIBUTE_NAME WHERE LEVEL1 = '{1}' AND LEVEL2 = '{2}' AND ATTRIBUTE_KEY = '{3}'
957
                                    ELSE
958
                                        INSERT INTO {0} (LEVEL1, LEVEL2, ATTRIBUTE_KEY, ATTRIBUTE_NAME) VALUES (@LEVEL1, @LEVEL2, @ATTRIBUTE_KEY, @ATTRIBUTE_NAME)",
959
                                        APID_ATTRIBUTE_TABLE, item.Item1, item.Item2, item.Item3);
960
                                    cmd.Parameters.AddWithValue("@LEVEL1", item.Item1);
961
                                    cmd.Parameters.AddWithValue("@LEVEL2", item.Item2);
962
                                    cmd.Parameters.AddWithValue("@ATTRIBUTE_KEY", item.Item3);
963
                                    cmd.Parameters.AddWithValue("@ATTRIBUTE_NAME", item.Item4);
964
                                    cmd.ExecuteNonQuery();
965
                                }
966
                            }
967
                            connection.Close();
968
                        }
969
                    }
970
                    catch (Exception ex)
971
                    {
972
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
973
                        return false;
974
                    }
975
                    finally
976
                    {
977
                        if (connection != null)
978
                            connection.Dispose();
979
                    }
980
                }
981
            }
982
983
            return true;
984
        }
985 4622d687 gaqhf
        public static DataTable GetProjectAttribute()
986 7a56b228 gaqhf
        {
987
            DataTable dt = new DataTable();
988
            Project_Info projectInfo = Project_Info.GetInstance();
989
            if (projectInfo.DBType == ID2DB_Type.SQLite)
990
            {
991
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
992
                {
993
                    try
994
                    {
995
                        connection.Open();
996
                        using (SQLiteCommand cmd = connection.CreateCommand())
997
                        {
998
                            cmd.CommandText = string.Format(@"
999
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.APID_ATTRIBUTE, spa.APID_ATTRIBUTE_TYPE, sp.APID_SYMBOL, sa.Property
1000
                            FROM {1} as sa, {0} as st 
1001
                                 LEFT OUTER JOIN {2} as sp 
1002
                                      ON sa.UID = SP.UID 
1003
                                LEFT OUTER JOIN {3} as spa 
1004
                                     ON sa.UID = spa.UID
1005
                            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);
1006
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1007
                                dt.Load(dr);
1008
                        }
1009
                        connection.Close();
1010
                    }
1011
                    catch (Exception ex)
1012
                    {
1013
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1014
                    }
1015
                    finally
1016
                    {
1017
                        connection.Dispose();
1018
                    }
1019
                }
1020
            }
1021
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1022
            {
1023
                using (SqlConnection connection = GetSqlConnection())
1024
                {
1025
                    try
1026
                    {
1027
                        if (connection != null && connection.State == ConnectionState.Open)
1028
                        {
1029
                            using (SqlCommand cmd = connection.CreateCommand())
1030
                            {
1031
                                cmd.CommandText = string.Format(@"
1032
                            SELECT sa.UID, sa.DisplayAttribute, st.Type, spa.APID_ATTRIBUTE, spa.APID_ATTRIBUTE_TYPE, sp.APID_SYMBOL, sa.Property
1033
                            FROM {1} as sa
1034
                                 LEFT OUTER JOIN {2} as sp 
1035
                                      ON sa.UID = SP.UID 
1036
                                LEFT OUTER JOIN {3} as spa 
1037
                                     ON sa.UID = spa.UID
1038
                                LEFT OUTER JOIN {0} as st 
1039
                                     ON sa.SymbolType_UID = st.UID 
1040
                            WHERE (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, APID_SYMBOL_MAPPING_TABLE, APID_ATTRIBUTE_MAPPING_TABLE);
1041
                                using (SqlDataReader dr = cmd.ExecuteReader())
1042
                                    dt.Load(dr);
1043
                            }
1044
                            connection.Close();
1045
                        }
1046
                    }
1047
                    catch (Exception ex)
1048
                    {
1049
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1050
                    }
1051
                    finally
1052
                    {
1053
                        if (connection != null)
1054
                            connection.Dispose();
1055
                    }
1056
                }
1057
            }
1058
1059
1060
            return dt;
1061
        }
1062 4622d687 gaqhf
        public static DataTable GetProjectLineProperties()
1063 2b02c914 gaqhf
        {
1064
            DataTable dt = new DataTable();
1065
            Project_Info projectInfo = Project_Info.GetInstance();
1066
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1067
            {
1068
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1069
                {
1070
                    try
1071
                    {
1072
                        connection.Open();
1073
                        using (SQLiteCommand cmd = connection.CreateCommand())
1074
                        {
1075
                            cmd.CommandText = string.Format(@"
1076
                            SELECT lp.UID, lp.DisplayName, sp.APID_SYMBOL, spa.APID_ATTRIBUTE
1077
                            FROM {0} as lp 
1078
                                 LEFT OUTER JOIN {1} as sp 
1079
                                      ON lp.UID = sp.UID
1080
                                 LEFT OUTER JOIN {2} as spa 
1081
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, APID_SYMBOL_MAPPING_TABLE, APID_ATTRIBUTE_MAPPING_TABLE);
1082
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1083
                                dt.Load(dr);
1084
                        }
1085
                        connection.Close();
1086
                    }
1087
                    catch (Exception ex)
1088
                    {
1089
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1090
                    }
1091
                    finally
1092
                    {
1093
                        connection.Dispose();
1094
                    }
1095
                }
1096
            }
1097
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1098
            {
1099
                using (SqlConnection connection = GetSqlConnection())
1100
                {
1101
                    try
1102
                    {
1103
                        if (connection != null && connection.State == ConnectionState.Open)
1104
                        {
1105
                            using (SqlCommand cmd = connection.CreateCommand())
1106
                            {
1107
                                cmd.CommandText = string.Format(@"
1108
                            SELECT lp.UID, lp.DisplayName, sp.APID_SYMBOL, spa.APID_ATTRIBUTE
1109
                            FROM {0} as lp 
1110
                                 LEFT OUTER JOIN {1} as sp 
1111
                                      ON lp.UID = sp.UID
1112
                                 LEFT OUTER JOIN {2} as spa 
1113
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, APID_SYMBOL_MAPPING_TABLE, APID_ATTRIBUTE_MAPPING_TABLE);
1114
                                using (SqlDataReader dr = cmd.ExecuteReader())
1115
                                    dt.Load(dr);
1116
                            }
1117
                            connection.Close();
1118
                        }
1119
                    }
1120
                    catch (Exception ex)
1121
                    {
1122
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1123 4622d687 gaqhf
                    }
1124
                    finally
1125
                    {
1126
                        if (connection != null)
1127
                            connection.Dispose();
1128
                    }
1129
                }
1130
            }
1131
1132
            return dt;
1133
        }
1134
        public static DataTable GetSymbolMappingTableOnlySymbol()
1135
        {
1136
            DataTable dt = new DataTable();
1137
            Project_Info projectInfo = Project_Info.GetInstance();
1138
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1139
            {
1140
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1141
                {
1142
                    try
1143
                    {
1144
                        connection.Open();
1145
                        using (SQLiteCommand cmd = connection.CreateCommand())
1146
                        {
1147
                            cmd.CommandText = string.Format("SELECT * FROM {0}", APID_SYMBOL_MAPPING_TABLE);
1148
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1149
                                dt.Load(dr);
1150
                        }
1151
                        connection.Close();
1152
                    }
1153
                    catch (Exception ex)
1154
                    {
1155
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1156
                    }
1157
                    finally
1158
                    {
1159
                        connection.Dispose();
1160
                    }
1161
                }
1162
            }
1163
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1164
            {
1165
                using (SqlConnection connection = GetSqlConnection())
1166
                {
1167
                    try
1168
                    {
1169
                        if (connection != null && connection.State == ConnectionState.Open)
1170
                        {
1171
                            using (SqlCommand cmd = connection.CreateCommand())
1172
                            {
1173
                                cmd.CommandText = string.Format("SELECT * FROM {0}", APID_SYMBOL_MAPPING_TABLE);
1174
                                using (SqlDataReader dr = cmd.ExecuteReader())
1175
                                    dt.Load(dr);
1176
                            }
1177
                            connection.Close();
1178
                        }
1179
                    }
1180
                    catch (Exception ex)
1181
                    {
1182
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1183
                    }
1184
                    finally
1185
                    {
1186
                        if (connection != null)
1187
                            connection.Dispose();
1188
                    }
1189
                }
1190
            }
1191
1192
            return dt;
1193
        }
1194
        public static DataTable GetAttributeMappingTableOnlyAttribute()
1195
        {
1196
            DataTable dt = new DataTable();
1197
            Project_Info projectInfo = Project_Info.GetInstance();
1198
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1199
            {
1200
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1201
                {
1202
                    try
1203
                    {
1204
                        connection.Open();
1205
                        using (SQLiteCommand cmd = connection.CreateCommand())
1206
                        {
1207
                            cmd.CommandText = string.Format("SELECT * FROM {0}", APID_ATTRIBUTE_MAPPING_TABLE);
1208
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1209
                                dt.Load(dr);
1210
                        }
1211
                        connection.Close();
1212
                    }
1213
                    catch (Exception ex)
1214
                    {
1215
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1216
                    }
1217
                    finally
1218
                    {
1219
                        connection.Dispose();
1220
                    }
1221
                }
1222
            }
1223
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1224
            {
1225
                using (SqlConnection connection = GetSqlConnection())
1226
                {
1227
                    try
1228
                    {
1229
                        if (connection != null && connection.State == ConnectionState.Open)
1230
                        {
1231
                            using (SqlCommand cmd = connection.CreateCommand())
1232
                            {
1233
                                cmd.CommandText = string.Format("SELECT * FROM {0}", APID_ATTRIBUTE_MAPPING_TABLE);
1234
                                using (SqlDataReader dr = cmd.ExecuteReader())
1235
                                    dt.Load(dr);
1236
                            }
1237
                            connection.Close();
1238
                        }
1239
                    }
1240
                    catch (Exception ex)
1241
                    {
1242
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1243 faba1fc7 gaqhf
                    }
1244
                    finally
1245
                    {
1246
                        if (connection != null)
1247
                            connection.Dispose();
1248
                    }
1249
                }
1250
            }
1251
1252
            return dt;
1253
        }
1254
        public static DataTable GetAPIDAttributeTable()
1255
        {
1256
            DataTable dt = new DataTable();
1257
            Project_Info projectInfo = Project_Info.GetInstance();
1258
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1259
            {
1260
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1261
                {
1262
                    try
1263
                    {
1264
                        connection.Open();
1265
                        using (SQLiteCommand cmd = connection.CreateCommand())
1266
                        {
1267
                            cmd.CommandText = string.Format("SELECT * FROM {0}", APID_ATTRIBUTE_TABLE);
1268
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1269
                                dt.Load(dr);
1270
                        }
1271
                        connection.Close();
1272
                    }
1273
                    catch (Exception ex)
1274
                    {
1275
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1276
                    }
1277
                    finally
1278
                    {
1279
                        connection.Dispose();
1280
                    }
1281
                }
1282
            }
1283
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1284
            {
1285
                using (SqlConnection connection = GetSqlConnection())
1286
                {
1287
                    try
1288
                    {
1289
                        if (connection != null && connection.State == ConnectionState.Open)
1290
                        {
1291
                            using (SqlCommand cmd = connection.CreateCommand())
1292
                            {
1293
                                cmd.CommandText = string.Format("SELECT * FROM {0}", APID_ATTRIBUTE_TABLE);
1294
                                using (SqlDataReader dr = cmd.ExecuteReader())
1295
                                    dt.Load(dr);
1296
                            }
1297
                            connection.Close();
1298
                        }
1299
                    }
1300
                    catch (Exception ex)
1301
                    {
1302
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1303 2b02c914 gaqhf
                    }
1304
                    finally
1305
                    {
1306
                        if (connection != null)
1307
                            connection.Dispose();
1308
                    }
1309
                }
1310
            }
1311
1312
            return dt;
1313
        }
1314 a77303ca gaqhf
        #endregion
1315 e9ed6b7b gaqhf
1316
        #region AVEVA
1317 534ca9c5 gaqhf
        public static string GetAvevaConnectionString()
1318 e9ed6b7b gaqhf
        {
1319
            string strConn = string.Empty;
1320
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
1321
            {
1322
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);
1323
            }
1324
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
1325
            {
1326
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
1327
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
1328
            }
1329
1330
            return strConn;
1331
        }
1332 534ca9c5 gaqhf
        public static string GetAvevaConnectionString_Reports()
1333 e9ed6b7b gaqhf
        {
1334
            string strConn = string.Empty;
1335
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
1336
            {
1337
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
1338
            }
1339
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
1340
            {
1341
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
1342
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
1343
            }
1344
1345
            return strConn;
1346
        }
1347 534ca9c5 gaqhf
        public static string GetAvevaConnectionString_Admin()
1348 e9ed6b7b gaqhf
        {
1349
            string strConn = string.Empty;
1350
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
1351
            {
1352
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName);
1353
            }
1354
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
1355
            {
1356
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
1357
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString);
1358
            }
1359
1360
            return strConn;
1361
        }
1362 59184d1b gaqhf
        public static string GetDirectiveValue(string name)
1363
        {
1364
            string result = null;
1365
1366
            using (SqlConnection connection = new SqlConnection())
1367
            {
1368
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1369
                connection.Open();
1370
                using (SqlCommand cmd = connection.CreateCommand())
1371
                {
1372
                    cmd.CommandText = string.Format("select DirectiveValue from ProjectDirectives where DirectiveName = '{0}'", name);
1373
                    using (SqlDataReader dr = cmd.ExecuteReader())
1374
                    {
1375
                        if (dr.Read())
1376
                            result = dr.GetString(0);
1377
                    }
1378
                }
1379
                connection.Close();
1380
            }
1381
1382
            return result;
1383
        }
1384
        public static DataTable GetDrawingTemplate()
1385
        {
1386
            DataTable dt = new DataTable();
1387
1388
            using (SqlConnection connection = new SqlConnection())
1389
            {
1390
                connection.ConnectionString = GetAvevaConnectionString_Reports();
1391
                connection.Open();
1392
                using (SqlCommand cmd = connection.CreateCommand())
1393
                {
1394
                    cmd.CommandText = "select ID, Name,IsDWG,DateCreation,MachineName,FutureUse1,FutureUse2 from DrawingTemplates where IsDWG=0";
1395
                    using (SqlDataReader dr = cmd.ExecuteReader())
1396
                        dt.Load(dr);
1397
                }
1398
                connection.Close();
1399
            }
1400
1401
            return dt;
1402
        }
1403 56cd4954 gaqhf
        public static DataTable SelectStandardSymbolTable()
1404 e9ed6b7b gaqhf
        {
1405
            DataTable dt = new DataTable();
1406
1407
            using (SqlConnection connection = new SqlConnection())
1408
            {
1409
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1410
                connection.Open();
1411
                using (SqlCommand cmd = connection.CreateCommand())
1412
                {
1413 56cd4954 gaqhf
                    cmd.CommandText = string.Format(@"
1414
                    SELECT ss.ID, ss.Symbol_Name, sps.Relative_Path FROM {0} as ss
1415
                    LEFT OUTER JOIN {1} as sps
1416
                    ON ss.Relative_Path = sps.ID
1417
                    ORDER BY sps.ID ASC
1418
                    ;", "stdSymbols", "StdPrjFolStructure");
1419
1420
                    using (SqlDataReader dr = cmd.ExecuteReader())
1421
                        dt.Load(dr);
1422
                }
1423
                connection.Close();
1424
            }
1425
1426
            return dt;
1427
        }
1428
        public static DataTable SelectStandardSymbolStructureTable()
1429
        {
1430
            DataTable dt = new DataTable();
1431
1432
            using (SqlConnection connection = new SqlConnection())
1433
            {
1434
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1435
                connection.Open();
1436
                using (SqlCommand cmd = connection.CreateCommand())
1437
                {
1438
                    cmd.CommandText = string.Format(@"
1439 acfb1ca2 Denny
                    SELECT ID, Relative_Path, 'STANDARD' as Type FROM {0} WHERE Relative_Path > ' '
1440 56cd4954 gaqhf
                    ;", "StdPrjFolStructure");
1441
1442
                    using (SqlDataReader dr = cmd.ExecuteReader())
1443
                        dt.Load(dr);
1444
                }
1445
                connection.Close();
1446
            }
1447
1448
            return dt;
1449
        }
1450 acfb1ca2 Denny
        public static DataTable SelectProjectSymbolStructureTable()
1451
        {
1452
            DataTable dt = new DataTable();
1453
1454
            using (SqlConnection connection = new SqlConnection())
1455
            {
1456
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1457
                connection.Open();
1458
                using (SqlCommand cmd = connection.CreateCommand())
1459
                {
1460
                    cmd.CommandText = string.Format(@"
1461
                    SELECT ID, Relative_Path, 'PROJECT' as Type FROM {0} WHERE Relative_Path > ' '
1462
                    ;", "UsrStdFolStructure");
1463
1464
                    using (SqlDataReader dr = cmd.ExecuteReader())
1465
                        dt.Load(dr);
1466
                }
1467
                connection.Close();
1468
            }
1469
1470
            return dt;
1471
        }
1472 56cd4954 gaqhf
        public static DataTable SelectStandardSymbolImageTable()
1473
        {
1474
            DataTable dt = new DataTable();
1475
1476
            using (SqlConnection connection = new SqlConnection())
1477
            {
1478
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1479
                connection.Open();
1480
                using (SqlCommand cmd = connection.CreateCommand())
1481
                {
1482
                    cmd.CommandText = string.Format(@"
1483 acfb1ca2 Denny
                    SELECT ss.Symbol_Name, ss.Pic_Binary, sps.Relative_Path, 'STANDARD' as Type FROM {0} as ss
1484 56cd4954 gaqhf
                    LEFT OUTER JOIN {1} as sps
1485
                    ON ss.Relative_Path = sps.ID
1486
                    ORDER BY sps.ID ASC
1487
                    ;", "stdSymbols", "StdPrjFolStructure");
1488
1489 e9ed6b7b gaqhf
                    using (SqlDataReader dr = cmd.ExecuteReader())
1490
                        dt.Load(dr);
1491
                }
1492
                connection.Close();
1493
            }
1494
1495
            return dt;
1496
        }
1497 acfb1ca2 Denny
        public static DataTable SelectProjectSymbolImageTable()
1498
        {
1499
            DataTable dt = new DataTable();
1500
1501
            using (SqlConnection connection = new SqlConnection())
1502
            {
1503
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1504
                connection.Open();
1505
                using (SqlCommand cmd = connection.CreateCommand())
1506
                {
1507
                    cmd.CommandText = string.Format(@"
1508
                    SELECT ss.Symbol_Name, ss.Pic_Binary, sps.Relative_Path, 'PROJECT' as Type FROM {0} as ss
1509
                    LEFT OUTER JOIN {1} as sps
1510
                    ON ss.Relative_Path = sps.ID
1511
                    ORDER BY sps.ID ASC
1512
                    ;", "prjSymbols", "UsrStdFolStructure");
1513
1514
                    using (SqlDataReader dr = cmd.ExecuteReader())
1515
                        dt.Load(dr);
1516
                }
1517
                connection.Close();
1518
            }
1519
1520
            return dt;
1521
        }
1522 128c844f gaqhf
        public static DataTable SelectDrawingTable()
1523
        {
1524
            DataTable dt = new DataTable();
1525 e9ed6b7b gaqhf
1526 128c844f gaqhf
            using (SqlConnection connection = new SqlConnection())
1527
            {
1528
                connection.ConnectionString = GetAvevaConnectionString_Reports();
1529
                connection.Open();
1530
                using (SqlCommand cmd = connection.CreateCommand())
1531
                {
1532
                    cmd.CommandText = string.Format(@"
1533
                    SELECT DrawingId, XDNLABEL, XDSLABEL FROM {0}
1534
                    ;", "SynchroniseDetails");
1535
1536
                    using (SqlDataReader dr = cmd.ExecuteReader())
1537
                        dt.Load(dr);
1538
                }
1539
                connection.Close();
1540
            }
1541
1542
            return dt;
1543
        }
1544 495bb8f5 gaqhf
        public static DataTable SelectSymbolTable()
1545
        {
1546
            DataTable dt = new DataTable();
1547
1548
            using (SqlConnection connection = new SqlConnection())
1549
            {
1550
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1551
                connection.Open();
1552
                using (SqlCommand cmd = connection.CreateCommand())
1553
                {
1554
                    cmd.CommandText = string.Format(@"
1555
                    SELECT * FROM {0}
1556
                    ;", "SYMBOLS");
1557
1558
                    using (SqlDataReader dr = cmd.ExecuteReader())
1559
                        dt.Load(dr);
1560
                }
1561
                connection.Close();
1562
            }
1563
1564
            return dt;
1565
        }
1566 77a33d5f gaqhf
        public static DataTable SelectUDADetails()
1567
        {
1568
            DataTable dt = new DataTable();
1569
1570
            using (SqlConnection connection = new SqlConnection())
1571
            {
1572
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1573
                connection.Open();
1574
                using (SqlCommand cmd = connection.CreateCommand())
1575
                {
1576
                    cmd.CommandText = string.Format(@"
1577
                    SELECT * FROM {0}
1578
                    ;", "UDADetails");
1579
1580
                    using (SqlDataReader dr = cmd.ExecuteReader())
1581
                        dt.Load(dr);
1582
                }
1583
                connection.Close();
1584
            }
1585
1586
            return dt;
1587
        }
1588 e9ed6b7b gaqhf
        #endregion
1589
1590 53a4ebb8 gaqhf
    }
1591
}
클립보드 이미지 추가 (최대 크기: 500 MB)