프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / ID2PSN / DB.cs @ 5c248ee3

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

    
13
namespace ID2PSN
14
{
15
    public static 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_PATHITEMS = "SPPIDPathItem";
20
        const string PSN_SEQUENCEDATA = "SPPIDSequenceData";
21
        const string PSN_PIPESYSTEMNETWORK = "SPPIDPipeSystemNetwork";
22
        const string PSN_EQUIPMENT = "SPPIDEquipment";
23
        const string PSN_NOZZLE = "SPPIDNozzle";
24
        const string PSN_FLUIDCODE = "SPPIDFluidCode";
25
        const string PSN_PIPINGMATLCLASS = "SPPIDPipingMatClass";
26
        const string PSN_VIEW = "T_PSN_VIEW";
27
        public static bool ConnTestAndCreateTable()
28
        {
29
            bool result = false;
30
            ID2Info id2Info = ID2Info.GetInstance();
31
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", id2Info.DBFilePath), true))
32
            {
33
                try
34
                {
35
                    connection.Open();
36
                    if (connection.State == ConnectionState.Open)
37
                    {
38
                        using (SQLiteCommand cmd = connection.CreateCommand())
39
                        {
40
                            cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
41
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
42
                            using (DataTable dt = new DataTable())
43
                            {
44
                                dt.Load(dr);
45

    
46
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_HEADER_SETTING)).Length == 0)
47
                                {
48
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (GROUP_ID TEXT, DESCRIPTION TEXT, [INDEX] INTEGER, NAME TEXT)", PSN_HEADER_SETTING);
49
                                    cmd.ExecuteNonQuery();
50
                                }
51
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_TOPOLOGY_RULE)).Length == 0)
52
                                {
53
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT)", PSN_TOPOLOGY_RULE);
54
                                    cmd.ExecuteNonQuery();
55
                                }
56
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_PATHITEMS)).Length == 0)
57
                                {
58
                                    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, TYPE TEXT, PIDNAME TEXT, NPD TEXT, PipeSystemNetwork_OID TEXT, PipeRun_OID TEXT)", PSN_PATHITEMS);
59
                                    cmd.ExecuteNonQuery();
60
                                }
61
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_SEQUENCEDATA)).Length == 0)
62
                                {
63
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, SERIALNUMBER TEXT, PathItem_OID TEXT, TopologySet_OID_Key TEXT)", PSN_SEQUENCEDATA);
64
                                    cmd.ExecuteNonQuery();
65
                                }
66
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_PIPESYSTEMNETWORK)).Length == 0)
67
                                {
68
                                    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, PathOID TEXT, PBS TEXT, PIDDrawings TEXT)", PSN_PIPESYSTEMNETWORK);
69
                                    cmd.ExecuteNonQuery();
70
                                }
71

    
72
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_EQUIPMENT)).Length == 0)
73
                                {
74
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT, ITEMTAG TEXT, XCOORDS TEXT, YCOORDS TEXT)", PSN_EQUIPMENT);
75
                                    cmd.ExecuteNonQuery();
76
                                }
77
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_NOZZLE)).Length == 0)
78
                                {
79
                                    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);
80
                                    cmd.ExecuteNonQuery();
81
                                }
82
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_FLUIDCODE)).Length == 0)
83
                                {
84
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT, Code TEXT, Description TEXT, Condition TEXT, Remarks TEXT, GroundLevel TEXT)", PSN_FLUIDCODE);
85
                                    cmd.ExecuteNonQuery();
86
                                }
87
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_PIPINGMATLCLASS)).Length == 0)
88
                                {
89
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT, Priority TEXT, Code TEXT, Description TEXT, Condition TEXT, Remarks TEXT, GroundLevel TEXT)", PSN_PIPINGMATLCLASS);
90
                                    cmd.ExecuteNonQuery();
91
                                }
92
                                if (dt.Select(string.Format("NAME = '{0}'", PSN_VIEW)).Length == 0)
93
                                {
94
                                    cmd.CommandText = string.Format("CREATE TABLE {0} (OID TEXT)", PSN_VIEW);
95
                                    cmd.ExecuteNonQuery();
96
                                }
97
                            }
98
                        }
99
                        result = true;
100
                    }
101
                    connection.Close();
102
                }
103
                catch (Exception ex)
104
                {
105
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
106
                }
107
                finally
108
                {
109
                    connection.Dispose();
110
                }
111
            }
112
            return result;
113
        }
114

    
115
        public static DataTable SelectHeaderSetting()
