프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / DB / Project_DB.cs @ 0249157a

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

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

    
1050

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

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

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

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

    
1303
            return dt;
1304
        }
1305
        #endregion
1306

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

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

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

    
1351
            return strConn;
1352
        }
1353
        public static string GetDirectiveValue(string name)
1354
        {
1355
            string result = null;
1356

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

    
1373
            return result;
1374
        }
1375
        public static DataTable GetDrawingTemplate()
1376
        {
1377
            DataTable dt = new DataTable();
1378

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

    
1392
            return dt;
1393
        }
1394
        public static DataTable SelectStandardSymbolTable()
1395
        {
1396
            DataTable dt = new DataTable();
1397

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

    
1411
                    using (SqlDataReader dr = cmd.ExecuteReader())
1412
                        dt.Load(dr);
1413
                }
1414
                connection.Close();
1415
            }
1416

    
1417
            return dt;
1418
        }
1419
        public static DataTable SelectStandardSymbolStructureTable()
1420
        {
1421
            DataTable dt = new DataTable();
1422

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

    
1433
                    using (SqlDataReader dr = cmd.ExecuteReader())
1434
                        dt.Load(dr);
1435
                }
1436
                connection.Close();
1437
            }
1438

    
1439
            return dt;
1440
        }
1441
        public static DataTable SelectStandardSymbolImageTable()
1442
        {
1443
            DataTable dt = new DataTable();
1444

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

    
1458
                    using (SqlDataReader dr = cmd.ExecuteReader())
1459
                        dt.Load(dr);
1460
                }
1461
                connection.Close();
1462
            }
1463

    
1464
            return dt;
1465
        }
1466
        public static DataTable SelectDrawingTable()
1467
        {
1468
            DataTable dt = new DataTable();
1469

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

    
1480
                    using (SqlDataReader dr = cmd.ExecuteReader())
1481
                        dt.Load(dr);
1482
                }
1483
                connection.Close();
1484
            }
1485

    
1486
            return dt;
1487
        }
1488
        public static DataTable SelectSymbolTable()
1489
        {
1490
            DataTable dt = new DataTable();
1491

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

    
1502
                    using (SqlDataReader dr = cmd.ExecuteReader())
1503
                        dt.Load(dr);
1504
                }
1505
                connection.Close();
1506
            }
1507

    
1508
            return dt;
1509
        }
1510
        public static DataTable SelectUDADetails()
1511
        {
1512
            DataTable dt = new DataTable();
1513

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

    
1524
                    using (SqlDataReader dr = cmd.ExecuteReader())
1525
                        dt.Load(dr);
1526
                }
1527
                connection.Close();
1528
            }
1529

    
1530
            return dt;
1531
        }
1532
        #endregion
1533

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