프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ 1ae1a1c6

이력 | 보기 | 이력해설 | 다운로드 (84 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.Data.SQLite;
7
using System.Data;
8
using System.Globalization;
9
using System.Text.RegularExpressions;
10
using System.Data.Common;
11
using System.Data.SqlClient;
12

    
13
namespace ID2PSN
14
{
15
    public class DB 
16
    {
17
        const string PSN_TOPOLOGY_RULE = "T_PSN_TOPOLOGY_RULE";
18
        const string PSN_HEADER_SETTING = "T_PSN_HEADER_SETTING";
19
        const string PSN_VENTDRAIN_SETTING = "T_PSN_VENTDRAIN_SETTING";
20
        const string PSN_PATHITEMS = "SPPIDPathItem";
21
        const string PSN_SEQUENCEDATA = "SPPIDSequenceData";
22
        const string PSN_PIPESYSTEMNETWORK = "SPPIDPipeSystemNetwork";
23
        const string PSN_EQUIPMENT = "SPPIDEquipment";
24
        const string PSN_NOZZLE = "SPPIDNozzle";
25
        const string PSN_FLUIDCODE = "SPPIDFluidCode";
26
        const string PSN_PIPINGMATLCLASS = "SPPIDPipingMatClass";
27
        const string PSN_VIEW = "T_PSN_VIEW";
28
        const string PSN_TOPOLOGYSET = "SPPIDTopologySet";
29

    
30
        /// <summary>
31
        ///  ID2 Project.db 데이터를 가져온다. 
32
        ///  DB 접속 정보 및 DBType (Sqlite, Mssql) 정보를 가져옴
33
        ///  - JY
34
        /// </summary>
35
        /// <returns></returns>
36
        public static DataTable GetProject()
37
        {
38
            DataTable dt = new DataTable();
39
            ID2Info id2Info = ID2Info.GetInstance();
40
            try
41
            {
42
                using (SQLiteConnection connection = new SQLiteConnection(@"Data Source = C:\ProgramData\Digital PID\Project.db", true))
43
                {
44
                    connection.Open();
45
                    if (connection.State.Equals(ConnectionState.Open))
46
                    {
47
                        using (SQLiteCommand cmd = connection.CreateCommand())
48
                        {
49
                            cmd.CommandText = "SELECT p.[Id], p.Name, p.Path, s.DBTypes_UID, s.Host, s.[User], s.[Password], s.FilePath FROM DBSettings s, Projects p WHERE s.Projects_UID = p.[Id]";
50
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
51
                                dt.Load(dr);
52
                        }
53

    
54
                    }
55
                    connection.Close();
56
                }
57
            }
58
            catch (Exception ex)
59
            {
60
                System.Windows.Forms.MessageBox.Show(ex.Message);
61
            }
62

    
63
            dt.AcceptChanges();
64
            dt.DefaultView.Sort = "Name";
65
            dt = dt.DefaultView.ToTable();
66

    
67
            return dt;
68
        }
69

    
70
        /// <summary>
71
        /// Mssql 연결
72
        /// - JY
73
        /// </summary>
74
        /// <returns></returns>
75
        private static SqlConnection GetSqlConnection()
76
        {
77
            ID2Info id2Info = ID2Info.GetInstance();
78
            SqlConnection connection = null;
79
            try
80
            {
81
                //new SqlConnection(@"Data Source = {0}; Initial Catalog = {2}; User ID = {3}; Password = {4};",
82
                connection = new SqlConnection(string.Format("Server= {0}; Database= {1}; uid= {2} ; pwd= {3}",                
83
                    id2Info.ServerIP,
84
                    //id2Info.Port,
85
                    System.IO.Path.GetFileName(id2Info.DefaultPath),
86
                    id2Info.DBUser,
87
                    id2Info.DBPassword));
88

    
89
                connection.Open();
90
            }
91
            catch (Exception ex)
92
            {
93
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
94
                if (connection != null)
95
                    connection.Dispose();
96
                connection = null;
97
            }
98

    
99
            return connection;
100

    
101
        }
102

    
103
        /// <summary>
104
        ///  초기 DB 생성
105
        ///  - JY
106
        /// </summary>
107
        /// <returns></returns>
108
        public static bool ConnTestAndCreateTable()
109
        {
110
            bool result = false;
111
            ID2Info id2Info = ID2Info.GetInstance();
112

    
113
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
114
            {
115
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", id2Info.DBFilePath), true))
116
                {
117
                    try
118
                    {
119
                        connection.Open();
120
                        if (connection.State == ConnectionState.Open)
121
                        {
122
                            using (SQLiteCommand cmd = connection.CreateCommand())
123
                            {
124
                                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
125
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
126
                                using (DataTable dt = new DataTable())
127
                                {
128
                                    dt.Load(dr);
129

    
130
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_HEADER_SETTING)).Length == 0)
131
                                    {
132
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)", PSN_HEADER_SETTING);
133
                                        cmd.ExecuteNonQuery();
134
                                    }
135
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_VENTDRAIN_SETTING)).Length == 0)
136
                                    {
137
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)", PSN_VENTDRAIN_SETTING);
138
                                        cmd.ExecuteNonQuery();
139
                                    }
140
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_TOPOLOGY_RULE)).Length == 0)
141
                                    {
142
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT)", PSN_TOPOLOGY_RULE);
143
                                        cmd.ExecuteNonQuery();
144

    
145
                                        DataTable topologyRule = new DataTable();
146
                                        topologyRule.Columns.Add("NAME", typeof(string));
147

    
148
                                        topologyRule.Rows.Add("FluidCode");
149
                                        topologyRule.Rows.Add("-");
150
                                        topologyRule.Rows.Add("PipingMaterialsClass");
151
                                        topologyRule.Rows.Add("-");
152
                                        topologyRule.Rows.Add("Tag Seq No");
153

    
154
                                        SaveTopologyRule(topologyRule);
155
                                    }
156
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_PATHITEMS)).Length == 0)
157
                                    {
158
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, SequenceData_OID TEXT, TopologySet_OID TEXT, BranchTopologySet_OID TEXT, PipeLine_OID TEXT, ITEMNAME TEXT, ITEMTAG TEXT, Class TEXT, SubClass TEXT, TYPE TEXT, PIDNAME TEXT, NPD TEXT, PipeSystemNetwork_OID TEXT, ViewPipeSystemNetwork_OID TEXT, PipeRun_OID TEXT)", PSN_PATHITEMS);
159
                                        cmd.ExecuteNonQuery();
160
                                    }
161
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_SEQUENCEDATA)).Length == 0)
162
                                    {
163
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, SERIALNUMBER TEXT, PathItem_OID TEXT, TopologySet_OID_Key TEXT)", PSN_SEQUENCEDATA);
164
                                        cmd.ExecuteNonQuery();
165
                                    }
166
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_PIPESYSTEMNETWORK)).Length == 0)
167
                                    {
168
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, Type TEXT, OrderNumber TEXT, Pipeline_OID TEXT, FROM_DATA TEXT, TO_DATA TEXT, TopologySet_OID_Key TEXT, PSNRevisionNumber TEXT, PBS TEXT, PIDDrawings TEXT, Validity TEXT, Status TEXT)", PSN_PIPESYSTEMNETWORK);
169
                                        cmd.ExecuteNonQuery();
170
                                    }
171
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_EQUIPMENT)).Length == 0)
172
                                    {
173
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, ITEMTAG TEXT, XCOORDS TEXT, YCOORDS TEXT)", PSN_EQUIPMENT);
174
                                        cmd.ExecuteNonQuery();
175
                                    }
176
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_NOZZLE)).Length == 0)
177
                                    {
178
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, ITEMTAG TEXT, XCOORDS TEXT, YCOORDS TEXT, Equipment_OID TEXT, FLUID TEXT, NPD TEXT, ROTATION TEXT, FlowDirection TEXT)", PSN_NOZZLE);
179
                                        cmd.ExecuteNonQuery();