116
        {
117
            DataTable dt = new DataTable();
118
            ID2Info id2Info = ID2Info.GetInstance();
119
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
120
            {
121
                try
122
                {
123
                    connection.Open();
124
                    using (SQLiteCommand cmd = connection.CreateCommand())
125
                    {
126
                        cmd.CommandText = string.Format(@"SELECT GROUP_ID, DESCRIPTION, [INDEX], NAME FROM {0};", PSN_HEADER_SETTING);
127
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
128
                            dt.Load(dr);
129
                    }
130
                    connection.Close();
131
                }
132
                catch (Exception ex)
133
                {
134
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
135
                }
136
                finally
137
                {
138
                    connection.Dispose();
139
                }
140
            }
141
            return dt;
142
        }
143

    
144
        public static bool SaveHeaderSetting(List<HeaderInfo> headerInfos)
145
        {
146
            ID2Info id2Info = ID2Info.GetInstance();
147
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
148
            {
149
                try
150
                {
151
                    connection.Open();
152
                    using (SQLiteCommand cmd = connection.CreateCommand())
153
                    {
154
                        cmd.CommandText = string.Format("DELETE FROM {0}", PSN_HEADER_SETTING);
155
                        cmd.ExecuteNonQuery();
156

    
157
                        foreach (HeaderInfo headerInfo in headerInfos)
158
                        {
159
                            foreach (HeaderItem item in headerInfo.HeaderItems)
160
                            {
161
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@GROUP_ID, @DESCRIPTION, @INDEX, @NAME)", PSN_HEADER_SETTING);
162
                                cmd.Parameters.Clear();
163
                                cmd.Parameters.AddWithValue("@GROUP_ID", headerInfo.UID);
164
                                cmd.Parameters.AddWithValue("@DESCRIPTION", headerInfo.Description);
165
                                cmd.Parameters.AddWithValue("@INDEX", item.Index);
166
                                cmd.Parameters.AddWithValue("@NAME", item.Name);
167
                                cmd.ExecuteNonQuery();
168
                            }
169
                        }
170
                    }
171
                    connection.Close();
172
                }
173
                catch (Exception ex)
174
                {
175
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
176
                    return false;
177
                }
178
                finally
179
                {
180
                    connection.Dispose();
181
                }
182
            }
183
            return true;
184
        }
185

    
186
        public static DataTable SelectOPCRelations()
187
        {
188
            DataTable dt = new DataTable();
189
            ID2Info id2Info = ID2Info.GetInstance();
190
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
191
            {
192
                try
193
                {
194
                    connection.Open();
195
                    using (SQLiteCommand cmd = connection.CreateCommand())
196
                    {
197
                        cmd.CommandText = "SELECT * FROM OPCRelations;";
198
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
199
                            dt.Load(dr);
200
                    }
201
                    connection.Close();
202
                }
203
                catch (Exception ex)
204
                {
205
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
206
                }
207
                finally
208
                {
209
                    connection.Dispose();
210
                }
211
            }
212
            return dt;
213
        }
214

    
215
        public static DataTable SelectDrawings()
216
        {
217
            DataTable dt = new DataTable();
218
            ID2Info id2Info = ID2Info.GetInstance();
219
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
220
            {
221
                try
222
                {
223
                    connection.Open();
224
                    using (SQLiteCommand cmd = connection.CreateCommand())
225
                    {
226
                        cmd.CommandText = "SELECT * FROM Drawings;";
227
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
228
                            dt.Load(dr);
229
                    }
230
                    connection.Close();
231
                }
232
                catch (Exception ex)
233
                {
234
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
235
                }
236
                finally
237
                {
238
                    connection.Dispose();
239
                }
240
            }
241
            return dt;
242
        }
243

    
244
        public static DataTable SelectLineProperties()
245
        {
246
            DataTable dt = new DataTable();
247
            ID2Info id2Info = ID2Info.GetInstance();
248
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
249
            {
250
                try
251
                {
252
                    connection.Open();
253
                    using (SQLiteCommand cmd = connection.CreateCommand())
254
                    {
255
                        cmd.CommandText = "SELECT * FROM LineProperties;";
256
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
257
                            dt.Load(dr);
258
                    }
259
                    connection.Close();
260
                }
261
                catch (Exception ex)
262
                {
263
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
264
                }
265
                finally
266
                {
267
                    connection.Dispose();
268
                }
269
            }
270
            return dt;
271
        }
272
        public static DataTable SelectTopologyRule()
273
        {
274
            DataTable dt = new DataTable();
275
            ID2Info id2Info = ID2Info.GetInstance();
276
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
277
            {
278
                try
279
                {
280
                    connection.Open();
281
                    using (SQLiteCommand cmd = connection.CreateCommand())
282
                    {
283
                        cmd.CommandText = string.Format(@"SELECT * FROM {0};", PSN_TOPOLOGY_RULE);
284
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
285
                            dt.Load(dr);
286
                    }
287
                    connection.Close();
288
                }
289
                catch (Exception ex)
290
                {
291
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
292
                }
293
                finally
294
                {
295
                    connection.Dispose();
296
                }
297
            }
298
            return dt;
299
        }
