프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

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

이력 | 보기 | 이력해설 | 다운로드 (68.5 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6
using System.Globalization;
7
using System.Data.SQLite;
8
using System.Data;
9
using System.Data.SqlClient;
10
using System.IO;
11
using Newtonsoft.Json;
12
using AVEVA.PID.CustomizationUtility;
13
using AVEVA.PID.CustomizationUtility.Model;
14

    
15
namespace AVEVA.PID.CustomizationUtility.DB
16
{
17
    public class Project_DB
18
    {
19
        const string APID_SYMBOL_MAPPING_TABLE = "T_APID_SYMBOL_MAPPING";
20
        const string APID_OPC_MAPPING_TABLE = "T_APID_OPC_MAPPING";
21
        const string APID_ATTRIBUTE_MAPPING_TABLE = "T_APID_ATTRIBUTE_MAPPING";
22
        const string APID_ATTRIBUTE_TABLE = "T_APID_ATTRIBUTE";
23

    
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
        #region Only ID2 DB
32
        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
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
51
                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
                                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
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, APID_SYMBOL TEXT, DATA1 TEXT)", APID_SYMBOL_MAPPING_TABLE);
82
                                        cmd.ExecuteNonQuery();
83
                                    }
84
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_OPC_MAPPING_TABLE)).Length == 0)
85
                                    {
86
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, IN_SYMBOL TEXT, OUT_SYMBOL TEXT)", APID_OPC_MAPPING_TABLE);
87
                                        cmd.ExecuteNonQuery();
88
                                    }
89
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
90
                                    {
91
                                        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
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (LEVEL1 TEXT, LEVEL2 TEXT, ATTRIBUTE_KEY TEXT, ATTRIBUTE_NAME TEXT)", APID_ATTRIBUTE_TABLE);
97
                                        cmd.ExecuteNonQuery();
98
                                    }
99
                                }
100
                            }
101
                            result = true;
102
                        }
103
                        connection.Close();
104
                    }
105
                    catch (Exception ex)
106
                    {
107
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
108
                    }
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
                                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
                                        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
                                        cmd.ExecuteNonQuery();
135
                                    }
136
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_OPC_MAPPING_TABLE)).Length == 0)
137
                                    {
138
                                        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
                                        cmd.ExecuteNonQuery();
140
                                    }
141
                                    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
                                    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
                                }
152
                            }
153
                            result = true;
154
                        }
155
                    }
156
                    catch (Exception ex)
157
                    {
158
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
159
                    }
160
                    finally
161
                    {
162
                        if (connection != null)
163
                            connection.Dispose();
164
                    }
165
                }
166
            }
167

    
168
            return result;
169
        }
170
        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
                            SELECT s.UID, s.Name, st.Type, sp.APID_SYMBOL, sp.DATA1 FROM {1} as st, {0} as s 
185
                                LEFT OUTER JOIN {2} as sp 
186
                                    ON s.UID = SP.UID 
187
                            WHERE s.SymbolType_UID = st.UID 
188
                            AND st.Type != 'Instrument OPC''s' AND st.Type != 'Piping OPC''s' 
189
                            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
                            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
                                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
                            AND st.Type != 'Instrument OPC''s' AND st.Type != 'Piping OPC''s' 
230
                            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
        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
                            SELECT s.UID, s.Name, st.Type, sp.IN_SYMBOL, sp.OUT_SYMBOL FROM {1} as st, {0} as s 
266
                                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
                            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
                                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
        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
                            SELECT l.UID, l.Name, sp.APID_SYMBOL, sp.DATA1 FROM {0} as l 
338
                                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
                            SELECT l.UID, l.Name, sp.APID_SYMBOL, sp.DATA1 FROM {0} as l 
367
                                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

    
387
            return dt;
388
        }
389
        public static DataTable GetID2SymbolTable()