180
                                    }
181
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_FLUIDCODE)).Length == 0)
182
                                    {
183
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT, Code TEXT, Description TEXT, Condition TEXT, Remarks TEXT, GroundLevel TEXT)", PSN_FLUIDCODE);
184
                                        cmd.ExecuteNonQuery();
185
                                    }
186
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_PIPINGMATLCLASS)).Length == 0)
187
                                    {
188
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT, Priority TEXT, Code TEXT, Description TEXT, Condition TEXT, Remarks TEXT, GroundLevel TEXT)", PSN_PIPINGMATLCLASS);
189
                                        cmd.ExecuteNonQuery();
190
                                    }
191
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_VIEW)).Length == 0)
192
                                    {
193
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT)", PSN_VIEW);
194
                                        cmd.ExecuteNonQuery();
195
                                    }
196
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_TOPOLOGYSET)).Length == 0)
197
                                    {
198
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, Type TEXT, SubType TEXT, HeadItemTag TEXT, TailItemTag TEXT, HeadItemID TEXT, TailItemID TEXT)", PSN_TOPOLOGYSET);
199
                                        cmd.ExecuteNonQuery();
200
                                    }
201
                                }
202
                            }
203
                            result = true;
204
                        }
205
                        connection.Close();
206
                    }
207
                    catch (Exception ex)
208
                    {
209
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
210
                    }
211
                    finally
212
                    {
213
                        connection.Dispose();
214
                    }
215
                }
216
            }
217
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
218
            {
219
                using (SqlConnection connection = GetSqlConnection())
220
                {
221
                    try
222
                    {
223
                        if (connection != null && connection.State == ConnectionState.Open)
224
                        {
225
                            using (SqlCommand cmd = connection.CreateCommand())
226
                            {
227
                                cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES";
228
                                using (SqlDataReader dr = cmd.ExecuteReader())
229
                                using (DataTable dt = new DataTable())
230
                                {
231
                                    dt.Load(dr);
232
                                    if (dt.Select(string.Format("NAME = '{0}'", PSN_VIEW)).Length == 0) //T_PSN_VIEW - id2 zoom 용
233
                                    {
234
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT)", PSN_VIEW);
235
                                        cmd.ExecuteNonQuery();
236
                                    }
237
                                }
238
                            }
239
                            result = true;
240
                        }
241
                    }
242
                    catch (Exception ex)
243
                    {
244
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
245
                    }
246
                    finally
247
                    {
248
                        if (connection != null)
249
                            connection.Dispose();
250
                    }
251
                }
252
            }
253

    
254
            return result;
255
        }
256

    
257
        // ID2 DB 데이터
258
        public static DataTable SelectOPCRelations()
259
        {
260
            DataTable dt = new DataTable();
261
            ID2Info id2Info = ID2Info.GetInstance();
262
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
263
            {
264
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
265
                {
266
                    try
267
                    {
268
                        connection.Open();
269
                        using (SQLiteCommand cmd = connection.CreateCommand())
270
                        {
271
                            cmd.CommandText = "SELECT * FROM OPCRelations;";
272
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
273
                                dt.Load(dr);
274
                        }
275
                        connection.Close();
276
                    }
277
                    catch (Exception ex)
278
                    {
279
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
280
                    }
281
                    finally
282
                    {
283
                        connection.Dispose();
284
                    }
285
                }
286
            }
287
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
288
            {
289
                using (SqlConnection connection = GetSqlConnection())
290
                {
291
                    try
292
                    {
293
                        if (connection != null && connection.State == ConnectionState.Open)
294
                        {
295
                            using (SqlCommand cmd = connection.CreateCommand())
296
                            {
297
                                cmd.CommandText = "SELECT * FROM OPCRelations;";
298
                                using (SqlDataReader dr = cmd.ExecuteReader())
299
                                    dt.Load(dr);
300
                            }
301
                            connection.Close();
302
                        }
303
                    }
304
                    catch (Exception ex)
305
                    {
306
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
307
                    }
308
                    finally
309
                    {
310
                        if (connection != null)
311
                            connection.Dispose();
312
                    }
313
                }
314
            }
315

    
316
            return dt;
317
        }
318

    
319
        public static DataTable SelectDrawings()
320
        {
321
            DataTable dt = new DataTable();
322
            ID2Info id2Info = ID2Info.GetInstance();
323
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
324
            {
325
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
326
                {
327
                    try
328
                    {
329
                        connection.Open();
330
                        using (SQLiteCommand cmd = connection.CreateCommand())
331
                        {
332
                            cmd.CommandText = "SELECT * FROM Drawings;";
333
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
334
                                dt.Load(dr);
335
                        }
336
                        connection.Close();
337
                    }
338
                    catch (Exception ex)
339
                    {
340
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
341
                    }
342
                    finally
343
                    {
344
                        connection.Dispose();
345
                    }
346
                }
347
            }
348
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
349
            {
350
                using (SqlConnection connection = GetSqlConnection())
351
                {
352
                    try
353
                    {
354
                        if (connection != null && connection.State == ConnectionState.Open)
355
                        {
356
                            using (SqlCommand cmd = connection.CreateCommand())
357
                            {
358
                                cmd.CommandText = "SELECT * FROM Drawings;";
359
                                using (SqlDataReader dr = cmd.ExecuteReader())
360
                                    dt.Load(dr);
361
                            }
362
                            connection.Close();
363
                        }
364
                    }
365
                    catch (Exception ex)
366
                    {
367
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
368
                    }
369
                    finally
370
                    {
371
                        if (connection != null)
372
                            connection.Dispose();
373
                    }
374
                }
375
            }
376

    
377
            return dt;
378
        }
379

    
380
        public static DataTable SelectLineProperties()
381
        {
382
            DataTable dt = new DataTable();
383
            ID2Info id2Info = ID2Info.GetInstance();
384
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
385
            {
386
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
387
                {
388
                    try
389
                    {
390
                        connection.Open();
391
                        using (SQLiteCommand cmd = connection.CreateCommand())
392
                        {
393
                            cmd.CommandText = "SELECT * FROM LineProperties;";
394
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
395
                                dt.Load(dr);
396
                        }
397
                        connection.Close();
398
                    }
399
                    catch (Exception ex)
400
                    {
401
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
402
                    }
403
                    finally
404
                    {
405
                        connection.Dispose();
406
                    }
407
                }
408
            }
409
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
410
            {
411
                using (SqlConnection connection = GetSqlConnection())
412
                {
413
                    try
414
                    {
415
                        if (connection != null && connection.State == ConnectionState.Open)
416
                        {
417
                            using (SqlCommand cmd = connection.CreateCommand())
418
                            {
419
                                cmd.CommandText = "SELECT * FROM LineProperties;";
420
                                using (SqlDataReader dr = cmd.ExecuteReader())
421
                                    dt.Load(dr);
422
                            }
423
                            connection.Close();
424
                        }
425
                    }
426
                    catch (Exception ex)
427
                    {
428
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
429
                    }
430
                    finally
431
                    {
432
                        if (connection != null)
433
                            connection.Dispose();
434
                    }
435
                }
436
            }
437
            return dt;
438
        }
439
        
440
        public static DataTable SelectFluidCode()
