프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / DB / Project_DB.cs @ 8847ea67

이력 | 보기 | 이력해설 | 다운로드 (62 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 Newtonsoft.Json;
11

    
12
namespace Converter.BaseModel
13
{
14
    public class Project_DB
15
    {
16
        const string SPPID_DB_INFO_TABLE = "T_SPPID_CONNECTION_INFO";
17
        const string SPPID_SYMBOL_MAPPING_TABLE = "T_SPPID_SYMBOL_MAPPING";
18
        const string SPPID_ATTRIBUTE_MAPPING_TABLE = "T_SPPID_ATTRIBUTE_MAPPING";
19
        const string SPPID_SETTING_TABLE = "T_SPPID_SETTING_TABLE";
20
        const string SPPID_LABEL_INFO_TABLE = "T_SPPID_LABEL_INFO";
21
        const string SPPID_DRAWING_INFO = "T_SPPID_DRAWING_INFO";
22
        const string SPPID_OPC_INFO = "T_SPPID_OPC_INFO";
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
        private static SqlConnection GetSqlConnection()
32
        {
33
            Project_Info projectInfo = Project_Info.GetInstance();
34
            SqlConnection connection = null;
35
            try
36
            {
37
                connection = new SqlConnection(string.Format(CultureInfo.CurrentCulture,
38
                    @"Data Source = {0},{1}; Initial Catalog = {2}; User ID = {3}; Password = {4};",
39
                    projectInfo.ServerIP,
40
                    projectInfo.Port,
41
                    System.IO.Path.GetFileName(projectInfo.DefaultPath),
42
                    projectInfo.DBUser,
43
                    projectInfo.DBPassword));
44

    
45
                connection.Open();
46
            }
47
            catch (Exception ex)
48
            {
49
                connection.Dispose();
50
                connection = null;
51
            }
52

    
53
            return connection;
54
        }
55

    
56
        public static bool ConnTestAndCreateTable()
57
        {
58
            bool result = false;
59
            Project_Info projectInfo = Project_Info.GetInstance();
60
            if (projectInfo.DBType == ID2DB_Type.SQLite)
61
            {
62
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath)))
63
                {
64
                    try
65
                    {
66
                        connection.Open();
67
                        if (connection.State == ConnectionState.Open)
68
                        {
69
                            using (SQLiteCommand cmd = connection.CreateCommand())
70
                            {
71
                                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
72
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
73
                                using (DataTable dt = new DataTable())
74
                                {
75
                                    dt.Load(dr);
76

    
77
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
78
                                    {
79
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE);
80
                                        cmd.ExecuteNonQuery();
81
                                    }
82
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
83
                                    {
84
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT, SettingType TEXT)", SPPID_SETTING_TABLE);
85
                                        cmd.ExecuteNonQuery();
86
                                    }
87
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
88
                                    {
89
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, SPPID_SYMBOL_PATH TEXT, LEADERLINE BOOLEAN)", SPPID_SYMBOL_MAPPING_TABLE);
90
                                        cmd.ExecuteNonQuery();
91
                                    }
92
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
93
                                    {
94
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, SPPID_ATTRIBUTE TEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
95
                                        cmd.ExecuteNonQuery();
96
                                    }
97
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
98
                                    {
99
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BOOLEAN)", SPPID_LABEL_INFO_TABLE);
100
                                        cmd.ExecuteNonQuery();
101
                                    }
102
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
103
                                    {
104
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
105
                                        cmd.ExecuteNonQuery();
106
                                    }
107
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
108
                                    {
109
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID TEXT PRIMARY KEY, SPPID_OPC_MODELITEM_ID TEXT, ID2_DRAWING_UID TEXT, ATTRIBUTES TEXT, PAIRED BOOL)", SPPID_OPC_INFO);
110
                                        cmd.ExecuteNonQuery();
111
                                    }
112
                                }
113

    
114
                                #region Check Column 업데이트시 예비용
115
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
116
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
117
                                using (DataTable dt = new DataTable())
118
                                {
119
                                    dt.Load(dr);
120
                                    if (!dt.Columns.Contains("LEADERLINE"))
121
                                    {
122
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN LEADERLINE BOOLEAN", SPPID_SYMBOL_MAPPING_TABLE);
123
                                        cmd.ExecuteNonQuery();
124
                                    }
125
                                }
126

    
127
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
128
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
129
                                using (DataTable dt = new DataTable())
130
                                {
131
                                    dt.Load(dr);
132
                                    if (!dt.Columns.Contains("SettingType"))
133
                                    {
134
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN SettingType Text", SPPID_SETTING_TABLE);
135
                                        cmd.ExecuteNonQuery();
136
                                    }
137
                                }
138

    
139
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
140
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
141
                                using (DataTable dt = new DataTable())
142
                                {
143
                                    dt.Load(dr);
144
                                    if (dt.Columns.Contains("DOCUMENT"))
145
                                    {
146
                                        cmd.CommandText = string.Format("DROP TABLE {0}", SPPID_DRAWING_INFO);
147
                                        cmd.ExecuteNonQuery();
148

    
149
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
150
                                        cmd.ExecuteNonQuery();
151
                                    }
152
                                }
153
                                #endregion
154
                            }