300
        public static bool SaveTopologyRule(DataTable dt)
301
        {
302
            ID2Info id2Info = ID2Info.GetInstance();
303
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
304
            {
305
                try
306
                {
307
                    connection.Open();
308
                    using (SQLiteCommand cmd = connection.CreateCommand())
309
                    {
310
                        cmd.CommandText = string.Format("DELETE FROM {0}", PSN_TOPOLOGY_RULE);
311
                        cmd.ExecuteNonQuery();
312

    
313
                        foreach (DataRow row in dt.Rows)
314
                        {
315
                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@UID)", PSN_TOPOLOGY_RULE);
316
                            cmd.Parameters.Clear();
317
                            cmd.Parameters.AddWithValue("@UID", row["NAME"].ToString());
318
                            cmd.ExecuteNonQuery();
319
                        }
320
                    }
321
                    connection.Close();
322
                }
323
                catch (Exception ex)
324
                {
325
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
326
                    return false;
327
                }
328
                finally
329
                {
330
                    connection.Dispose();
331
                }
332
            }
333
            return true;
334
        }
335

    
336
        public static DataTable SelectFluidCode()
337
        {
338
            DataTable dt = new DataTable();
339
            ID2Info id2Info = ID2Info.GetInstance();
340
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
341
            {
342
                try
343
                {
344
                    connection.Open();
345
                    using (SQLiteCommand cmd = connection.CreateCommand())
346
                    {
347
                        cmd.CommandText = "SELECT * FROM FluidCode;";
348
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
349
                            dt.Load(dr);
350
                    }
351
                    connection.Close();
352
                }
353
                catch (Exception ex)
354
                {
355
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
356
                }
357
                finally
358
                {
359
                    connection.Dispose();
360
                }
361
            }
362
            return dt;
363
        }
364
        public static DataTable SelectPipingMaterialsClass()
365
        {
366
            DataTable dt = new DataTable();
367
            ID2Info id2Info = ID2Info.GetInstance();
368
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
369
            {
370
                try
371
                {
372
                    connection.Open();
373
                    using (SQLiteCommand cmd = connection.CreateCommand())
374
                    {
375
                        cmd.CommandText = "SELECT * FROM PipingMaterialsClass;";
376
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
377
                            dt.Load(dr);
378
                    }
379
                    connection.Close();
380
                }
381
                catch (Exception ex)
382
                {
383
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
384
                }
385
                finally
386
                {
387
                    connection.Dispose();
388
                }
389
            }
390
            return dt;
391
        }
392

    
393
        public static DataTable SelectPSNFluidCode()
394
        {
395
            DataTable dt = new DataTable();
396
            ID2Info id2Info = ID2Info.GetInstance();
397
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
398
            {
399
                try
400
                {
401
                    connection.Open();
402
                    using (SQLiteCommand cmd = connection.CreateCommand())
403
                    {
404
                        cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_FLUIDCODE);
405
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
406
                            dt.Load(dr);
407
                    }
408
                    connection.Close();
409
                }
410
                catch (Exception ex)
411
                {
412
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
413
                }
414
                finally
415
                {
416
                    connection.Dispose();
417
                }
418
            }
419
            return dt;
420
        }
421
        public static DataTable SelectPSNPIPINGMATLCLASS()
422
        {
423
            DataTable dt = new DataTable();
424
            ID2Info id2Info = ID2Info.GetInstance();
425
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
426
            {
427
                try
428
                {
429
                    connection.Open();
430
                    using (SQLiteCommand cmd = connection.CreateCommand())
431
                    {
432
                        cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_PIPINGMATLCLASS);
433
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
434
                            dt.Load(dr);
435
                    }
436
                    connection.Close();
437
                }
438
                catch (Exception ex)
439
                {
440
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
441
                }
442
                finally
443
                {
444
                    connection.Dispose();
445
                }
446
            }
447
            return dt;
448
        }
449
        public static DataTable SelectNominalDiameter()
450
        {
451
            DataTable dt = new DataTable();
452
            ID2Info id2Info = ID2Info.GetInstance();
453
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
454
            {
455
                try
456
                {
457
                    connection.Open();
458
                    using (SQLiteCommand cmd = connection.CreateCommand())
459
                    {
460
                        cmd.CommandText = "SELECT * FROM NominalDiameter;";
461
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
462
                            dt.Load(dr);
463
                    }
464
                    connection.Close();
465
                }
466
                catch (Exception ex)
467
                {
468
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
469
                }
470
                finally
471
                {
472
                    connection.Dispose();
473
                }
474
            }
475
            return dt;
476
        }
477

    
478
        public static bool SavePSNData(PSN item)