441
        {
442
            DataTable dt = new DataTable();
443
            ID2Info id2Info = ID2Info.GetInstance();
444
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
445
            {
446
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
447
                {
448
                    try
449
                    {
450
                        connection.Open();
451
                        using (SQLiteCommand cmd = connection.CreateCommand())
452
                        {
453
                            cmd.CommandText = "SELECT * FROM FluidCode;";
454
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
455
                                dt.Load(dr);
456
                        }
457
                        connection.Close();
458
                    }
459
                    catch (Exception ex)
460
                    {
461
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
462
                    }
463
                    finally
464
                    {
465
                        connection.Dispose();
466
                    }
467
                }
468
            }
469
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
470
            {
471
                using (SqlConnection connection = GetSqlConnection())
472
                {
473
                    try
474
                    {
475
                        if (connection != null && connection.State == ConnectionState.Open)
476
                        {
477
                            using (SqlCommand cmd = connection.CreateCommand())
478
                            {
479
                                cmd.CommandText = "SELECT * FROM FluidCode;";
480
                                using (SqlDataReader dr = cmd.ExecuteReader())
481
                                    dt.Load(dr);
482
                            }
483
                            connection.Close();
484
                        }
485
                    }
486
                    catch (Exception ex)
487
                    {
488
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
489
                    }
490
                    finally
491
                    {
492
                        if (connection != null)
493
                            connection.Dispose();
494
                    }
495
                }
496
            }
497

    
498
            return dt;
499
        }
500

    
501
        public static DataTable SelectPipingMaterialsClass()
502
        {
503
            DataTable dt = new DataTable();
504
            ID2Info id2Info = ID2Info.GetInstance();
505
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
506
            {
507
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
508
                {
509
                    try
510
                    {
511
                        connection.Open();
512
                        using (SQLiteCommand cmd = connection.CreateCommand())
513
                        {
514
                            cmd.CommandText = "SELECT * FROM PipingMaterialsClass;";
515
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
516
                                dt.Load(dr);
517
                        }
518
                        connection.Close();
519
                    }
520
                    catch (Exception ex)
521
                    {
522
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
523
                    }
524
                    finally
525
                    {
526
                        connection.Dispose();
527
                    }
528
                }
529
            }
530
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
531
            {
532
                using (SqlConnection connection = GetSqlConnection())
533
                {
534
                    try
535
                    {
536
                        if (connection != null && connection.State == ConnectionState.Open)
537
                        {
538
                            using (SqlCommand cmd = connection.CreateCommand())
539
                            {
540
                                cmd.CommandText = "SELECT * FROM PipingMaterialsClass;";
541
                                using (SqlDataReader dr = cmd.ExecuteReader())
542
                                    dt.Load(dr);
543
                            }
544
                            connection.Close();
545
                        }
546
                    }
547
                    catch (Exception ex)
548
                    {
549
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
550
                    }
551
                    finally
552
                    {
553
                        if (connection != null)
554
                            connection.Dispose();
555
                    }
556
                }
557
            }
558

    
559
            return dt;
560
        }
561

    
562
        public static DataTable SelectPSNPIPINGMATLCLASS()
563
        {
564
            DataTable dt = new DataTable();
565
            ID2Info id2Info = ID2Info.GetInstance();
566
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
567
            {
568
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
569
                {
570
                    try
571
                    {
572
                        connection.Open();
573
                        using (SQLiteCommand cmd = connection.CreateCommand())
574
                        {
575
                            cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_PIPINGMATLCLASS);
576
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
577
                                dt.Load(dr);
578
                        }
579
                        connection.Close();
580
                    }
581
                    catch (Exception ex)
582
                    {
583
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
584
                    }
585
                    finally
586
                    {
587
                        connection.Dispose();
588
                    }
589
                }
590
            }
591
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
592
            {
593
                using (SqlConnection connection = GetSqlConnection())
594
                {
595
                    try
596
                    {
597
                        if (connection != null && connection.State == ConnectionState.Open)
598
                        {
599
                            using (SqlCommand cmd = connection.CreateCommand())
600
                            {
601
                                cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_PIPINGMATLCLASS);
602
                                using (SqlDataReader dr = cmd.ExecuteReader())
603
                                    dt.Load(dr);
604
                            }
605
                            connection.Close();
606
                        }
607
                    }
608
                    catch (Exception ex)
609
                    {
610
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
611
                    }
612
                    finally
613
                    {
614
                        if (connection != null)
615
                            connection.Dispose();
616
                    }
617
                }
618
            }
619

    
620
            return dt;
621
        }
622

    
623
        public static DataTable SelectNominalDiameter()
624
        {
625
            DataTable dt = new DataTable();
626
            ID2Info id2Info = ID2Info.GetInstance();
627
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
628
            {
629
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
630
                {
631
                    try
632
                    {
633
                        connection.Open();
634
                        using (SQLiteCommand cmd = connection.CreateCommand())
635
                        {
636
                            cmd.CommandText = "SELECT * FROM NominalDiameter ORDER BY Metric DESC;";
637
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
638
                                dt.Load(dr);
639
                        }
640
                        connection.Close();
641
                    }
642
                    catch (Exception ex)
643
                    {
644
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
645
                    }
646
                    finally
647
                    {
648
                        connection.Dispose();
649
                    }
650
                }
651
            }
652
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
653
            {
654
                using (SqlConnection connection = GetSqlConnection())
655
                {
656
                    try
657
                    {
658
                        if (connection != null && connection.State == ConnectionState.Open)
659
                        {
660
                            using (SqlCommand cmd = connection.CreateCommand())
661
                            {
662
                                cmd.CommandText = "SELECT * FROM NominalDiameter ORDER BY Metric DESC;";
663
                                using (SqlDataReader dr = cmd.ExecuteReader())
664
                                    dt.Load(dr);
665
                            }
666
                            connection.Close();
667
                        }
668
                    }
669
                    catch (Exception ex)
670
                    {
671
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
672
                    }
673
                    finally
674
                    {
675
                        if (connection != null)
676
                            connection.Dispose();
677
                    }
678
                }
679
            }
680

    
681
            dt.Rows.RemoveAt(0);
682
            dt.Rows.RemoveAt(0);
683
            dt.Rows.RemoveAt(0);
684
            dt.Rows.RemoveAt(0);
685

    
686
            return dt;
687
        }
688

    
689
        public static DataTable SelectSymbolAttribute()
690
        {
691
            DataTable dt = new DataTable();
692
            ID2Info id2Info = ID2Info.GetInstance();
693
            if(id2Info.ID2DBType == ID2DB_Type.SQLite)
694
            {
695
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
696
                {
697
                    try
698
                    {
699
                        connection.Open();
700
                        using (SQLiteCommand cmd = connection.CreateCommand())
701
                        {
702
                            cmd.CommandText = "SELECT DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
703
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
704
                                dt.Load(dr);
705
                        }
706
                        connection.Close();
707
                    }
708
                    catch (Exception ex)
709
                    {
710
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
711
                    }
712
                    finally
713
                    {
714
                        connection.Dispose();
715
                    }
716
                }
717
            }
718
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
719
            {
720
                using (SqlConnection connection = GetSqlConnection())
721
                {
722
                    try
723
                    {
724
                        if (connection != null && connection.State == ConnectionState.Open)
725
                        {
726
                            using (SqlCommand cmd = connection.CreateCommand())
727
                            {
728
                                cmd.CommandText = "SELECT DISTINCT Attribute, DisplayAttribute FROM SymbolAttribute;";
729
                                using (SqlDataReader dr = cmd.ExecuteReader())
730
                                    dt.Load(dr);
731
                            }
732
                            connection.Close();
733
                        }
734
                    }
735
                    catch (Exception ex)
736
                    {
737
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
738
                    }
739
                    finally
740
                    {
741
                        if (connection != null)
742
                            connection.Dispose();
743
                    }
744
                }
745
            }
746
            return dt;
747
        }
748

    
749
        public static DataTable SelectSymbolName()