155
                            result = true;
156
                        }
157
                        connection.Close();
158
                    }
159
                    catch (Exception ex)
160
                    {
161

    
162
                    }
163
                    finally
164
                    {
165
                        connection.Dispose();
166
                    }
167
                }
168
            }
169
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
170
            {
171
                using (SqlConnection connection = GetSqlConnection())
172
                {
173
                    try
174
                    {
175
                        if (connection != null && connection.State == ConnectionState.Open)
176
                        {
177
                            using (SqlCommand cmd = connection.CreateCommand())
178
                            {
179
                                cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES";
180
                                using (SqlDataReader dr = cmd.ExecuteReader())
181
                                using (DataTable dt = new DataTable())
182
                                {
183
                                    dt.Load(dr);
184

    
185
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
186
                                    {
187
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(255))", SPPID_DB_INFO_TABLE);
188
                                        cmd.ExecuteNonQuery();
189
                                    }
190
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
191
                                    {
192
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(255), SettingType varchar(255))", SPPID_SETTING_TABLE);
193
                                        cmd.ExecuteNonQuery();
194
                                    }
195
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
196
                                    {
197
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(255), SPPID_SYMBOL_PATH varchar(255), LEADERLINE BIT)", SPPID_SYMBOL_MAPPING_TABLE);
198
                                        cmd.ExecuteNonQuery();
199
                                    }
200
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
201
                                    {
202
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, SPPID_ATTRIBUTE varchar(255))", SPPID_ATTRIBUTE_MAPPING_TABLE);
203
                                        cmd.ExecuteNonQuery();
204
                                    }
205
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
206
                                    {
207
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BIT)", SPPID_LABEL_INFO_TABLE);
208
                                        cmd.ExecuteNonQuery();
209
                                    }
210
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
211
                                    {
212
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID varchar(255) PRIMARY KEY, PATH varchar(255), DRAWINGNUMBER varchar(255), DRAWINGNAME varchar(255))", SPPID_DRAWING_INFO);
213
                                        cmd.ExecuteNonQuery();
214
                                    }
215
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
216
                                    {
217
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID varchar(255) PRIMARY KEY, SPPID_OPC_MODELITEM_ID varchar(255), ID2_DRAWING_UID varchar(255), ATTRIBUTES varchar(255), PAIRED BIT)", SPPID_OPC_INFO);
218
                                        cmd.ExecuteNonQuery();
219
                                    }
220
                                }
221

    
222
                                #region Check Column 업데이트시 예비용
223
                                #endregion
224
                            }
225
                            result = true;
226
                        }
227
                    }
228
                    catch (Exception ex)
229
                    {
230

    
231
                    }
232
                    finally
233
                    {
234
                        if (connection != null)
235
                            connection.Dispose();
236
                    }
237
                }
238
            }
239

    
240
            return result;
241
        }
242

    
243
        public static bool SaveSPPID_DB_INFO(string jsonString)
244
        {
245
            Project_Info projectInfo = Project_Info.GetInstance();
246
            if (projectInfo.DBType == ID2DB_Type.SQLite)
247
            {
248
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
249
                {
250

    
251
                    try
252
                    {
253
                        connection.Open();
254
                        using (SQLiteCommand cmd = connection.CreateCommand())
255
                        {
256
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
257
                            cmd.ExecuteNonQuery();
258

    
259
                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
260
                            cmd.Parameters.AddWithValue("@jsonString", jsonString);
261
                            cmd.ExecuteNonQuery();
262
                        }
263
                        connection.Close();
264
                    }
265
                    catch (Exception ex)
266
                    {
267
                        return false;
268
                    }
269
                    finally
270
                    {
271
                        connection.Dispose();
272
                    }
273
                }
274
            }
275
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
276
            {
277
                using (SqlConnection connection = GetSqlConnection())
278
                {
279
                    try
280
                    {
281
                        if (connection != null && connection.State == ConnectionState.Open)
282
                        {
283

    
284
                        }
285
                    }
286
                    catch (Exception ex)
287
                    {
288

    
289
                    }
290
                    finally
291
                    {
292
                        if (connection != null)
293
                            connection.Dispose();
294
                    }
295
                }
296
            }
297

    
298

    
299
            return true;
300
        }
301

    
302
        public static DataTable SelectSPPID_DB_INFO()