390
        {
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
        public static DataTable GetSymbolType()
450
        {
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

    
507
            return dt;
508
        }
509
        public static DataTable GetDrawings()
510
        {
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
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, string>> datas)
570
        {
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
                                        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
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
590
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
591
                                        cmd.Parameters.AddWithValue("@APID_SYMBOL", item.Item3);
592
                                        cmd.Parameters.AddWithValue("@DATA1", item.Item4);
593
                                        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
                                        UPDATE {0} SET NAME = @NAME, APID_SYMBOL = @APID_SYMBOL, DATA1 = @DATA1 WHERE UID = @UID
638
                                    ELSE
639
                                        INSERT INTO {0} (UID, NAME, APID_SYMBOL, DATA1) VALUES (@UID, @NAME, @APID_SYMBOL, @DATA1)", APID_SYMBOL_MAPPING_TABLE, item.Item1);
640
                                    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
                                    if (string.IsNullOrEmpty(item.Item4))
650
                                        cmd.Parameters.AddWithValue("@DATA1", DBNull.Value);
651
                                    else
652
                                        cmd.Parameters.AddWithValue("@DATA1", item.Item4);
653

    
654
                                    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
        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
                                }
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
                                }
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
        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
                                        IF EXISTS (SELECT * FROM {0} WHERE LEVEL1 = '{1}' AND LEVEL2 = '{2}' AND ATTRIBUTE_KEY = '{3}')
898
                                            UPDATE {0} SET ATTRIBUTE_NAME = @ATTRIBUTE_NAME WHERE LEVEL1 = '{1}' AND LEVEL2 = '{2}' AND ATTRIBUTE_KEY = '{3}'
899
                                        ELSE
900
                                            INSERT INTO {0} (LEVEL1, LEVEL2, ATTRIBUTE_KEY, ATTRIBUTE_NAME) VALUES (@LEVEL1, @LEVEL2, @ATTRIBUTE_KEY, @ATTRIBUTE_NAME)",
901
                                            APID_ATTRIBUTE_TABLE, item.Item1, item.Item2, item.Item3);
902
                                        cmd.Parameters.AddWithValue("@LEVEL1", item.Item1);
903
                                        cmd.Parameters.AddWithValue("@LEVEL2", item.Item2);
904
                                        cmd.Parameters.AddWithValue("@ATTRIBUTE_KEY", item.Item3);
905
                                        cmd.Parameters.AddWithValue("@ATTRIBUTE_NAME", item.Item4);
906
                                        cmd.ExecuteNonQuery();
907
                                    }
908
                                }
909
                                transaction.Commit();
910
                                connection.Close();
911
                            }
912
                            catch (Exception ex)
913
                            {
914
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
915
                                transaction.Rollback();
916
                                return false;
917
                            }
918
                            finally
919
                            {
920
                                transaction.Dispose();
921
                            }
922
                        }
923
                    }
924
                    catch (Exception ex)
925
                    {
926
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
927
                        return false;
928
                    }
929
                    finally
930
                    {
931
                        connection.Dispose();
932
                    }
933
                }
934
            }