750
        {
751
            DataTable dt = new DataTable();
752
            ID2Info id2Info = ID2Info.GetInstance();
753
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
754
            {
755
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
756
                {
757
                    try
758
                    {
759
                        connection.Open();
760
                        using (SQLiteCommand cmd = connection.CreateCommand())
761
                        {
762
                            cmd.CommandText = "SELECT * FROM SymbolName;";
763
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
764
                                dt.Load(dr);
765
                        }
766
                        connection.Close();
767
                    }
768
                    catch (Exception ex)
769
                    {
770
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
771
                    }
772
                    finally
773
                    {
774
                        connection.Dispose();
775
                    }
776
                }
777
            }
778
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
779
            {
780
                using (SqlConnection connection = GetSqlConnection())
781
                {
782
                    try
783
                    {
784
                        if (connection != null && connection.State == ConnectionState.Open)
785
                        {
786
                            using (SqlCommand cmd = connection.CreateCommand())
787
                            {
788
                                cmd.CommandText = "SELECT * FROM SymbolName;";
789
                                using (SqlDataReader dr = cmd.ExecuteReader())
790
                                    dt.Load(dr);
791
                            }
792
                            connection.Close();
793
                        }
794
                    }
795
                    catch (Exception ex)
796
                    {
797
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
798
                    }
799
                    finally
800
                    {
801
                        if (connection != null)
802
                            connection.Dispose();
803
                    }
804
                }
805
            }
806
            return dt;
807
        }              
808

    
809
        public static double[] GetDrawingSize()
810
        {
811
            double[] result = null;
812

    
813
            DataTable dt = new DataTable();
814
            ID2Info id2Info = ID2Info.GetInstance();
815
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
816
            {
817
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
818
                {
819
                    try
820
                    {
821
                        connection.Open();
822
                        using (SQLiteCommand cmd = connection.CreateCommand())
823
                        {
824
                            cmd.CommandText = "SELECT value FROM Configuration WHERE Section = 'Area' AND Key = 'Drawing';";
825
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
826
                                dt.Load(dr);
827

    
828
                            if (dt.Rows.Count == 1)
829
                            {
830
                                string value = dt.Rows[0][0].ToString();
831
                                string[] split = value.Split(new char[] { ',' });
832
                                result = new double[] {
833
                                Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", "")),
834
                                Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", "")),
835
                                Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", "")),
836
                                Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", ""))
837
                                };
838
                                result = new double[] {
839
                                Math.Min(result[0], result[2]),
840
                                Math.Min(result[1], result[3]),
841
                                Math.Max(result[0], result[2]),
842
                                Math.Max(result[1], result[3])
843
                                };
844
                            }
845
                        }
846
                        connection.Close();
847
                    }
848
                    catch (Exception ex)
849
                    {
850
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
851
                    }
852
                    finally
853
                    {
854
                        connection.Dispose();
855
                    }
856
                }
857
            }
858
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
859
            {
860
                using (SqlConnection connection = GetSqlConnection())
861
                {
862
                    try
863
                    {
864
                        if (connection != null && connection.State == ConnectionState.Open)
865
                        {
866
                            using (SqlCommand cmd = connection.CreateCommand())
867
                            {
868
                                cmd.CommandText = "SELECT value FROM Configuration WHERE Section = 'Area' AND Key = 'Drawing';";
869
                                using (SqlDataReader dr = cmd.ExecuteReader())
870
                                    dt.Load(dr);
871

    
872
                                if (dt.Rows.Count == 1)
873
                                {
874
                                    string value = dt.Rows[0][0].ToString();
875
                                    string[] split = value.Split(new char[] { ',' });
876
                                    result = new double[] {
877
                                    Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", "")),
878
                                    Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", "")),
879
                                    Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", "")),
880
                                    Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", ""))
881
                                    };
882
                                    result = new double[] {
883
                                    Math.Min(result[0], result[2]),
884
                                    Math.Min(result[1], result[3]),
885
                                    Math.Max(result[0], result[2]),
886
                                    Math.Max(result[1], result[3])
887
                                    };
888
                                }
889
                            }
890
                            connection.Close();
891
                        }
892
                    }
893
                    catch (Exception ex)
894
                    {
895
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
896
                    }
897
                    finally
898
                    {
899
                        if (connection != null)
900
                            connection.Dispose();
901
                    }
902
                }
903
            }
904

    
905
            return result;
906
        }
907

    
908
        public static DataTable GetEquipmentType()
909
        {
910
            DataTable dt = new DataTable();
911
            ID2Info id2Info = ID2Info.GetInstance();
912
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
913
            {
914
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
915
                {
916
                    try
917
                    {
918
                        connection.Open();
919
                        using (SQLiteCommand cmd = connection.CreateCommand())
920
                        {
921
                            cmd.CommandText = "SELECT Type FROM SymbolType WHERE Category = 'Equipment';";
922
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
923
                                dt.Load(dr);
924
                        }
925
                        connection.Close();
926
                    }
927
                    catch (Exception ex)
928
                    {
929
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
930
                    }
931
                    finally
932
                    {
933
                        connection.Dispose();
934
                    }
935
                }
936
            }
937
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
938
            {
939
                using (SqlConnection connection = GetSqlConnection())
940
                {
941
                    try
942
                    {
943
                        if (connection != null && connection.State == ConnectionState.Open)
944
                        {
945
                            using (SqlCommand cmd = connection.CreateCommand())
946
                            {
947
                                cmd.CommandText = "SELECT Type FROM SymbolType WHERE Category = 'Equipment';";
948
                                using (SqlDataReader dr = cmd.ExecuteReader())
949
                                    dt.Load(dr);
950
                            }
951
                            connection.Close();
952
                        }
953
                    }
954
                    catch (Exception ex)
955
                    {
956
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
957
                    }
958
                    finally
959
                    {
960
                        if (connection != null)
961
                            connection.Dispose();
962
                    }
963
                }
964
            }
965
            return dt;
966
        }
967

    
968
        /// <summary>
969
        ///  Zoom 을 위하여 저장 T_PSN_VIEW 데이터로 PSN 데이터지만 Msqql 과 Sqlite 둘다 저장되어 있음
970
        ///  => ID2 가 DB 타입에 따라서 바라보는 DB가 mssql이냐 sqlite냐로 지정되기 때문에 zoom 기능을 그대로 사용하려면 해당 데이터는 psn도 id2 dbtype에 맞춰서 저장 및 로드해야함
971
        /// </summary>
972
        /// <param name="values"></param>
973
        /// <returns></returns>
974
        public static bool SaveView(List<string> values)
975
        {
976
            ID2Info id2Info = ID2Info.GetInstance();
977

    
978
            bool result = true;
979

    
980
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
981
            {
982
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
983
                {
984
                    try
985
                    {
986
                        connection.Open();
987

    
988
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
989
                        {
990
                            try
991
                            {
992
                                using (SQLiteCommand cmd = connection.CreateCommand())
993
                                {
994
                                    cmd.CommandText = string.Format("DELETE FROM {0}", PSN_VIEW);
995
                                    cmd.ExecuteNonQuery();
996

    
997
                                    foreach (string value in values)
998
                                    {
999
                                        cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID)", PSN_VIEW);
1000
                                        cmd.Parameters.Clear();
1001
                                        cmd.Parameters.AddWithValue("@OID", value);
1002
                                        cmd.ExecuteNonQuery();
1003
                                    }
1004
                                }
1005
                                transaction.Commit();
1006
                                connection.Close();
1007
                            }
1008
                            catch (Exception ex)
1009
                            {
1010
                                transaction.Rollback();
1011
                                result = false;
1012
                            }
1013
                            finally
1014
                            {
1015
                                transaction.Dispose();
1016
                            }
1017
                        }
1018
                    }
1019
                    catch (Exception ex)
1020
                    {
1021
                        System.Windows.Forms.MessageBox.Show(ex.Message);
1022
                        result = false;
1023
                    }
1024
                    finally
1025
                    {
1026
                        connection.Dispose();
1027
                    }
1028
                }
1029
            }