303
        {
304
            DataTable dt = new DataTable();
305
            Project_Info projectInfo = Project_Info.GetInstance();
306
            if (projectInfo.DBType == ID2DB_Type.SQLite)
307
            {
308
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
309
                {
310
                    try
311
                    {
312
                        connection.Open();
313
                        using (SQLiteCommand cmd = connection.CreateCommand())
314
                        {
315
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
316
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
317
                                dt.Load(dr);
318
                        }
319
                        connection.Close();
320
                    }
321
                    catch (Exception ex)
322
                    {
323

    
324
                    }
325
                    finally
326
                    {
327
                        connection.Dispose();
328
                    }
329
                }
330
            }
331
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
332
            {
333
                using (SqlConnection connection = GetSqlConnection())
334
                {
335
                    try
336
                    {
337
                        if (connection != null && connection.State == ConnectionState.Open)
338
                        {
339

    
340
                        }
341
                    }
342
                    catch (Exception ex)
343
                    {
344

    
345
                    }
346
                    finally
347
                    {
348
                        if (connection != null)
349
                            connection.Dispose();
350
                    }
351
                }
352
            }
353

    
354
            return dt;
355
        }
356

    
357
        public static bool SaveETCSetting(Dictionary<string,string> dicSetting)
358
        {
359
            Project_Info projectInfo = Project_Info.GetInstance();
360
            if (projectInfo.DBType == ID2DB_Type.SQLite)
361
            {
362
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
363
                {
364

    
365
                    try
366
                    {
367
                        connection.Open();
368
                        using (SQLiteCommand cmd = connection.CreateCommand())
369
                        {
370
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
371
                            cmd.ExecuteNonQuery();
372

    
373
                            foreach (var item in dicSetting)
374
                            {
375
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
376
                                cmd.Parameters.AddWithValue("@jsonString", item.Value);
377
                                cmd.Parameters.AddWithValue("@SettingType", item.Key);
378
                                cmd.ExecuteNonQuery();
379
                            }
380
                        }
381
                        connection.Close();
382
                    }
383
                    catch (Exception ex)
384
                    {
385
                        return false;
386
                    }
387
                    finally
388
                    {
389
                        connection.Dispose();
390
                    }
391
                }
392
            }
393
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
394
            {
395
                using (SqlConnection connection = GetSqlConnection())
396
                {
397
                    try
398
                    {
399
                        if (connection != null && connection.State == ConnectionState.Open)
400
                        {
401

    
402
                        }
403
                    }
404
                    catch (Exception ex)
405
                    {
406

    
407
                    }
408
                    finally
409
                    {
410
                        if (connection != null)
411
                            connection.Dispose();
412
                    }
413
                }
414
            }
415

    
416
            return true;
417
        }
418

    
419
        public static DataTable SelectSetting()
420
        {
421
            DataTable dt = new DataTable();
422
            Project_Info projectInfo = Project_Info.GetInstance();
423
            if (projectInfo.DBType == ID2DB_Type.SQLite)
424
            {
425
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
426
                {
427
                    try
428
                    {
429
                        connection.Open();
430
                        using (SQLiteCommand cmd = connection.CreateCommand())
431
                        {
432
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
433
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
434
                                dt.Load(dr);
435
                        }
436
                        connection.Close();
437
                    }
438
                    catch (Exception ex)
439
                    {
440

    
441
                    }
442
                    finally
443
                    {
444
                        connection.Dispose();
445
                    }
446
                }
447
            }
448
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
449
            {
450
                using (SqlConnection connection = GetSqlConnection())
451
                {
452
                    try
453
                    {
454
                        if (connection != null && connection.State == ConnectionState.Open)
455
                        {
456

    
457
                        }
458
                    }
459
                    catch (Exception ex)
460
                    {
461

    
462
                    }
463
                    finally
464
                    {
465
                        if (connection != null)
466
                            connection.Dispose();
467
                    }
468
                }
469
            }
470

    
471
            return dt;
472
        }
473

    
474
        public static DataTable SelectProjectSymbol()