935
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
936
            {
937
                using (SqlConnection connection = GetSqlConnection())
938
                {
939
                    try
940
                    {
941
                        if (connection != null && connection.State == ConnectionState.Open)
942
                        {
943
                            using (SqlCommand cmd = connection.CreateCommand())
944
                            {
945
                                foreach (var item in datas)
946
                                {
947
                                    cmd.Parameters.Clear();
948
                                    cmd.CommandText = string.Format(@"
949
                                    IF EXISTS (SELECT * FROM {0} WHERE LEVEL1 = '{1}' AND LEVEL2 = '{2}' AND ATTRIBUTE_KEY = '{3}')
950
                                        UPDATE {0} SET ATTRIBUTE_NAME = @ATTRIBUTE_NAME WHERE LEVEL1 = '{1}' AND LEVEL2 = '{2}' AND ATTRIBUTE_KEY = '{3}'
951
                                    ELSE
952
                                        INSERT INTO {0} (LEVEL1, LEVEL2, ATTRIBUTE_KEY, ATTRIBUTE_NAME) VALUES (@LEVEL1, @LEVEL2, @ATTRIBUTE_KEY, @ATTRIBUTE_NAME)",
953
                                        APID_ATTRIBUTE_TABLE, item.Item1, item.Item2, item.Item3);
954
                                    cmd.Parameters.AddWithValue("@LEVEL1", item.Item1);
955
                                    cmd.Parameters.AddWithValue("@LEVEL2", item.Item2);
956
                                    cmd.Parameters.AddWithValue("@ATTRIBUTE_KEY", item.Item3);
957
                                    cmd.Parameters.AddWithValue("@ATTRIBUTE_NAME", item.Item4);
958
                                    cmd.ExecuteNonQuery();
959
                                }
960
                            }
961
                            connection.Close();
962
                        }
963
                    }
964
                    catch (Exception ex)
965
                    {
966
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
967
                        return false;
968
                    }
969
                    finally
970
                    {
971
                        if (connection != null)
972
                            connection.Dispose();
973
                    }
974
                }
975
            }
976

    
977
            return true;
978
        }
979
        public static DataTable GetProjectAttribute()