479
        {
480
            ID2Info id2Info = ID2Info.GetInstance();
481

    
482
            bool result = true;
483
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
484
            {
485
                try
486
                {
487
                    connection.Open();
488

    
489
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
490
                    {
491
                        try
492
                        {
493
                            using (SQLiteCommand cmd = connection.CreateCommand())
494
                            {
495
                                // Path Items
496
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_PATHITEMS);
497
                                cmd.ExecuteNonQuery();
498
                                foreach (DataRow row in item.PathItems.Rows)
499
                                {
500
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @SequenceData_OID, @TopologySet_OID, @BranchTopologySet_OID, @PipeLine_OID, @ITEMNAME, @ITEMTAG, @TYPE, @PIDNAME, @NPD, @PipeSystemNetwork_OID, @PipeRun_OID)", PSN_PATHITEMS);
501
                                    cmd.Parameters.Clear();
502
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
503
                                    cmd.Parameters.AddWithValue("@SequenceData_OID", row["SequenceData_OID"].ToString());
504
                                    cmd.Parameters.AddWithValue("@TopologySet_OID", row["TopologySet_OID"].ToString());
505
                                    cmd.Parameters.AddWithValue("@BranchTopologySet_OID", row["BranchTopologySet_OID"].ToString());
506
                                    cmd.Parameters.AddWithValue("@PipeLine_OID", row["PipeLine_OID"].ToString());
507
                                    cmd.Parameters.AddWithValue("@ITEMNAME", row["ITEMNAME"].ToString());
508
                                    cmd.Parameters.AddWithValue("@ITEMTAG", row["ITEMTAG"].ToString());
509
                                    cmd.Parameters.AddWithValue("@TYPE", row["TYPE"].ToString());
510
                                    cmd.Parameters.AddWithValue("@PIDNAME", row["PIDNAME"].ToString());
511
                                    cmd.Parameters.AddWithValue("@NPD", row["NPD"].ToString());
512
                                    cmd.Parameters.AddWithValue("@PipeSystemNetwork_OID", row["PipeSystemNetwork_OID"].ToString());
513
                                    cmd.Parameters.AddWithValue("@PipeRun_OID", row["PipeRun_OID"].ToString());
514
                                    cmd.ExecuteNonQuery();
515
                                }
516

    
517
                                // Sequence
518
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_SEQUENCEDATA);
519
                                cmd.ExecuteNonQuery();
520
                                foreach (DataRow row in item.SequenceData.Rows)
521
                                {
522
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @SERIALNUMBER, @PathItem_OID, @TopologySet_OID_Key)", PSN_SEQUENCEDATA);
523
                                    cmd.Parameters.Clear();
524
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
525
                                    cmd.Parameters.AddWithValue("@SERIALNUMBER", row["SERIALNUMBER"].ToString());
526
                                    cmd.Parameters.AddWithValue("@PathItem_OID", row["PathItem_OID"].ToString());
527
                                    cmd.Parameters.AddWithValue("@TopologySet_OID_Key", row["TopologySet_OID_Key"].ToString());
528
                                    cmd.ExecuteNonQuery();
529
                                }
530

    
531
                                // Nozzle
532
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_NOZZLE);
533
                                cmd.ExecuteNonQuery();
534
                                foreach (DataRow row in item.Nozzle.Rows)
535
                                {
536
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS, @Equipment_OID, @FLUID, @NPD, @ROTATION, @FlowDirection)", PSN_NOZZLE);
537
                                    cmd.Parameters.Clear();
538
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
539
                                    cmd.Parameters.AddWithValue("@ITEMTAG", row["ITEMTAG"].ToString());
540
                                    cmd.Parameters.AddWithValue("@XCOORDS", row["XCOORDS"].ToString());
541
                                    cmd.Parameters.AddWithValue("@YCOORDS", row["YCOORDS"].ToString());
542
                                    cmd.Parameters.AddWithValue("@Equipment_OID", row["Equipment_OID"].ToString());
543
                                    cmd.Parameters.AddWithValue("@FLUID", row["FLUID"].ToString());
544
                                    cmd.Parameters.AddWithValue("@NPD", row["NPD"].ToString());
545
                                    cmd.Parameters.AddWithValue("@ROTATION", row["ROTATION"].ToString());
546
                                    cmd.Parameters.AddWithValue("@FlowDirection", row["FlowDirection"].ToString());
547
                                    cmd.ExecuteNonQuery();
548
                                }
549

    
550
                                //Equipment
551
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_EQUIPMENT);
552
                                cmd.ExecuteNonQuery();
553
                                foreach (DataRow row in item.Equipment.Rows)
554
                                {
555
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @ITEMTAG, @XCOORDS, @YCOORDS)", PSN_EQUIPMENT);
556
                                    cmd.Parameters.Clear();