475
        {
476
            DataTable dt = new DataTable();
477
            Project_Info projectInfo = Project_Info.GetInstance();
478
            if (projectInfo.DBType == ID2DB_Type.SQLite)
479
            {
480
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
481
                {
482
                    try
483
                    {
484
                        connection.Open();
485
                        using (SQLiteCommand cmd = connection.CreateCommand())
486
                        {
487
                            cmd.CommandText = string.Format(@"
488
                            SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
489
                                LEFT OUTER JOIN {2} as sp 
490
                                    ON s.UID = SP.UID 
491
                            WHERE s.SymbolType_UID = st.UID 
492
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
493
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
494
                                dt.Load(dr);
495

    
496
                            DataTable dtClone = dt.Clone();
497
                            dtClone.Columns["UID"].DataType = typeof(string);
498
                            foreach (DataRow row in dt.Rows)
499
                            {
500
                                dtClone.ImportRow(row);
501
                            }
502
                            dt.Dispose();
503
                            dt = dtClone;
504
                        }
505
                        connection.Close();
506
                    }
507
                    catch (Exception ex)
508
                    {
509

    
510
                    }
511
                    finally
512
                    {
513
                        connection.Dispose();
514
                    }
515
                }
516
            }
517
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
518
            {
519
                using (SqlConnection connection = GetSqlConnection())
520
                {
521
                    try
522
                    {
523
                        if (connection != null && connection.State == ConnectionState.Open)
524
                        {
525

    
526
                        }
527
                    }
528
                    catch (Exception ex)
529
                    {
530

    
531
                    }
532
                    finally
533
                    {
534
                        if (connection != null)
535
                            connection.Dispose();
536
                    }
537
                }
538
            }
539

    
540
            return dt;
541
        }
542

    
543
        public static DataTable SelectProjectChildSymbol()
544
        {
545
            DataTable result = new DataTable();
546
            result.Columns.Add("UID");
547
            result.Columns.Add("Name");
548
            result.Columns.Add("Type");
549
            result.Columns.Add("SPPID_SYMBOL_PATH");
550
            
551
            Project_Info projectInfo = Project_Info.GetInstance();
552
            if (projectInfo.DBType == ID2DB_Type.SQLite)
553
            {
554
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
555
                using (DataTable dt = new DataTable())
556
                {
557
                    try
558
                    {
559
                        connection.Open();
560
                        using (SQLiteCommand cmd = connection.CreateCommand())
561
                        {
562
                            cmd.CommandText = string.Format(@"
563
                            SELECT AdditionalSymbol FROM Symbol");
564
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
565
                                dt.Load(dr);
566
                            List<string> childList = new List<string>();
567
                            foreach (DataRow row in dt.Rows)
568
                            {
569
                                if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
570
                                {
571
                                    string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
572
                                    foreach (var childString in array)
573
                                    {
574
                                        childList.Add(childString.Split(new char[] { ',' })[2]);
575
                                    }
576
                                }
577

    
578
                            }
579

    
580
                            dt.Clear();
581
                            cmd.Reset();
582
                            cmd.CommandText = string.Format(@"
583
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
584
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
585
                                dt.Load(dr);
586

    
587
                            childList = childList.Distinct().ToList();
588
                            foreach (var child in childList)
589
                            {
590
                                string mappingPath = string.Empty;
591
                                DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
592
                                if (rows.Length == 1)
593
                                    mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
594

    
595
                                DataRow newRow = result.NewRow();
596
                                newRow["UID"] = child;
597
                                newRow["Name"] = child;
598
                                newRow["Type"] = "Child Symbol";
599
                                newRow["SPPID_SYMBOL_PATH"] = mappingPath;
600
                                result.Rows.Add(newRow);
601
                            }
602
                        }
603
                        connection.Close();
604
                    }
605
                    catch (Exception ex)
606
                    {
607

    
608
                    }
609
                    finally
610
                    {
611
                        connection.Dispose();
612
                    }
613
                }
614
            }
615
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
616
            {
617
                using (SqlConnection connection = GetSqlConnection())
618
                {
619
                    try
620
                    {
621
                        if (connection != null && connection.State == ConnectionState.Open)
622
                        {
623

    
624
                        }
625
                    }
626
                    catch (Exception ex)
627
                    {
628

    
629
                    }
630
                    finally
631
                    {
632
                        if (connection != null)
633
                            connection.Dispose();
634
                    }
635
                }
636
            }
637
            return result;
638
        }
639

    
640
        public static DataTable SelectProjectLine()
641
        {
642
            DataTable dt = new DataTable();
643
            Project_Info projectInfo = Project_Info.GetInstance();
644
            if (projectInfo.DBType == ID2DB_Type.SQLite)
645
            {
646
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
647
                {
648
                    try
649
                    {
650
                        connection.Open();
651
                        using (SQLiteCommand cmd = connection.CreateCommand())
652
                        {
653
                            cmd.CommandText = string.Format(@"
654
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
655
                                LEFT OUTER JOIN {1} as sp 
656
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
657
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
658
                                dt.Load(dr);
659
                        }
660
                        connection.Close();
661
                    }
662
                    catch (Exception ex)
663
                    {
664

    
665
                    }
666
                    finally
667
                    {
668
                        connection.Dispose();
669
                    }
670
                }
671
            }
672
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
673
            {
674
                using (SqlConnection connection = GetSqlConnection())
675
                {
676
                    try
677
                    {
678
                        if (connection != null && connection.State == ConnectionState.Open)
679
                        {
680

    
681
                        }
682
                    }
683
                    catch (Exception ex)
684
                    {
685

    
686
                    }
687
                    finally
688
                    {
689
                        if (connection != null)
690
                            connection.Dispose();
691
                    }
692
                }
693
            }
694

    
695
            return dt;
696
        }
697

    
698
        public static DataTable SelectProjectLineProperties()
699
        {
700
            DataTable dt = new DataTable();
701
            Project_Info projectInfo = Project_Info.GetInstance();
702
            if (projectInfo.DBType == ID2DB_Type.SQLite)
703
            {
704
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
705
                {
706
                    try
707
                    {
708
                        connection.Open();
709
                        using (SQLiteCommand cmd = connection.CreateCommand())
710
                        {
711
                            cmd.CommandText = string.Format(@"
712
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
713
                            FROM {0} as lp 
714
                                 LEFT OUTER JOIN {1} as sp 
715
                                      ON lp.UID = sp.UID
716
                                 LEFT OUTER JOIN {2} as spa 
717
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
718
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
719
                                dt.Load(dr);
720
                        }
721
                        connection.Close();
722
                    }
723
                    catch (Exception ex)
724
                    {
725

    
726
                    }
727
                    finally
728
                    {
729
                        connection.Dispose();
730
                    }
731
                }
732
            }
733
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
734
            {
735
                using (SqlConnection connection = GetSqlConnection())
736
                {
737
                    try
738
                    {
739
                        if (connection != null && connection.State == ConnectionState.Open)
740
                        {
741

    
742
                        }
743
                    }
744
                    catch (Exception ex)
745
                    {
746

    
747
                    }
748
                    finally
749
                    {
750
                        if (connection != null)
751
                            connection.Dispose();
752
                    }
753
                }
754
            }
755

    
756
            return dt;
757
        }
758

    
759
        public static DataTable SelectProjectAttribute()
760
        {
761
            DataTable dt = new DataTable();
762
            Project_Info projectInfo = Project_Info.GetInstance();
763
            if (projectInfo.DBType == ID2DB_Type.SQLite)
764
            {
765
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
766
                {
767
                    try
768
                    {
769
                        connection.Open();
770
                        using (SQLiteCommand cmd = connection.CreateCommand())
771
                        {
772
                            cmd.CommandText = string.Format(@"
773
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spl.LOCATION, spl.LEADERLINE, sa.Property
774
                            FROM {1} as sa, {0} as st 
775
                                 LEFT OUTER JOIN {2} as sp 
776
                                      ON sa.UID = SP.UID 
777
                                LEFT OUTER JOIN {3} as spa 
778
                                     ON sa.UID = spa.UID
779
                                LEFT OUTER JOIN {4} as spl 
780
                                     ON sa.UID = spl.UID
781
                            WHERE sa.SymbolType_UID = st.UID AND (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE, SPPID_LABEL_INFO_TABLE);
782
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
783
                                dt.Load(dr);
784
                        }
785
                        connection.Close();
786
                    }
787
                    catch (Exception ex)
788
                    {
789

    
790
                    }
791
                    finally
792
                    {
793
                        connection.Dispose();
794
                    }
795
                }
796
            }
797
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
798
            {
799
                using (SqlConnection connection = GetSqlConnection())
800
                {
801
                    try
802
                    {
803
                        if (connection != null && connection.State == ConnectionState.Open)
804
                        {
805

    
806
                        }
807
                    }
808
                    catch (Exception ex)
809
                    {
810

    
811
                    }
812
                    finally
813
                    {
814
                        if (connection != null)
815
                            connection.Dispose();
816
                    }
817
                }
818
            }
819

    
820

    
821
            return dt;
822
        }
823

    
824
        public static DataTable SelectID2SymbolTable()
825
        {
826
            DataTable dt = new DataTable();
827
            Project_Info projectInfo = Project_Info.GetInstance();
828
            if (projectInfo.DBType == ID2DB_Type.SQLite)
829
            {
830
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
831
                {
832
                    try
833
                    {
834
                        connection.Open();
835
                        using (SQLiteCommand cmd = connection.CreateCommand())
836
                        {
837
                            cmd.CommandText = @"SELECT * FROM Symbol";
838
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
839
                                dt.Load(dr);
840
                        }
841
                        connection.Close();
842
                    }
843
                    catch (Exception ex)
844
                    {
845

    
846
                    }
847
                    finally
848
                    {
849
                        connection.Dispose();
850
                    }
851
                }
852
            }
853
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
854
            {
855
                using (SqlConnection connection = GetSqlConnection())
856
                {
857
                    try
858
                    {
859
                        if (connection != null && connection.State == ConnectionState.Open)
860
                        {
861

    
862
                        }
863
                    }
864
                    catch (Exception ex)
865
                    {
866

    
867
                    }
868
                    finally
869
                    {
870
                        if (connection != null)
871
                            connection.Dispose();
872
                    }
873
                }
874
            }
875

    
876
            return dt;
877
        }
878

    
879
        public static DataTable SelectOPCRelations()
880
        {
881
            DataTable dt = new DataTable();
882
            Project_Info projectInfo = Project_Info.GetInstance();
883
            if (projectInfo.DBType == ID2DB_Type.SQLite)
884
            {
885
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
886
                {
887
                    try
888
                    {
889
                        connection.Open();
890
                        using (SQLiteCommand cmd = connection.CreateCommand())
891
                        {
892
                            cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
893
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
894
                                dt.Load(dr);
895
                        }
896
                        connection.Close();
897
                    }
898
                    catch (Exception ex)
899
                    {
900

    
901
                    }
902
                    finally
903
                    {
904
                        connection.Dispose();
905
                    }
906
                }
907
            }
908
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
909
            {
910
                using (SqlConnection connection = GetSqlConnection())
911
                {
912
                    try
913
                    {
914
                        if (connection != null && connection.State == ConnectionState.Open)
915
                        {
916

    
917
                        }
918
                    }
919
                    catch (Exception ex)
920
                    {
921

    
922
                    }
923
                    finally
924
                    {
925
                        if (connection != null)
926
                            connection.Dispose();
927
                    }
928
                }
929
            }
930

    
931
            return dt;
932
        }
933

    
934
        public static DataTable SelectDrawings()
935
        {
936
            DataTable dt = new DataTable();
937
            Project_Info projectInfo = Project_Info.GetInstance();
938
            if (projectInfo.DBType == ID2DB_Type.SQLite)
939
            {
940
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
941
                {
942
                    try
943
                    {
944
                        connection.Open();
945
                        using (SQLiteCommand cmd = connection.CreateCommand())
946
                        {
947
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
948
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
949
                                dt.Load(dr);
950
                        }
951
                        connection.Close();
952
                    }
953
                    catch (Exception ex)
954
                    {
955

    
956
                    }
957
                    finally
958
                    {
959
                        connection.Dispose();
960
                    }
961
                }
962
            }
963
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
964
            {
965
                using (SqlConnection connection = GetSqlConnection())
966
                {
967
                    try
968
                    {
969
                        if (connection != null && connection.State == ConnectionState.Open)
970
                        {
971

    
972
                        }
973
                    }
974
                    catch (Exception ex)
975
                    {
976

    
977
                    }
978
                    finally
979
                    {
980
                        if (connection != null)
981
                            connection.Dispose();
982
                    }
983
                }
984
            }
985

    
986
            return dt;
987
        }
988

    
989
        public static DataTable SelectOPCInfo()
990
        {
991
            DataTable dt = new DataTable();
992
            Project_Info projectInfo = Project_Info.GetInstance();
993
            if (projectInfo.DBType == ID2DB_Type.SQLite)
994
            {
995
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
996
                {
997
                    try
998
                    {
999
                        connection.Open();
1000
                        using (SQLiteCommand cmd = connection.CreateCommand())
1001
                        {
1002
                            cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO);
1003
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1004
                                dt.Load(dr);
1005
                        }
1006
                        connection.Close();
1007
                    }
1008
                    catch (Exception ex)
1009
                    {
1010

    
1011
                    }
1012
                    finally
1013
                    {
1014
                        connection.Dispose();
1015
                    }
1016
                }
1017
            }
1018
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1019
            {
1020
                using (SqlConnection connection = GetSqlConnection())
1021
                {
1022
                    try
1023
                    {
1024
                        if (connection != null && connection.State == ConnectionState.Open)
1025
                        {
1026

    
1027
                        }
1028
                    }
1029
                    catch (Exception ex)
1030
                    {
1031

    
1032
                    }
1033
                    finally
1034
                    {
1035
                        if (connection != null)
1036
                            connection.Dispose();
1037
                    }
1038
                }
1039
            }
1040

    
1041
            return dt;
1042
        }
1043

    
1044
        public static DataTable SelectSymbolType()
1045
        {
1046
            DataTable dt = new DataTable();
1047
            Project_Info projectInfo = Project_Info.GetInstance();
1048
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1049
            {
1050
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1051
                {
1052
                    try
1053
                    {
1054
                        connection.Open();
1055
                        using (SQLiteCommand cmd = connection.CreateCommand())
1056
                        {
1057
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1058
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1059
                                dt.Load(dr);
1060
                        }
1061
                        connection.Close();
1062
                    }
1063
                    catch (Exception ex)
1064
                    {
1065

    
1066
                    }
1067
                    finally
1068
                    {
1069
                        connection.Dispose();
1070
                    }
1071
                }
1072
            }
1073
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1074
            {
1075
                using (SqlConnection connection = GetSqlConnection())
1076
                {
1077
                    try
1078
                    {
1079
                        if (connection != null && connection.State == ConnectionState.Open)
1080
                        {
1081

    
1082
                        }
1083
                    }
1084
                    catch (Exception ex)
1085
                    {
1086

    
1087
                    }
1088
                    finally
1089
                    {
1090
                        if (connection != null)
1091
                            connection.Dispose();
1092
                    }
1093
                }
1094
            }
1095

    
1096
            return dt;
1097
        }
1098

    
1099
        public static DataTable SelectDrawingInfo()
1100
        {
1101
            DataTable dt = new DataTable();
1102
            Project_Info projectInfo = Project_Info.GetInstance();
1103
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1104
            {
1105
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1106
                {
1107
                    try
1108
                    {
1109
                        connection.Open();
1110
                        using (SQLiteCommand cmd = connection.CreateCommand())
1111
                        {
1112
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1113
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1114
                                dt.Load(dr);
1115
                        }
1116
                        connection.Close();
1117
                    }
1118
                    catch (Exception ex)
1119
                    {
1120

    
1121
                    }
1122
                    finally
1123
                    {
1124
                        connection.Dispose();
1125
                    }
1126
                }
1127
            }
1128
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1129
            {
1130
                using (SqlConnection connection = GetSqlConnection())
1131
                {
1132
                    try
1133
                    {
1134
                        if (connection != null && connection.State == ConnectionState.Open)
1135
                        {
1136

    
1137
                        }
1138
                    }
1139
                    catch (Exception ex)
1140
                    {
1141

    
1142
                    }
1143
                    finally
1144
                    {
1145
                        if (connection != null)
1146
                            connection.Dispose();
1147
                    }
1148
                }
1149
            }
1150

    
1151
            return dt;
1152
        }
1153

    
1154
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas)
1155
        {
1156
            Project_Info projectInfo = Project_Info.GetInstance();
1157
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1158
            {
1159
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1160
                {
1161
                    try
1162
                    {
1163
                        connection.Open();
1164
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1165
                        {
1166
                            try
1167
                            {
1168
                                using (SQLiteCommand cmd = connection.CreateCommand())
1169
                                {
1170
                                    foreach (var item in datas)
1171
                                    {
1172
                                        cmd.Parameters.Clear();
1173
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE);
1174
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1175
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1176
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1177
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1178
                                        cmd.ExecuteNonQuery();
1179
                                    }
1180
                                }
1181
                                transaction.Commit();
1182
                                connection.Close();
1183
                            }
1184
                            catch (Exception ex)
1185
                            {
1186
                                transaction.Rollback();
1187
                            }
1188
                            finally
1189
                            {
1190
                                transaction.Dispose();
1191
                            }
1192
                        }
1193
                    }
1194
                    catch (Exception ex)
1195
                    {
1196
                        return false;
1197
                    }
1198
                    finally
1199
                    {
1200
                        connection.Dispose();
1201
                    }
1202
                }
1203
            }
1204
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1205
            {
1206
                using (SqlConnection connection = GetSqlConnection())
1207
                {
1208
                    try
1209
                    {
1210
                        if (connection != null && connection.State == ConnectionState.Open)
1211
                        {
1212

    
1213
                        }
1214
                    }
1215
                    catch (Exception ex)
1216
                    {
1217

    
1218
                    }
1219
                    finally
1220
                    {
1221
                        if (connection != null)
1222
                            connection.Dispose();
1223
                    }
1224
                }
1225
            }
1226

    
1227
            return true;
1228
        }
1229

    
1230
        public static bool InsertAttributeMapping(List<Tuple<string, string>> datas)
1231
        {
1232
            Project_Info projectInfo = Project_Info.GetInstance();
1233
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1234
            {
1235
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1236
                {
1237
                    try
1238
                    {
1239
                        connection.Open();
1240
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1241
                        {
1242
                            try
1243
                            {
1244
                                using (SQLiteCommand cmd = connection.CreateCommand())
1245
                                {
1246
                                    foreach (var item in datas)
1247
                                    {
1248
                                        cmd.Parameters.Clear();
1249
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
1250
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1251
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1252
                                        cmd.ExecuteNonQuery();
1253
                                    }
1254
                                }
1255
                                transaction.Commit();
1256
                                connection.Close();
1257
                            }
1258
                            catch (Exception ex)
1259
                            {
1260
                                transaction.Rollback();
1261
                            }
1262
                            finally
1263
                            {
1264
                                transaction.Dispose();
1265
                            }
1266
                        }
1267
                    }
1268
                    catch (Exception ex)
1269
                    {
1270
                        return false;
1271
                    }
1272
                    finally
1273
                    {
1274
                        connection.Dispose();
1275
                    }
1276
                }
1277
            }
1278
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1279
            {
1280
                using (SqlConnection connection = GetSqlConnection())
1281
                {
1282
                    try
1283
                    {
1284
                        if (connection != null && connection.State == ConnectionState.Open)
1285
                        {
1286

    
1287
                        }
1288
                    }
1289
                    catch (Exception ex)
1290
                    {
1291

    
1292
                    }
1293
                    finally
1294
                    {
1295
                        if (connection != null)
1296
                            connection.Dispose();
1297
                    }
1298
                }
1299
            }
1300
            return true;
1301
        }
1302

    
1303
        public static bool InsertLabelInfoMapping(List<Tuple<string, int, bool>> datas)
1304
        {
1305
            Project_Info projectInfo = Project_Info.GetInstance();
1306
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1307
            {
1308
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1309
                {
1310
                    try
1311
                    {
1312
                        connection.Open();
1313
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1314
                        {
1315
                            try
1316
                            {
1317
                                using (SQLiteCommand cmd = connection.CreateCommand())
1318
                                {
1319
                                    foreach (var item in datas)
1320
                                    {
1321
                                        cmd.Parameters.Clear();
1322
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE);
1323
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1324
                                        cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
1325
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
1326
                                        cmd.ExecuteNonQuery();
1327
                                    }
1328
                                }
1329
                                transaction.Commit();
1330
                                connection.Close();
1331
                            }
1332
                            catch (Exception ex)
1333
                            {
1334
                                transaction.Rollback();
1335
                            }
1336
                            finally
1337
                            {
1338
                                transaction.Dispose();
1339
                            }
1340
                        }
1341
                    }
1342
                    catch (Exception ex)
1343
                    {
1344
                        return false;
1345
                    }
1346
                    finally
1347
                    {
1348
                        connection.Dispose();
1349
                    }
1350
                }
1351
            }
1352
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1353
            {
1354
                using (SqlConnection connection = GetSqlConnection())
1355
                {
1356
                    try
1357
                    {
1358
                        if (connection != null && connection.State == ConnectionState.Open)
1359
                        {
1360

    
1361
                        }
1362
                    }
1363
                    catch (Exception ex)
1364
                    {
1365

    
1366
                    }
1367
                    finally
1368
                    {
1369
                        if (connection != null)
1370
                            connection.Dispose();
1371
                    }
1372
                }
1373
            }
1374
            return true;
1375
        }
1376

    
1377
        public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document)
1378
        {
1379
            Project_Info projectInfo = Project_Info.GetInstance();
1380
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1381
            {
1382
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1383
                {
1384
                    try
1385
                    {
1386
                        connection.Open();
1387
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1388
                        {
1389
                            try
1390
                            {
1391
                                using (SQLiteCommand cmd = connection.CreateCommand())
1392
                                {
1393
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO);
1394
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1395
                                    cmd.Parameters.AddWithValue("@PATH", path);
1396
                                    cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1397
                                    cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1398
                                    cmd.ExecuteNonQuery();
1399
                                }
1400

    
1401
                                List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1402
                                foreach (var item in OPCs)
1403
                                {
1404
                                    using (SQLiteCommand cmd = connection.CreateCommand())
1405
                                    {
1406
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, ATTRIBUTES, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @ATTRIBUTES, @PAIRED)", SPPID_OPC_INFO);
1407
                                        cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1408
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1409
                                        cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1410
                                        cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1411
                                        cmd.Parameters.AddWithValue("@PAIRED", false);
1412
                                        cmd.ExecuteNonQuery();
1413
                                    }
1414
                                }
1415

    
1416
                                transaction.Commit();
1417
                                connection.Close();
1418
                            }
1419
                            catch (Exception ex)
1420
                            {
1421
                                transaction.Rollback();
1422
                            }
1423
                            finally
1424
                            {
1425
                                transaction.Dispose();
1426
                            }
1427
                        }
1428
                    }
1429
                    catch (Exception ex)
1430
                    {
1431
                        return false;
1432
                    }
1433
                    finally
1434
                    {
1435
                        connection.Dispose();
1436
                    }
1437
                }
1438
            }