1030
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
1031
            {
1032
                using (SqlConnection connection = GetSqlConnection())
1033
                {
1034
                    try
1035
                    {
1036
                        if (connection != null && connection.State == ConnectionState.Open)
1037
                        {
1038
                            using (SqlCommand cmd = connection.CreateCommand())
1039
                            {
1040
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_VIEW);
1041
                                cmd.ExecuteNonQuery();
1042

    
1043
                                foreach (string value in values)
1044
                                {
1045
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID)", PSN_VIEW);
1046
                                    cmd.Parameters.Clear();
1047
                                    cmd.Parameters.AddWithValue("@OID", value);
1048
                                    cmd.ExecuteNonQuery();
1049
                                }
1050
                            }
1051
                            connection.Close();
1052
                        }
1053
                        else
1054
                        {
1055
                            return false;
1056
                        }
1057
                    }
1058
                    catch (Exception ex)
1059
                    {
1060
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1061
                        return false;
1062
                    }
1063
                    finally
1064
                    {
1065
                        if (connection != null)
1066
                            connection.Dispose();
1067
                    }
1068
                }
1069
            }
1070
            return result;
1071
        }
1072

    
1073
        public static bool DeleteView()
1074
        {
1075
            ID2Info id2Info = ID2Info.GetInstance();
1076

    
1077
            bool result = true;
1078
            if (id2Info.ID2DBType == ID2DB_Type.SQLite)
1079
            {
1080
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1081
                {
1082
                    try
1083
                    {
1084
                        connection.Open();
1085

    
1086
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1087
                        {
1088
                            try
1089
                            {
1090
                                using (SQLiteCommand cmd = connection.CreateCommand())
1091
                                {
1092
                                    cmd.CommandText = string.Format("DELETE FROM {0}", PSN_VIEW);
1093
                                    cmd.ExecuteNonQuery();
1094
                                }
1095
                                transaction.Commit();
1096
                                connection.Close();
1097
                            }
1098
                            catch (Exception ex)
1099
                            {
1100
                                transaction.Rollback();
1101
                                result = false;
1102
                            }
1103
                            finally
1104
                            {
1105
                                transaction.Dispose();
1106
                            }
1107
                        }
1108
                    }
1109
                    catch (Exception ex)
1110
                    {
1111
                        System.Windows.Forms.MessageBox.Show(ex.Message);
1112
                        result = false;
1113
                    }
1114
                    finally
1115
                    {
1116
                        connection.Dispose();
1117
                    }
1118
                }
1119
            }
1120
            else if (id2Info.ID2DBType == ID2DB_Type.MSSQL)
1121
            {
1122
                using (SqlConnection connection = GetSqlConnection())
1123
                {
1124
                    try
1125
                    {
1126
                        if (connection != null && connection.State == ConnectionState.Open)
1127
                        {
1128
                            using (SqlCommand cmd = connection.CreateCommand())
1129
                            {
1130
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_VIEW);
1131
                                cmd.ExecuteNonQuery();
1132
                            }
1133
                            connection.Close();
1134
                        }
1135
                        else
1136
                        {
1137
                            return false;
1138
                        }
1139
                    }
1140
                    catch (Exception ex)
1141
                    {
1142
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1143
                        return false;
1144
                    }
1145
                    finally
1146
                    {
1147
                        if (connection != null)
1148
                            connection.Dispose();
1149
                    }
1150
                }
1151
            }
1152

    
1153

    
1154
            return result;
1155
        }
1156

    
1157
        //PSN Sqlite 
1158
        public static DataTable SelectHeaderSetting()
1159
        {
1160
            DataTable dt = new DataTable();
1161
            ID2Info id2Info = ID2Info.GetInstance();
1162

    
1163
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1164
            {
1165
                try
1166
                {
1167
                    connection.Open();
1168
                    using (SQLiteCommand cmd = connection.CreateCommand())
1169
                    {
1170
                        cmd.CommandText = string.Format(@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {0};", PSN_HEADER_SETTING);
1171
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1172
                            dt.Load(dr);
1173
                    }
1174
                    connection.Close();
1175
                }
1176
                catch (Exception ex)
1177
                {
1178
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1179
                }
1180
                finally
1181
                {
1182
                    connection.Dispose();
1183
                }
1184
            }
1185

    
1186
            return dt;
1187
        }
1188

    
1189
        public static DataTable SelectVentDrainSetting()
1190
        {
1191
            DataTable dt = new DataTable();
1192
            ID2Info id2Info = ID2Info.GetInstance();
1193
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1194
            {
1195
                try
1196
                {
1197
                    connection.Open();
1198
                    using (SQLiteCommand cmd = connection.CreateCommand())
1199
                    {
1200
                        cmd.CommandText = string.Format(@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {0};", PSN_VENTDRAIN_SETTING);
1201
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1202
                            dt.Load(dr);
1203
                    }
1204
                    connection.Close();
1205
                }
1206
                catch (Exception ex)
1207
                {
1208
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1209
                }
1210
                finally
1211
                {
1212
                    connection.Dispose();
1213
                }
1214
            }
1215
            return dt;
1216
        }
1217

    
1218
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
1219
        {
1220
            ID2Info id2Info = ID2Info.GetInstance();
1221
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1222
            {
1223
                try
1224
                {
1225
                    connection.Open();
1226
                    using (SQLiteCommand cmd = connection.CreateCommand())
1227
                    {
1228
                        cmd.CommandText = string.Format("DELETE FROM {0}", PSN_HEADER_SETTING);
1229
                        cmd.ExecuteNonQuery();
1230

    
1231
                        foreach (HeaderInfo headerInfo in headerInfos)
1232
                        {
1233
                            foreach (HeaderItem item in headerInfo.HeaderItems)
1234
                            {
1235
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)", PSN_HEADER_SETTING);
1236
                                cmd.Parameters.Clear();
1237
                                cmd.Parameters.AddWithValue("@GROUP_ID", headerInfo.UID);
1238
                                cmd.Parameters.AddWithValue("@DESCRIPTION", headerInfo.Description);
1239
                                cmd.Parameters.AddWithValue("@INDEX", item.Index);
1240
                                cmd.Parameters.AddWithValue("@NAME", item.Name);
1241
                                cmd.ExecuteNonQuery();
1242
                            }
1243
                        }
1244
                    }
1245
                    connection.Close();
1246
                }
1247
                catch (Exception ex)
1248
                {
1249
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1250
                    return false;
1251
                }
1252
                finally
1253
                {
1254
                    connection.Dispose();
1255
                }
1256
            }
1257
            return true;
1258
        }
1259

    
1260
        public static bool SaveVentDrainSetting(List<VentDrainInfo> ventDrainInfos)
1261
        {
1262
            ID2Info id2Info = ID2Info.GetInstance();
1263
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1264
            {
1265
                try
1266
                {
1267
                    connection.Open();
1268
                    using (SQLiteCommand cmd = connection.CreateCommand())
1269
                    {
1270
                        cmd.CommandText = string.Format("DELETE FROM {0}", PSN_VENTDRAIN_SETTING);
1271
                        cmd.ExecuteNonQuery();
1272

    
1273
                        foreach (VentDrainInfo ventDrainInfo in ventDrainInfos)
1274
                        {
1275
                            foreach (VentDrainItem item in ventDrainInfo.VentDrainItems)
1276
                            {
1277
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)", PSN_VENTDRAIN_SETTING);
1278
                                cmd.Parameters.Clear();
1279
                                cmd.Parameters.AddWithValue("@GROUP_ID", ventDrainInfo.UID);
1280
                                cmd.Parameters.AddWithValue("@DESCRIPTION", ventDrainInfo.Description);
1281
                                cmd.Parameters.AddWithValue("@INDEX", item.Index);
1282
                                cmd.Parameters.AddWithValue("@NAME", item.Name);
1283
                                cmd.ExecuteNonQuery();
1284
                            }
1285
                        }
1286
                    }
1287
                    connection.Close();
1288
                }
1289
                catch (Exception ex)
1290
                {
1291
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1292
                    return false;
1293
                }
1294
                finally
1295
                {
1296
                    connection.Dispose();
1297
                }
1298
            }
1299
            return true;
1300
        }