980
        {
981
            DataTable dt = new DataTable();
982
            Project_Info projectInfo = Project_Info.GetInstance();
983
            if (projectInfo.DBType == ID2DB_Type.SQLite)
984
            {
985
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
986
                {
987
                    try
988
                    {
989
                        connection.Open();
990
                        using (SQLiteCommand cmd = connection.CreateCommand())
991
                        {
992
                            cmd.CommandText = string.Format(@"
993
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.APID_ATTRIBUTE, spa.APID_ATTRIBUTE_TYPE, sp.APID_SYMBOL, sa.Property
994
                            FROM {1} as sa, {0} as st 
995
                                 LEFT OUTER JOIN {2} as sp 
996
                                      ON sa.UID = SP.UID 
997
                                LEFT OUTER JOIN {3} as spa 
998
                                     ON sa.UID = spa.UID
999
                            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);
1000
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1001
                                dt.Load(dr);
1002
                        }
1003
                        connection.Close();
1004
                    }
1005
                    catch (Exception ex)
1006
                    {
1007
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1008
                    }
1009
                    finally
1010
                    {
1011
                        connection.Dispose();
1012
                    }
1013
                }
1014
            }
1015
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1016
            {
1017
                using (SqlConnection connection = GetSqlConnection())
1018
                {
1019
                    try
1020
                    {
1021
                        if (connection != null && connection.State == ConnectionState.Open)
1022
                        {
1023
                            using (SqlCommand cmd = connection.CreateCommand())
1024
                            {
1025
                                cmd.CommandText = string.Format(@"
1026
                            SELECT sa.UID, sa.DisplayAttribute, st.Type, spa.APID_ATTRIBUTE, spa.APID_ATTRIBUTE_TYPE, sp.APID_SYMBOL, sa.Property
1027
                            FROM {1} as sa
1028
                                 LEFT OUTER JOIN {2} as sp 
1029
                                      ON sa.UID = SP.UID 
1030
                                LEFT OUTER JOIN {3} as spa 
1031
                                     ON sa.UID = spa.UID
1032
                                LEFT OUTER JOIN {0} as st 
1033
                                     ON sa.SymbolType_UID = st.UID 
1034
                            WHERE (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, APID_SYMBOL_MAPPING_TABLE, APID_ATTRIBUTE_MAPPING_TABLE);
1035
                                using (SqlDataReader dr = cmd.ExecuteReader())
1036
                                    dt.Load(dr);
1037
                            }
1038
                            connection.Close();
1039
                        }
1040
                    }
1041
                    catch (Exception ex)
1042
                    {
1043
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1044
                    }
1045
                    finally
1046
                    {
1047
                        if (connection != null)
1048
                            connection.Dispose();
1049
                    }
1050
                }
1051
            }
1052

    
1053

    
1054
            return dt;
1055
        }
1056
        public static DataTable GetProjectLineProperties()
1057
        {
1058
            DataTable dt = new DataTable();
1059
            Project_Info projectInfo = Project_Info.GetInstance();
1060
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1061
            {
1062
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1063
                {
1064
                    try
1065
                    {
1066
                        connection.Open();
1067
                        using (SQLiteCommand cmd = connection.CreateCommand())
1068
                        {
1069
                            cmd.CommandText = string.Format(@"
1070
                            SELECT lp.UID, lp.DisplayName, sp.APID_SYMBOL, spa.APID_ATTRIBUTE
1071
                            FROM {0} as lp 
1072
                                 LEFT OUTER JOIN {1} as sp 
1073
                                      ON lp.UID = sp.UID
1074
                                 LEFT OUTER JOIN {2} as spa 
1075
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, APID_SYMBOL_MAPPING_TABLE, APID_ATTRIBUTE_MAPPING_TABLE);
1076
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1077
                                dt.Load(dr);
1078
                        }
1079
                        connection.Close();
1080
                    }
1081
                    catch (Exception ex)
1082
                    {
1083
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1084
                    }
1085
                    finally
1086
                    {
1087
                        connection.Dispose();
1088
                    }
1089
                }
1090
            }
1091
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1092
            {
1093
                using (SqlConnection connection = GetSqlConnection())
1094
                {
1095
                    try
1096
                    {
1097
                        if (connection != null && connection.State == ConnectionState.Open)
1098
                        {
1099
                            using (SqlCommand cmd = connection.CreateCommand())
1100
                            {
1101
                                cmd.CommandText = string.Format(@"
1102
                            SELECT lp.UID, lp.DisplayName, sp.APID_SYMBOL, spa.APID_ATTRIBUTE
1103
                            FROM {0} as lp 
1104
                                 LEFT OUTER JOIN {1} as sp 
1105
                                      ON lp.UID = sp.UID
1106
                                 LEFT OUTER JOIN {2} as spa 
1107
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, APID_SYMBOL_MAPPING_TABLE, APID_ATTRIBUTE_MAPPING_TABLE);
1108
                                using (SqlDataReader dr = cmd.ExecuteReader())
1109
                                    dt.Load(dr);
1110
                            }
1111
                            connection.Close();
1112
                        }
1113
                    }
1114
                    catch (Exception ex)
1115
                    {
1116
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1117
                    }
1118
                    finally
1119
                    {
1120
                        if (connection != null)
1121
                            connection.Dispose();
1122
                    }
1123
                }
1124
            }
1125

    
1126
            return dt;
1127
        }
1128
        public static DataTable GetSymbolMappingTableOnlySymbol()
1129
        {
1130
            DataTable dt = new DataTable();
1131
            Project_Info projectInfo = Project_Info.GetInstance();
1132
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1133
            {
1134
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1135
                {
1136
                    try
1137
                    {
1138
                        connection.Open();
1139
                        using (SQLiteCommand cmd = connection.CreateCommand())
1140
                        {
1141
                            cmd.CommandText = string.Format("SELECT * FROM {0}", APID_SYMBOL_MAPPING_TABLE);
1142
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1143
                                dt.Load(dr);
1144
                        }
1145
                        connection.Close();
1146
                    }
1147
                    catch (Exception ex)
1148
                    {
1149
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1150
                    }
1151
                    finally
1152
                    {
1153
                        connection.Dispose();
1154
                    }
1155
                }
1156
            }
1157
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1158
            {
1159
                using (SqlConnection connection = GetSqlConnection())
1160
                {
1161
                    try
1162
                    {
1163
                        if (connection != null && connection.State == ConnectionState.Open)
1164
                        {
1165
                            using (SqlCommand cmd = connection.CreateCommand())
1166
                            {
1167
                                cmd.CommandText = string.Format("SELECT * FROM {0}", APID_SYMBOL_MAPPING_TABLE);
1168
                                using (SqlDataReader dr = cmd.ExecuteReader())
1169
                                    dt.Load(dr);
1170
                            }
1171
                            connection.Close();
1172
                        }
1173
                    }
1174
                    catch (Exception ex)
1175
                    {
1176
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1177
                    }
1178
                    finally
1179
                    {
1180
                        if (connection != null)
1181
                            connection.Dispose();
1182
                    }
1183
                }
1184
            }
1185

    
1186
            return dt;
1187
        }
1188
        public static DataTable GetAttributeMappingTableOnlyAttribute()
1189
        {
1190
            DataTable dt = new DataTable();
1191
            Project_Info projectInfo = Project_Info.GetInstance();
1192
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1193
            {
1194
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1195
                {
1196
                    try
1197
                    {
1198
                        connection.Open();
1199
                        using (SQLiteCommand cmd = connection.CreateCommand())
1200
                        {
1201
                            cmd.CommandText = string.Format("SELECT * FROM {0}", APID_ATTRIBUTE_MAPPING_TABLE);
1202
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1203
                                dt.Load(dr);
1204
                        }
1205
                        connection.Close();
1206
                    }
1207
                    catch (Exception ex)
1208
                    {
1209
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1210
                    }
1211
                    finally
1212
                    {
1213
                        connection.Dispose();
1214
                    }
1215
                }
1216
            }
1217
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1218
            {
1219
                using (SqlConnection connection = GetSqlConnection())
1220
                {
1221
                    try
1222
                    {
1223
                        if (connection != null && connection.State == ConnectionState.Open)
1224
                        {
1225
                            using (SqlCommand cmd = connection.CreateCommand())
1226
                            {
1227
                                cmd.CommandText = string.Format("SELECT * FROM {0}", APID_ATTRIBUTE_MAPPING_TABLE);
1228
                                using (SqlDataReader dr = cmd.ExecuteReader())
1229
                                    dt.Load(dr);
1230
                            }
1231
                            connection.Close();
1232
                        }
1233
                    }
1234
                    catch (Exception ex)
1235
                    {
1236
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1237
                    }
1238
                    finally
1239
                    {
1240
                        if (connection != null)
1241
                            connection.Dispose();
1242
                    }
1243
                }
1244
            }
1245

    
1246
            return dt;
1247
        }
1248
        public static DataTable GetAPIDAttributeTable()
1249
        {
1250
            DataTable dt = new DataTable();
1251
            Project_Info projectInfo = Project_Info.GetInstance();
1252
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1253
            {
1254
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1255
                {
1256
                    try
1257
                    {
1258
                        connection.Open();
1259
                        using (SQLiteCommand cmd = connection.CreateCommand())
1260
                        {
1261
                            cmd.CommandText = string.Format("SELECT * FROM {0}", APID_ATTRIBUTE_TABLE);
1262
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1263
                                dt.Load(dr);
1264
                        }
1265
                        connection.Close();
1266
                    }
1267
                    catch (Exception ex)
1268
                    {
1269
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1270
                    }
1271
                    finally
1272
                    {
1273
                        connection.Dispose();
1274
                    }
1275
                }
1276
            }
1277
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1278
            {
1279
                using (SqlConnection connection = GetSqlConnection())
1280
                {
1281
                    try
1282
                    {
1283
                        if (connection != null && connection.State == ConnectionState.Open)
1284
                        {
1285
                            using (SqlCommand cmd = connection.CreateCommand())
1286
                            {
1287
                                cmd.CommandText = string.Format("SELECT * FROM {0}", APID_ATTRIBUTE_TABLE);
1288
                                using (SqlDataReader dr = cmd.ExecuteReader())
1289
                                    dt.Load(dr);
1290
                            }
1291
                            connection.Close();
1292
                        }
1293
                    }
1294
                    catch (Exception ex)
1295
                    {
1296
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1297
                    }
1298
                    finally
1299
                    {
1300
                        if (connection != null)
1301
                            connection.Dispose();
1302
                    }
1303
                }
1304
            }
1305

    
1306
            return dt;
1307
        }
1308
        #endregion
1309

    
1310
        #region AVEVA
1311
        public static string GetAvevaConnectionString()
1312
        {
1313
            string strConn = string.Empty;
1314
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
1315
            {
1316
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);
1317
            }
1318
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
1319
            {
1320
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
1321
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
1322
            }
1323

    
1324
            return strConn;
1325
        }