1439
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1440
            {
1441
                using (SqlConnection connection = GetSqlConnection())
1442
                {
1443
                    try
1444
                    {
1445
                        if (connection != null && connection.State == ConnectionState.Open)
1446
                        {
1447

    
1448
                        }
1449
                    }
1450
                    catch (Exception ex)
1451
                    {
1452

    
1453
                    }
1454
                    finally
1455
                    {
1456
                        if (connection != null)
1457
                            connection.Dispose();
1458
                    }
1459
                }
1460
            }
1461
            return true;
1462
        }
1463

    
1464
        public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired)
1465
        {
1466
            Project_Info projectInfo = Project_Info.GetInstance();
1467
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1468
            {
1469
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1470
                {
1471
                    try
1472
                    {
1473
                        connection.Open();
1474
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1475
                        {
1476
                            try
1477
                            {
1478
                                using (SQLiteCommand cmd = connection.CreateCommand())
1479
                                {
1480
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @PAIRED)", SPPID_OPC_INFO);
1481
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
1482
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
1483
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
1484
                                    cmd.Parameters.AddWithValue("@PAIRED", Paired);
1485
                                    cmd.ExecuteNonQuery();
1486
                                }
1487

    
1488
                                transaction.Commit();
1489
                                connection.Close();
1490
                            }
1491
                            catch (Exception ex)
1492
                            {
1493
                                transaction.Rollback();
1494
                            }
1495
                            finally
1496
                            {
1497
                                transaction.Dispose();
1498
                            }
1499
                        }
1500
                    }
1501
                    catch (Exception ex)
1502
                    {
1503
                        return false;
1504
                    }
1505
                    finally
1506
                    {
1507
                        connection.Dispose();
1508
                    }
1509
                }
1510
            }
1511
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1512
            {
1513
                using (SqlConnection connection = GetSqlConnection())
1514
                {
1515
                    try
1516
                    {
1517
                        if (connection != null && connection.State == ConnectionState.Open)
1518
                        {
1519

    
1520
                        }
1521
                    }
1522
                    catch (Exception ex)
1523
                    {
1524

    
1525
                    }
1526
                    finally
1527
                    {
1528
                        if (connection != null)
1529
                            connection.Dispose();
1530
                    }
1531
                }
1532
            }
1533
            return true;
1534
        }
1535
    }
1536
}
클립보드 이미지 추가 (최대 크기: 500 MB)