557
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
558
                                    cmd.Parameters.AddWithValue("@ITEMTAG", row["ITEMTAG"].ToString());
559
                                    cmd.Parameters.AddWithValue("@XCOORDS", row["XCOORDS"].ToString());
560
                                    cmd.Parameters.AddWithValue("@YCOORDS", row["YCOORDS"].ToString());
561
                                    cmd.ExecuteNonQuery();
562
                                }
563

    
564
                                // PSN
565
                                cmd.CommandText = string.Format("DELETE FROM {0} WHERE PSNRevisionNumber = '{1}'", PSN_PIPESYSTEMNETWORK, string.Format("{0:D3}", item.Revision));
566
                                cmd.ExecuteNonQuery();
567
                                foreach (DataRow row in item.PipeSystemNetwork.Rows)
568
                                {
569
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID, @Type, @OrderNumber, @Pipeline_OID, @FROM_DATA, @TO_DATA, @TopologySet_OID_Key, @PSNRevisionNumber, @PathOID, @PBS, @PIDDrawings)", PSN_PIPESYSTEMNETWORK);
570
                                    cmd.Parameters.Clear();
571
                                    cmd.Parameters.AddWithValue("@OID", row["OID"].ToString());
572
                                    cmd.Parameters.AddWithValue("@Type", row["Type"].ToString());
573
                                    cmd.Parameters.AddWithValue("@OrderNumber", row["OrderNumber"].ToString());
574
                                    cmd.Parameters.AddWithValue("@Pipeline_OID", row["Pipeline_OID"].ToString());
575
                                    cmd.Parameters.AddWithValue("@FROM_DATA", row["FROM_DATA"].ToString());
576
                                    cmd.Parameters.AddWithValue("@TO_DATA", row["TO_DATA"].ToString());
577
                                    cmd.Parameters.AddWithValue("@TopologySet_OID_Key", row["TopologySet_OID_Key"].ToString());
578
                                    cmd.Parameters.AddWithValue("@PSNRevisionNumber", row["PSNRevisionNumber"].ToString());
579
                                    cmd.Parameters.AddWithValue("@PathOID", row["PathOID"].ToString());
580
                                    cmd.Parameters.AddWithValue("@PBS", row["PBS"].ToString());
581
                                    cmd.Parameters.AddWithValue("@PIDDrawings", row["PIDDrawings"].ToString());
582
                                    cmd.ExecuteNonQuery();
583
                                }
584

    
585
                            }
586
                            transaction.Commit();
587
                            connection.Close();
588
                        }
589
                        catch (Exception ex)
590
                        {
591
                            transaction.Rollback();
592
                            result = false;
593
                        }
594
                        finally
595
                        {
596
                            transaction.Dispose();
597
                        }
598
                    }
599
                }
600
                catch (Exception ex)
601
                {
602
                    System.Windows.Forms.MessageBox.Show(ex.Message);
603
                    result = false;
604
                }
605
                finally
606
                {
607
                    connection.Dispose();
608
                }
609
            }
610

    
611
            return result;
612
        }
613

    
614
        public static bool SavePSNFluidCode(DataTable dt)
615
        {
616
            ID2Info id2Info = ID2Info.GetInstance();
617

    
618
            bool result = true;
619
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
620
            {
621
                try
622
                {
623
                    connection.Open();
624

    
625
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
626
                    {
627
                        try
628
                        {
629
                            using (SQLiteCommand cmd = connection.CreateCommand())
630
                            {
631
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_FLUIDCODE);
632
                                cmd.ExecuteNonQuery();
633

    
634
                                foreach (DataRow row in dt.Rows)
635
                                {
636
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@UID, @Code, @Description, @Condition, @Remarks, @GroundLevel)", PSN_FLUIDCODE);
637
                                    cmd.Parameters.Clear();
638
                                    cmd.Parameters.AddWithValue("@UID", row["UID"].ToString());
639
                                    cmd.Parameters.AddWithValue("@Code", row["Code"].ToString());
640
                                    cmd.Parameters.AddWithValue("@Description", row["Description"].ToString());
641
                                    cmd.Parameters.AddWithValue("@Condition", row["Condition"].ToString());
642
                                    cmd.Parameters.AddWithValue("@Remarks", row["Remarks"].ToString());
643
                                    cmd.Parameters.AddWithValue("@GroundLevel", row["GroundLevel"].ToString());
644
                                    cmd.ExecuteNonQuery();
645
                                }
646
                            }
647
                            transaction.Commit();
648
                            connection.Close();
649
                        }
650
                        catch (Exception ex)
651
                        {
652
                            transaction.Rollback();
653
                            result = false;
654
                        }
655
                        finally
656
                        {
657
                            transaction.Dispose();
658
                        }
659
                    }
660
                }
661
                catch (Exception ex)