1301

    
1302
        public static bool SaveTopologyRule(DataTable dt)
1303
        {
1304
            ID2Info id2Info = ID2Info.GetInstance();
1305
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1306
            {
1307
                try
1308
                {
1309
                    connection.Open();
1310
                    using (SQLiteCommand cmd = connection.CreateCommand())
1311
                    {
1312
                        cmd.CommandText = string.Format("DELETE FROM {0}", PSN_TOPOLOGY_RULE);
1313
                        cmd.ExecuteNonQuery();
1314

    
1315
                        foreach (DataRow row in dt.Rows)
1316
                        {
1317
                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@UID)", PSN_TOPOLOGY_RULE);
1318
                            cmd.Parameters.Clear();
1319
                            cmd.Parameters.AddWithValue("@UID", row["NAME"].ToString());
1320
                            cmd.ExecuteNonQuery();
1321
                        }
1322
                    }
1323
                    connection.Close();
1324
                }
1325
                catch (Exception ex)
1326
                {
1327
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1328
                    return false;
1329
                }
1330
                finally
1331
                {
1332
                    connection.Dispose();
1333
                }
1334
            }
1335
            return true;
1336
        }
1337

    
1338
        public static DataTable SelectTopologyRule()
1339
        {
1340
            DataTable dt = new DataTable();
1341
            ID2Info id2Info = ID2Info.GetInstance();
1342
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1343
            {
1344
                try
1345
                {
1346
                    connection.Open();
1347
                    using (SQLiteCommand cmd = connection.CreateCommand())
1348
                    {
1349
                        cmd.CommandText = string.Format(@"SELECT * FROM {0};", PSN_TOPOLOGY_RULE);
1350
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1351
                            dt.Load(dr);
1352
                    }
1353
                    connection.Close();
1354
                }
1355
                catch (Exception ex)
1356
                {
1357
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1358
                }
1359
                finally
1360
                {
1361
                    connection.Dispose();
1362
                }
1363
            }
1364
            return dt;
1365
        }
1366
               
1367
        public static bool SavePSNData(PSN item)
1368
        {
1369
            ID2Info id2Info = ID2Info.GetInstance();
1370

    
1371
            bool result = true;
1372
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1373
            {
1374
                try
1375
                {
1376
                    connection.Open();
1377

    
1378
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
1379
                    {
1380
                        try
1381
                        {
1382
                            using (SQLiteCommand cmd = connection.CreateCommand())
1383
                            {
1384
                                // Path Items
1385
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_PATHITEMS);
1386
                                cmd.ExecuteNonQuery();
1387
                                foreach (DataRow row in item.PathItems.Rows)
1388
                                {
1389
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @Class, @SubClass, @TYPE, @PIDNAME, @NPD, @PipeSystemNetwork_OID, @ViewPipeSystemNetwork_OID, @PipeRun_OID)", PSN_PATHITEMS);
1390
                                    cmd.Parameters.Clear();
1391
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
1392
                                    cmd.Parameters.AddWithValue("@SequenceData_OID", row["SequenceData_OID"].ToString());
1393
                                    cmd.Parameters.AddWithValue("@TopologySet_OID", row["TopologySet_OID"].ToString());
1394
                                    cmd.Parameters.AddWithValue("@BranchTopologySet_OID", row["BranchTopologySet_OID"].ToString());
1395
                                    cmd.Parameters.AddWithValue("@PipeLine_OID", row["PipeLine_OID"].ToString());
1396
                                    cmd.Parameters.AddWithValue("@ITEMNAME", row["ITEMNAME"].ToString());
1397
                                    cmd.Parameters.AddWithValue("@ITEMTAG", row["ITEMTAG"].ToString());
1398
                                    cmd.Parameters.AddWithValue("@Class", row["Class"].ToString());
1399
                                    cmd.Parameters.AddWithValue("@SubClass", row["SubClass"].ToString());
1400
                                    cmd.Parameters.AddWithValue("@TYPE", row["TYPE"].ToString());
1401
                                    cmd.Parameters.AddWithValue("@PIDNAME", row["PIDNAME"].ToString());
1402
                                    cmd.Parameters.AddWithValue("@NPD", row["NPD"].ToString());
1403
                                    cmd.Parameters.AddWithValue("@PipeSystemNetwork_OID", row["PipeSystemNetwork_OID"].ToString());
1404
                                    cmd.Parameters.AddWithValue("@ViewPipeSystemNetwork_OID", row["ViewPipeSystemNetwork_OID"].ToString());
1405
                                    cmd.Parameters.AddWithValue("@PipeRun_OID", row["PipeRun_OID"].ToString());
1406
                                    cmd.ExecuteNonQuery();
1407
                                }
1408

    
1409
                                // Sequence
1410
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_SEQUENCEDATA);
1411
                                cmd.ExecuteNonQuery();
1412
                                foreach (DataRow row in item.SequenceData.Rows)
1413
                                {
1414
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)", PSN_SEQUENCEDATA);
1415
                                    cmd.Parameters.Clear();
1416
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
1417
                                    cmd.Parameters.AddWithValue("@SERIALNUMBER", row["SERIALNUMBER"].ToString());
1418
                                    cmd.Parameters.AddWithValue("@PathItem_OID", row["PathItem_OID"].ToString());
1419
                                    cmd.Parameters.AddWithValue("@TopologySet_OID_Key", row["TopologySet_OID_Key"].ToString());
1420
                                    cmd.ExecuteNonQuery();
1421
                                }
1422

    
1423
                                // Nozzle
1424
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_NOZZLE);
1425
                                cmd.ExecuteNonQuery();
1426
                                foreach (DataRow row in item.Nozzle.Rows)
1427
                                {
1428
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS, @Equipment_OID, @FLUID, @NPD, @ROTATION, @FlowDirection)", PSN_NOZZLE);
1429
                                    cmd.Parameters.Clear();
1430
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
1431
                                    cmd.Parameters.AddWithValue("@ITEMTAG", row["ITEMTAG"].ToString());
1432
                                    cmd.Parameters.AddWithValue("@XCOORDS", row["XCOORDS"].ToString());
1433
                                    cmd.Parameters.AddWithValue("@YCOORDS", row["YCOORDS"].ToString());
1434
                                    cmd.Parameters.AddWithValue("@Equipment_OID", row["Equipment_OID"].ToString());
1435
                                    cmd.Parameters.AddWithValue("@FLUID", row["FLUID"].ToString());
1436
                                    cmd.Parameters.AddWithValue("@NPD", row["NPD"].ToString());
1437
                                    cmd.Parameters.AddWithValue("@ROTATION", row["ROTATION"].ToString());
1438
                                    cmd.Parameters.AddWithValue("@FlowDirection", row["FlowDirection"].ToString());
1439
                                    cmd.ExecuteNonQuery();
1440
                                }
1441

    
1442
                                //Equipment
1443
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_EQUIPMENT);
1444
                                cmd.ExecuteNonQuery();
1445
                                foreach (DataRow row in item.Equipment.Rows)
1446
                                {
1447
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)", PSN_EQUIPMENT);
1448
                                    cmd.Parameters.Clear();
1449
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
1450
                                    cmd.Parameters.AddWithValue("@ITEMTAG", row["ITEMTAG"].ToString());
1451
                                    cmd.Parameters.AddWithValue("@XCOORDS", row["XCOORDS"].ToString());