1326
        public static string GetAvevaConnectionString_Reports()
1327
        {
1328
            string strConn = string.Empty;
1329
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
1330
            {
1331
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
1332
            }
1333
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
1334
            {
1335
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
1336
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
1337
            }
1338

    
1339
            return strConn;
1340
        }
1341
        public static string GetAvevaConnectionString_Admin()
1342
        {
1343
            string strConn = string.Empty;
1344
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
1345
            {
1346
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName);
1347
            }
1348
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
1349
            {
1350
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
1351
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString);
1352
            }
1353

    
1354
            return strConn;
1355
        }
1356
        public static string GetDirectiveValue(string name)
1357
        {
1358
            string result = null;
1359

    
1360
            using (SqlConnection connection = new SqlConnection())
1361
            {
1362
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1363
                connection.Open();
1364
                using (SqlCommand cmd = connection.CreateCommand())
1365
                {
1366
                    cmd.CommandText = string.Format("select DirectiveValue from ProjectDirectives where DirectiveName = '{0}'", name);
1367
                    using (SqlDataReader dr = cmd.ExecuteReader())
1368
                    {
1369
                        if (dr.Read())
1370
                            result = dr.GetString(0);
1371
                    }
1372
                }
1373
                connection.Close();
1374
            }
1375

    
1376
            return result;
1377
        }