662
                {
663
                    System.Windows.Forms.MessageBox.Show(ex.Message);
664
                    result = false;
665
                }
666
                finally
667
                {
668
                    connection.Dispose();
669
                }
670
            }
671

    
672
            return result;
673
        }
674

    
675
        public static bool SavePSNPMC(DataTable dt)
676
        {
677
            ID2Info id2Info = ID2Info.GetInstance();
678

    
679
            bool result = true;
680
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
681
            {
682
                try
683
                {
684
                    connection.Open();
685

    
686
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
687
                    {
688
                        try
689
                        {
690
                            using (SQLiteCommand cmd = connection.CreateCommand())
691
                            {
692
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_PIPINGMATLCLASS);
693
                                cmd.ExecuteNonQuery();
694

    
695
                                foreach (DataRow row in dt.Rows)
696
                                {
697
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@UID, @Priority, @Code, @Description, @Condition, @Remarks, @GroundLevel)", PSN_PIPINGMATLCLASS);
698
                                    cmd.Parameters.Clear();
699
                                    cmd.Parameters.AddWithValue("@UID", row["UID"].ToString());
700
                                    cmd.Parameters.AddWithValue("@Priority", row["Priority"].ToString());
701
                                    cmd.Parameters.AddWithValue("@Code", row["Code"].ToString());
702
                                    cmd.Parameters.AddWithValue("@Description", row["Description"].ToString());
703
                                    cmd.Parameters.AddWithValue("@Condition", row["Condition"].ToString());
704
                                    cmd.Parameters.AddWithValue("@Remarks", row["Remarks"].ToString());
705
                                    cmd.Parameters.AddWithValue("@GroundLevel", row["GroundLevel"].ToString());
706
                                    cmd.ExecuteNonQuery();
707
                                }
708
                            }
709
                            transaction.Commit();
710
                            connection.Close();
711
                        }
712
                        catch (Exception ex)
713
                        {
714
                            transaction.Rollback();
715
                            result = false;
716
                        }
717
                        finally
718
                        {
719
                            transaction.Dispose();
720
                        }
721
                    }
722
                }
723
                catch (Exception ex)
724
                {
725
                    System.Windows.Forms.MessageBox.Show(ex.Message);
726
                    result = false;
727
                }
728
                finally
729
                {
730
                    connection.Dispose();
731
                }
732
            }
733

    
734
            return result;
735
        }
736

    
737
        public static bool SaveView(List<string> values)
738
        {
739
            ID2Info id2Info = ID2Info.GetInstance();
740

    
741
            bool result = true;
742
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
743
            {
744
                try
745
                {
746
                    connection.Open();
747

    
748
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
749
                    {
750
                        try
751
                        {
752
                            using (SQLiteCommand cmd = connection.CreateCommand())
753
                            {
754
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_VIEW);
755
                                cmd.ExecuteNonQuery();
756

    
757
                                foreach (string value in values)
758
                                {
759
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@OID)", PSN_VIEW);
760
                                    cmd.Parameters.Clear();
761
                                    cmd.Parameters.AddWithValue("@OID", value);
762
                                    cmd.ExecuteNonQuery();
763
                                }
764
                            }
765
                            transaction.Commit();
766
                            connection.Close();
767
                        }
768
                        catch (Exception ex)
769
                        {
770
                            transaction.Rollback();
771
                            result = false;
772
                        }
773
                        finally
774
                        {
775
                            transaction.Dispose();
776
                        }
777
                    }
778
                }
779
                catch (Exception ex)
780
                {
781
                    System.Windows.Forms.MessageBox.Show(ex.Message);
782
                    result = false;
783
                }
784
                finally
785
                {
786
                    connection.Dispose();
787
                }
788
            }
789

    
790
            return result;
791
        }
792
        public static bool DeleteView()
793
        {
794
            ID2Info id2Info = ID2Info.GetInstance();
795

    
796
            bool result = true;
797
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
798
            {
799
                try
800
                {
801
                    connection.Open();
802

    
803
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
804
                    {
805
                        try
806
                        {
807
                            using (SQLiteCommand cmd = connection.CreateCommand())
808
                            {
809
                                cmd.CommandText = string.Format("DELETE FROM {0}", PSN_VIEW);
810
                                cmd.ExecuteNonQuery();
811
                            }
812
                            transaction.Commit();
813
                            connection.Close();
814
                        }
815
                        catch (Exception ex)
816
                        {
817
                            transaction.Rollback();
818
                            result = false;
819
                        }
820
                        finally
821
                        {
822
                            transaction.Dispose();
823
                        }
824
                    }
825
                }
826
                catch (Exception ex)
827
                {
828
                    System.Windows.Forms.MessageBox.Show(ex.Message);
829
                    result = false;
830
                }
831
                finally
832
                {
833
                    connection.Dispose();
834
                }
835
            }
836

    
837
            return result;
838
        }