1452
                                    cmd.Parameters.AddWithValue("@YCOORDS", row["YCOORDS"].ToString());
1453
                                    cmd.ExecuteNonQuery();
1454
                                }
1455

    
1456
                                // TopologySet
1457
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_TOPOLOGYSET);
1458
                                cmd.ExecuteNonQuery();
1459
                                foreach (DataRow row in item.TopologySet.Rows)
1460
                                {
1461
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @Type, @SubType, @HeadItemTag, @TailItemTag, @HeadItemID, @TailItemID)", PSN_TOPOLOGYSET);
1462
                                    cmd.Parameters.Clear();
1463
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
1464
                                    cmd.Parameters.AddWithValue("@Type", row["Type"].ToString());
1465
                                    cmd.Parameters.AddWithValue("@SubType", row["SubType"].ToString());
1466
                                    cmd.Parameters.AddWithValue("@HeadItemTag", row["HeadItemTag"].ToString());
1467
                                    cmd.Parameters.AddWithValue("@TailItemTag", row["TailItemTag"].ToString());
1468
                                    cmd.Parameters.AddWithValue("@HeadItemID", row["HeadItemID"].ToString());
1469
                                    cmd.Parameters.AddWithValue("@TailItemID", row["TailItemID"].ToString());
1470
                                    cmd.ExecuteNonQuery();
1471
                                }
1472

    
1473
                                // PSN
1474
                                cmd.CommandText = string.Format("DELETE FROM {0} WHERE PSNRevisionNumber = '{1}'", PSN_PIPESYSTEMNETWORK, string.Format("V{0:D4}", item.Revision));
1475
                                cmd.ExecuteNonQuery();
1476
                                foreach (DataRow row in item.PipeSystemNetwork.Rows)
1477
                                {
1478
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber, @PBS, @PIDDrawings, @Validity, @Status)", PSN_PIPESYSTEMNETWORK);
1479
                                    cmd.Parameters.Clear();
1480
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
1481
                                    cmd.Parameters.AddWithValue("@Type", row["Type"].ToString());
1482
                                    cmd.Parameters.AddWithValue("@OrderNumber", row["OrderNumber"].ToString());
1483
                                    cmd.Parameters.AddWithValue("@Pipeline_OID", row["Pipeline_OID"].ToString());
1484
                                    cmd.Parameters.AddWithValue("@FROM_DATA", row["FROM_DATA"].ToString());
1485
                                    cmd.Parameters.AddWithValue("@TO_DATA", row["TO_DATA"].ToString());
1486
                                    cmd.Parameters.AddWithValue("@TopologySet_OID_Key", row["TopologySet_OID_Key"].ToString());
1487
                                    cmd.Parameters.AddWithValue("@PSNRevisionNumber", row["PSNRevisionNumber"].ToString());
1488
                                    cmd.Parameters.AddWithValue("@PBS", row["PBS"].ToString());
1489
                                    cmd.Parameters.AddWithValue("@PIDDrawings", row["PIDDrawings"].ToString());
1490
                                    cmd.Parameters.AddWithValue("@Validity", row["Validity"].ToString());
1491
                                    cmd.Parameters.AddWithValue("@Status", row["Status"].ToString());
1492
                                    cmd.ExecuteNonQuery();
1493
                                }
1494
                            }
1495
                            transaction.Commit();
1496
                            connection.Close();
1497
                        }
1498
                        catch (Exception ex)
1499
                        {
1500
                            transaction.Rollback();
1501
                            result = false;
1502
                        }
1503
                        finally
1504
                        {
1505
                            transaction.Dispose();
1506
                        }
1507
                    }
1508
                }
1509
                catch (Exception ex)
1510
                {
1511
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1512
                    result = false;
1513
                }
1514
                finally
1515
                {
1516
                    connection.Dispose();
1517
                }
1518
            }
1519

    
1520
            return result;
1521
        }
1522

    
1523
        public static bool SavePSNFluidCode(DataTable dt)
1524
        {
1525
            ID2Info id2Info = ID2Info.GetInstance();
1526

    
1527
            bool result = true;
1528
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1529
            {
1530
                try
1531
                {
1532
                    connection.Open();
1533

    
1534
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
1535
                    {
1536
                        try
1537
                        {
1538
                            using (SQLiteCommand cmd = connection.CreateCommand())
1539
                            {
1540
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_FLUIDCODE);
1541
                                cmd.ExecuteNonQuery();
1542

    
1543
                                foreach (DataRow row in dt.Rows)
1544
                                {
1545
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)", PSN_FLUIDCODE);
1546
                                    cmd.Parameters.Clear();
1547
                                    cmd.Parameters.AddWithValue("@UID", row["UID"].ToString());
1548
                                    cmd.Parameters.AddWithValue("@Code", row["Code"].ToString());
1549
                                    cmd.Parameters.AddWithValue("@Description", row["Description"].ToString());
1550
                                    cmd.Parameters.AddWithValue("@Condition", row["Condition"].ToString());
1551
                                    cmd.Parameters.AddWithValue("@Remarks", row["Remarks"].ToString());
1552
                                    cmd.Parameters.AddWithValue("@GroundLevel", row["GroundLevel"].ToString());
1553
                                    cmd.ExecuteNonQuery();
1554
                                }
1555
                            }
1556
                            transaction.Commit();
1557
                            connection.Close();
1558
                        }
1559
                        catch (Exception ex)
1560
                        {
1561
                            transaction.Rollback();
1562
                            result = false;
1563
                        }
1564
                        finally
1565
                        {
1566
                            transaction.Dispose();
1567
                        }
1568
                    }
1569
                }
1570
                catch (Exception ex)
1571
                {
1572
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1573
                    result = false;
1574
                }
1575
                finally
1576
                {
1577
                    connection.Dispose();
1578
                }
1579
            }
1580

    
1581
            return result;
1582
        }
1583

    
1584
        public static DataTable SelectPSNFluidCode()
1585
        {
1586
            DataTable dt = new DataTable();
1587
            ID2Info id2Info = ID2Info.GetInstance();
1588

    
1589
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1590
            {
1591
                try
1592
                {
1593
                    connection.Open();
1594
                    using (SQLiteCommand cmd = connection.CreateCommand())
1595
                    {
1596
                        cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_FLUIDCODE);
1597
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1598
                            dt.Load(dr);
1599
                    }
1600
                    connection.Close();
1601
                }
1602
                catch (Exception ex)
1603
                {
1604
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1605
                }
1606
                finally
1607
                {
1608
                    connection.Dispose();
1609
                }
1610
            }
1611
            return dt;
1612
        }
1613

    
1614
        public static bool SavePSNPMC(DataTable dt)
1615
        {
1616
            ID2Info id2Info = ID2Info.GetInstance();
1617

    
1618
            bool result = true;
1619
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1620
            {
1621
                try
1622
                {
1623
                    connection.Open();
1624

    
1625
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
1626
                    {
1627
                        try
1628
                        {
1629
                            using (SQLiteCommand cmd = connection.CreateCommand())
1630
                            {
1631
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_PIPINGMATLCLASS);
1632
                                cmd.ExecuteNonQuery();
1633

    
1634
                                foreach (DataRow row in dt.Rows)
1635
                                {
1636
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)", PSN_PIPINGMATLCLASS);
1637
                                    cmd.Parameters.Clear();
1638
                                    cmd.Parameters.AddWithValue("@UID", row["UID"].ToString());
1639
                                    cmd.Parameters.AddWithValue("@Priority", row["Priority"].ToString());
1640
                                    cmd.Parameters.AddWithValue("@Code", row["Code"].ToString());
1641
                                    cmd.Parameters.AddWithValue("@Description", row["Description"].ToString());
1642
                                    cmd.Parameters.AddWithValue("@Condition", row["Condition"].ToString());
1643
                                    cmd.Parameters.AddWithValue("@Remarks", row["Remarks"].ToString());
1644
                                    cmd.Parameters.AddWithValue("@GroundLevel", row["GroundLevel"].ToString());
1645
                                    cmd.ExecuteNonQuery();
1646
                                }
1647
                            }
1648
                            transaction.Commit();
1649
                            connection.Close();
1650
                        }
1651
                        catch (Exception ex)
1652
                        {
1653
                            transaction.Rollback();
1654
                            result = false;
1655
                        }
1656
                        finally
1657
                        {
1658
                            transaction.Dispose();
1659
                        }
1660
                    }
1661
                }