1378
        public static DataTable GetDrawingTemplate()
1379
        {
1380
            DataTable dt = new DataTable();
1381

    
1382
            using (SqlConnection connection = new SqlConnection())
1383
            {
1384
                connection.ConnectionString = GetAvevaConnectionString_Reports();
1385
                connection.Open();
1386
                using (SqlCommand cmd = connection.CreateCommand())
1387
                {
1388
                    cmd.CommandText = "select ID, Name,IsDWG,DateCreation,MachineName,FutureUse1,FutureUse2 from DrawingTemplates where IsDWG=0";
1389
                    using (SqlDataReader dr = cmd.ExecuteReader())
1390
                        dt.Load(dr);
1391
                }
1392
                connection.Close();
1393
            }
1394

    
1395
            return dt;
1396
        }
1397
        public static DataTable SelectStandardSymbolTable()
1398
        {
1399
            DataTable dt = new DataTable();
1400

    
1401
            using (SqlConnection connection = new SqlConnection())
1402
            {
1403
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1404
                connection.Open();
1405
                using (SqlCommand cmd = connection.CreateCommand())
1406
                {
1407
                    cmd.CommandText = string.Format(@"
1408
                    SELECT ss.ID, ss.Symbol_Name, sps.Relative_Path FROM {0} as ss
1409
                    LEFT OUTER JOIN {1} as sps
1410
                    ON ss.Relative_Path = sps.ID
1411
                    ORDER BY sps.ID ASC
1412
                    ;", "stdSymbols", "StdPrjFolStructure");
1413

    
1414
                    using (SqlDataReader dr = cmd.ExecuteReader())
1415
                        dt.Load(dr);
1416
                }
1417
                connection.Close();
1418
            }
1419

    
1420
            return dt;
1421
        }
1422
        public static DataTable SelectStandardSymbolStructureTable()
1423
        {
1424
            DataTable dt = new DataTable();
1425

    
1426
            using (SqlConnection connection = new SqlConnection())
1427
            {
1428
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1429
                connection.Open();
1430
                using (SqlCommand cmd = connection.CreateCommand())
1431
                {
1432
                    cmd.CommandText = string.Format(@"
1433
                    SELECT * FROM {0}
1434
                    ;", "StdPrjFolStructure");
1435

    
1436
                    using (SqlDataReader dr = cmd.ExecuteReader())
1437
                        dt.Load(dr);
1438
                }
1439
                connection.Close();
1440
            }
1441

    
1442
            return dt;
1443
        }
1444
        public static DataTable SelectStandardSymbolImageTable()
1445
        {
1446
            DataTable dt = new DataTable();
1447

    
1448
            using (SqlConnection connection = new SqlConnection())
1449
            {
1450
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1451
                connection.Open();
1452
                using (SqlCommand cmd = connection.CreateCommand())
1453
                {
1454
                    cmd.CommandText = string.Format(@"
1455
                    SELECT ss.Symbol_Name, ss.Pic_Binary, sps.Relative_Path FROM {0} as ss
1456
                    LEFT OUTER JOIN {1} as sps
1457
                    ON ss.Relative_Path = sps.ID
1458
                    ORDER BY sps.ID ASC
1459
                    ;", "stdSymbols", "StdPrjFolStructure");
1460

    
1461
                    using (SqlDataReader dr = cmd.ExecuteReader())
1462
                        dt.Load(dr);
1463
                }
1464
                connection.Close();
1465
            }
1466

    
1467
            return dt;
1468
        }
1469
        public static DataTable SelectDrawingTable()
1470
        {
1471
            DataTable dt = new DataTable();
1472

    
1473
            using (SqlConnection connection = new SqlConnection())
1474
            {
1475
                connection.ConnectionString = GetAvevaConnectionString_Reports();
1476
                connection.Open();
1477
                using (SqlCommand cmd = connection.CreateCommand())
1478
                {
1479
                    cmd.CommandText = string.Format(@"
1480
                    SELECT DrawingId, XDNLABEL, XDSLABEL FROM {0}
1481
                    ;", "SynchroniseDetails");
1482

    
1483
                    using (SqlDataReader dr = cmd.ExecuteReader())
1484
                        dt.Load(dr);
1485
                }
1486
                connection.Close();
1487
            }
1488

    
1489
            return dt;
1490
        }
1491
        public static DataTable SelectSymbolTable()
1492
        {
1493
            DataTable dt = new DataTable();
1494

    
1495
            using (SqlConnection connection = new SqlConnection())
1496
            {
1497
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1498
                connection.Open();
1499
                using (SqlCommand cmd = connection.CreateCommand())
1500
                {
1501
                    cmd.CommandText = string.Format(@"
1502
                    SELECT * FROM {0}
1503
                    ;", "SYMBOLS");
1504

    
1505
                    using (SqlDataReader dr = cmd.ExecuteReader())
1506
                        dt.Load(dr);
1507
                }
1508
                connection.Close();
1509
            }
1510

    
1511
            return dt;
1512
        }
1513
        public static DataTable SelectUDADetails()
1514
        {
1515
            DataTable dt = new DataTable();
1516

    
1517
            using (SqlConnection connection = new SqlConnection())
1518
            {
1519
                connection.ConnectionString = GetAvevaConnectionString_Admin();
1520
                connection.Open();
1521
                using (SqlCommand cmd = connection.CreateCommand())
1522
                {
1523
                    cmd.CommandText = string.Format(@"
1524
                    SELECT * FROM {0}
1525
                    ;", "UDADetails");
1526

    
1527
                    using (SqlDataReader dr = cmd.ExecuteReader())
1528
                        dt.Load(dr);
1529
                }
1530
                connection.Close();
1531
            }
1532

    
1533
            return dt;
1534
        }
1535
        #endregion
1536

    
1537
    }
1538
}
클립보드 이미지 추가 (최대 크기: 500 MB)