839

    
840
        public static PSN GetDBPSN()
841
        {
842
            PSN result = new PSN();
843
            ID2Info id2Info = ID2Info.GetInstance();
844

    
845
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
846
            {
847
                try
848
                {
849
                    connection.Open();
850

    
851
                    using (SQLiteTransaction transaction = connection.BeginTransaction())
852
                    {
853
                        try
854
                        {
855
                            using (SQLiteCommand cmd = connection.CreateCommand())
856
                            {
857
                                DataTable psnDT = new DataTable();
858
                                cmd.CommandText = string.Format("SELECT * FROM {0} WHERE PSNRevisionNumber = '{1}'", PSN_PIPESYSTEMNETWORK, string.Format("{0:D3}", GetRevision()));
859
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
860
                                    psnDT.Load(dr);
861
                                result.PipeSystemNetwork = psnDT;
862

    
863
                                DataTable equipDT = new DataTable();
864
                                cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_EQUIPMENT);
865
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
866
                                    equipDT.Load(dr);
867
                                result.Equipment = equipDT;
868

    
869
                                DataTable nozzleDT = new DataTable();
870
                                cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_NOZZLE);
871
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
872
                                    nozzleDT.Load(dr);
873
                                result.Nozzle = nozzleDT;
874

    
875
                                DataTable pathItemDT = new DataTable();
876
                                cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_PATHITEMS);
877
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
878
                                    pathItemDT.Load(dr);
879
                                result.PathItems = pathItemDT;
880

    
881
                                DataTable seqDT = new DataTable();
882
                                cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_SEQUENCEDATA);
883
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
884
                                    seqDT.Load(dr);
885
                                result.SequenceData = seqDT;
886

    
887
                                result.Revision = GetRevision();
888
                            }
889
                            transaction.Commit();
890
                            connection.Close();
891
                        }
892
                        catch (Exception ex)
893
                        {
894
                            transaction.Rollback();
895
                            result = null;
896
                        }
897
                        finally
898
                        {
899
                            transaction.Dispose();
900
                        }
901
                    }
902
                }
903
                catch (Exception ex)
904
                {
905
                    System.Windows.Forms.MessageBox.Show(ex.Message);
906
                    result = null;
907
                }
908
                finally
909
                {
910
                    connection.Dispose();
911
                }
912
            }
913
            return result;
914
        }
915

    
916
        public static int GetRevision()
917
        {
918
            int result = 0;
919
            DataTable dt = new DataTable();
920
            ID2Info id2Info = ID2Info.GetInstance();
921
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
922
            {
923
                try
924
                {
925
                    connection.Open();
926
                    using (SQLiteCommand cmd = connection.CreateCommand())
927
                    {
928
                        cmd.CommandText = string.Format("SELECT DISTINCT PSNRevisionNumber FROM {0};", PSN_PIPESYSTEMNETWORK);
929
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
930
                            dt.Load(dr);
931

    
932
                        foreach (DataRow row in dt.Rows)
933
                        {
934
                            int revisionNumber = Convert.ToInt32(row["PSNRevisionNumber"]);
935
                            if (result < revisionNumber)
936
                                result = revisionNumber;
937
                        }
938
                    }
939
                    connection.Close();
940
                }
941
                catch (Exception ex)
942
                {
943
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
944
                    result = -1;
945
                }
946
                finally
947
                {
948
                    connection.Dispose();
949
                }
950
            }
951

    
952
            return result;
953
        }
954

    
955
        public static double[] GetDrawingSize()
956
        {
957
            double[] result = null;
958

    
959
            DataTable dt = new DataTable();
960
            ID2Info id2Info = ID2Info.GetInstance();
961
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
962
            {
963
                try
964
                {
965
                    connection.Open();
966
                    using (SQLiteCommand cmd = connection.CreateCommand())
967
                    {
968
                        cmd.CommandText = "SELECT value FROM Configuration WHERE Section = 'Area' AND Key = 'Drawing';";
969
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
970
                            dt.Load(dr);
971
                        
972
                        if (dt.Rows.Count == 1)
973
                        {
974
                            string value = dt.Rows[0][0].ToString();
975
                            string[] split = value.Split(new char[] { ',' });
976
                            result = new double[] {
977
                                Convert.ToDouble(Regex.Replace(split[0], @"[^0-9]", "")),
978
                                Convert.ToDouble(Regex.Replace(split[1], @"[^0-9]", "")),
979
                                Convert.ToDouble(Regex.Replace(split[2], @"[^0-9]", "")),
980
                                Convert.ToDouble(Regex.Replace(split[3], @"[^0-9]", ""))
981
                            };
982
                            result = new double[] {
983
                                Math.Min(result[0], result[2]),
984
                                Math.Min(result[1], result[3]),
985
                                Math.Max(result[0], result[2]),
986
                                Math.Max(result[1], result[3])
987
                            };
988
                        }
989
                    }
990
                    connection.Close();
991
                }
992
                catch (Exception ex)
993
                {
994
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
995
                }
996
                finally
997
                {
998
                    connection.Dispose();
999
                }
1000
            }
1001

    
1002
            return result;
1003
        }