1662
                catch (Exception ex)
1663
                {
1664
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1665
                    result = false;
1666
                }
1667
                finally
1668
                {
1669
                    connection.Dispose();
1670
                }
1671
            }
1672

    
1673
            return result;
1674
        }
1675

    
1676
        public static PSN GetDBPSN()
1677
        {
1678
            PSN result = new PSN();
1679
            ID2Info id2Info = ID2Info.GetInstance();
1680

    
1681
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1682
            {
1683
                try
1684
                {
1685
                    connection.Open();
1686

    
1687
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
1688
                    {
1689
                        try
1690
                        {
1691
                            using (SQLiteCommand cmd = connection.CreateCommand())
1692
                            {
1693
                                DataTable psnDT = new DataTable();
1694
                                cmd.CommandText = string.Format("SELECT * FROM {0} WHERE PSNRevisionNumber = '{1}'", PSN_PIPESYSTEMNETWORK, string.Format("V{0:D4}", GetRevision()));
1695
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
1696
                                    psnDT.Load(dr);
1697
                                result.PipeSystemNetwork = psnDT;
1698

    
1699
                                DataTable equipDT = new DataTable();
1700
                                cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_EQUIPMENT);
1701
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
1702
                                    equipDT.Load(dr);
1703
                                result.Equipment = equipDT;
1704

    
1705
                                DataTable nozzleDT = new DataTable();
1706
                                cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_NOZZLE);
1707
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
1708
                                    nozzleDT.Load(dr);
1709
                                result.Nozzle = nozzleDT;
1710

    
1711
                                DataTable pathItemDT = new DataTable();
1712
                                cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_PATHITEMS);
1713
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
1714
                                    pathItemDT.Load(dr);
1715
                                result.PathItems = pathItemDT;
1716

    
1717
                                DataTable seqDT = new DataTable();
1718
                                cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_SEQUENCEDATA);
1719
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
1720
                                    seqDT.Load(dr);
1721
                                result.SequenceData = seqDT;
1722

    
1723
                                result.Revision = GetRevision();
1724
                            }
1725
                            transaction.Commit();
1726
                            connection.Close();
1727
                        }
1728
                        catch (Exception ex)
1729
                        {
1730
                            transaction.Rollback();
1731
                            result = null;
1732
                        }
1733
                        finally
1734
                        {
1735
                            transaction.Dispose();
1736
                        }
1737
                    }
1738
                }
1739
                catch (Exception ex)
1740
                {
1741
                    System.Windows.Forms.MessageBox.Show(ex.Message);
1742
                    result = null;
1743
                }
1744
                finally
1745
                {
1746
                    connection.Dispose();
1747
                }
1748
            }
1749
            return result;
1750
        }
1751

    
1752
        public static int GetRevision()
1753
        {
1754
            int result = 0;
1755
            DataTable dt = new DataTable();
1756
            ID2Info id2Info = ID2Info.GetInstance();
1757
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1758
            {
1759
                try
1760
                {
1761
                    connection.Open();
1762
                    using (SQLiteCommand cmd = connection.CreateCommand())
1763
                    {
1764
                        cmd.CommandText = string.Format("SELECT DISTINCT PSNRevisionNumber FROM {0};", PSN_PIPESYSTEMNETWORK);
1765
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1766
                            dt.Load(dr);
1767

    
1768
                        foreach (DataRow row in dt.Rows)
1769
                        {
1770
                            string value = row["PSNRevisionNumber"].ToString();
1771
                            if (value.StartsWith("V"))
1772
                                value = value.Remove(0, 1);
1773
                            int revisionNumber = Convert.ToInt32(value);
1774
                            if (result < revisionNumber)
1775
                                result = revisionNumber;
1776
                        }
1777
                    }
1778
                    connection.Close();
1779
                }
1780
                catch (Exception ex)
1781
                {
1782
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1783
                    result = -1;
1784
                }
1785
                finally
1786
                {
1787
                    connection.Dispose();
1788
                }
1789
            }
1790

    
1791
            return result;
1792
        }
1793

    
1794
        public static DataTable GetPathItem()
1795
        {
1796
            DataTable dt = new DataTable();
1797
            ID2Info id2Info = ID2Info.GetInstance();
1798
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1799
            {
1800
                try
1801
                {
1802
                    connection.Open();
1803
                    using (SQLiteCommand cmd = connection.CreateCommand())
1804
                    {
1805
                        cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_PATHITEMS);
1806
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1807
                            dt.Load(dr);
1808
                    }
1809
                    connection.Close();
1810
                }
1811
                catch (Exception ex)
1812
                {
1813
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1814
                }
1815
                finally
1816
                {
1817
                    connection.Dispose();
1818
                }
1819
            }
1820
            return dt;
1821
        }
1822

    
1823
        public static DataTable GetTopologySet()
1824
        {
1825
            DataTable dt = new DataTable();
1826
            ID2Info id2Info = ID2Info.GetInstance();
1827
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1828
            {
1829
                try
1830
                {
1831
                    connection.Open();
1832
                    using (SQLiteCommand cmd = connection.CreateCommand())
1833
                    {
1834
                        cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_TOPOLOGYSET);
1835
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1836
                            dt.Load(dr);
1837
                    }
1838
                    connection.Close();
1839
                }
1840
                catch (Exception ex)
1841
                {
1842
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1843
                }
1844
                finally
1845
                {
1846
                    connection.Dispose();
1847
                }
1848
            }
1849
            return dt;
1850
        }
1851

    
1852
        public static DataTable GetPipeSystemNetwork()
1853
        {
1854
            DataTable dt = new DataTable();
1855
            ID2Info id2Info = ID2Info.GetInstance();
1856
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1857
            {
1858
                try
1859
                {
1860
                    connection.Open();
1861
                    using (SQLiteCommand cmd = connection.CreateCommand())
1862
                    {
1863
                        cmd.CommandText = string.Format("SELECT * FROM {0} WHERE PSNRevisionNumber = '{1}'", PSN_PIPESYSTEMNETWORK, string.Format("V{0:D4}", GetRevision()));
1864
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1865
                            dt.Load(dr);
1866
                    }
1867
                    connection.Close();
1868
                }
1869
                catch (Exception ex)
1870
                {
1871
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1872
                }
1873
                finally
1874
                {
1875
                    connection.Dispose();
1876
                }
1877
            }
1878
            return dt;
1879
        }
1880

    
1881
        public static DataTable GetSequenceData()
1882
        {
1883
            DataTable dt = new DataTable();
1884
            ID2Info id2Info = ID2Info.GetInstance();
1885
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1886
            {
1887
                try
1888
                {
1889
                    connection.Open();
1890
                    using (SQLiteCommand cmd = connection.CreateCommand())
1891
                    {
1892
                        cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_SEQUENCEDATA);
1893
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1894
                            dt.Load(dr);
1895
                    }
1896
                    connection.Close();
1897
                }
1898
                catch (Exception ex)
1899
                {
1900
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1901
                }
1902
                finally
1903
                {
1904
                    connection.Dispose();
1905
                }
1906
            }
1907
            return dt;
1908
        }
1909

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