1004

    
1005
        public static DataTable GetEquipmentType()
1006
        {
1007
            DataTable dt = new DataTable();
1008
            ID2Info id2Info = ID2Info.GetInstance();
1009
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1010
            {
1011
                try
1012
                {
1013
                    connection.Open();
1014
                    using (SQLiteCommand cmd = connection.CreateCommand())
1015
                    {
1016
                        cmd.CommandText = "SELECT Type FROM SymbolType WHERE Category = 'Equipment';";
1017
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1018
                            dt.Load(dr);
1019
                    }
1020
                    connection.Close();
1021
                }
1022
                catch (Exception ex)
1023
                {
1024
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1025
                }
1026
                finally
1027
                {
1028
                    connection.Dispose();
1029
                }
1030
            }
1031

    
1032
            return dt;
1033
        }
1034
        public static DataTable GetPathItem()
1035
        {
1036
            DataTable dt = new DataTable();
1037
            ID2Info id2Info = ID2Info.GetInstance();
1038
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1039
            {
1040
                try
1041
                {
1042
                    connection.Open();
1043
                    using (SQLiteCommand cmd = connection.CreateCommand())
1044
                    {
1045
                        cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_PATHITEMS);
1046
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1047
                            dt.Load(dr);
1048
                    }
1049
                    connection.Close();
1050
                }
1051
                catch (Exception ex)
1052
                {
1053
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1054
                }
1055
                finally
1056
                {
1057
                    connection.Dispose();
1058
                }
1059
            }
1060
            return dt;
1061
        }
1062
        public static DataTable GetTopologySet_OID()
1063
        {
1064
            DataTable dt = new DataTable();
1065
            ID2Info id2Info = ID2Info.GetInstance();
1066
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1067
            {
1068
                try
1069
                {
1070
                    connection.Open();
1071
                    using (SQLiteCommand cmd = connection.CreateCommand())
1072
                    {
1073
                        cmd.CommandText = string.Format("SELECT DISTINCT(TopologySet_OID) FROM {0}", PSN_PATHITEMS);
1074
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1075
                            dt.Load(dr);
1076
                    }
1077
                    connection.Close();
1078
                }
1079
                catch (Exception ex)
1080
                {
1081
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1082
                }
1083
                finally
1084
                {
1085
                    connection.Dispose();
1086
                }
1087
            }
1088
            return dt;
1089
        }
1090
        public static DataTable GetPipeSystemNetwork()
1091
        {
1092
            DataTable dt = new DataTable();
1093
            ID2Info id2Info = ID2Info.GetInstance();
1094
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1095
            {
1096
                try
1097
                {
1098
                    connection.Open();
1099
                    using (SQLiteCommand cmd = connection.CreateCommand())
1100
                    {
1101
                        cmd.CommandText = string.Format("SELECT * FROM {0} WHERE PSNRevisionNumber = '{1}'", PSN_PIPESYSTEMNETWORK, string.Format("{0:D3}", GetRevision()));
1102
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1103
                            dt.Load(dr);
1104
                    }
1105
                    connection.Close();
1106
                }
1107
                catch (Exception ex)
1108
                {
1109
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1110
                }
1111
                finally
1112
                {
1113
                    connection.Dispose();
1114
                }
1115
            }
1116
            return dt;
1117
        }
1118
        public static DataTable GetSequenceData()
1119
        {
1120
            DataTable dt = new DataTable();
1121
            ID2Info id2Info = ID2Info.GetInstance();
1122
            using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", id2Info.DBFilePath), true))
1123
            {
1124
                try
1125
                {
1126
                    connection.Open();
1127
                    using (SQLiteCommand cmd = connection.CreateCommand())
1128
                    {
1129
                        cmd.CommandText = string.Format("SELECT * FROM {0}", PSN_SEQUENCEDATA);
1130
                        using (SQLiteDataReader dr = cmd.ExecuteReader())
1131
                            dt.Load(dr);
1132
                    }
1133
                    connection.Close();
1134
                }
1135
                catch (Exception ex)
1136
                {
1137
                    Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1138
                }
1139
                finally
1140
                {
1141
                    connection.Dispose();
1142
                }
1143
            }
1144
            return dt;
1145
        }
1146
    }
1147
}
클립보드 이미지 추가 (최대 크기: 500 MB)