프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / DB / Project_DB.cs @ e75b5c63

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

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

    
16
namespace Converter.BaseModel
17
{
18
    public class Project_DB
19
    {
20
        const string SPPID_DB_INFO_TABLE = "T_SPPID_CONNECTION_INFO";
21
        const string SPPID_SYMBOL_MAPPING_TABLE = "T_SPPID_SYMBOL_MAPPING";
22
        const string SPPID_ATTRIBUTE_MAPPING_TABLE = "T_SPPID_ATTRIBUTE_MAPPING";
23
        const string SPPID_SETTING_TABLE = "T_SPPID_SETTING_TABLE";
24
        const string SPPID_LABEL_INFO_TABLE = "T_SPPID_LABEL_INFO";
25
        const string SPPID_DRAWING_INFO = "T_SPPID_DRAWING_INFO";
26
        const string SPPID_OPC_INFO = "T_SPPID_OPC_INFO";
27
        const string SPPID_PIPERUN_PROP_SETTING = "T_SPPID_PIPERUN_PROP_SETTING";
28
        const string ID2_DRAWINGATTRIBUTE_TABLE = "T_ID2_DRAWINGATTRIBUTE";
29
        const string ID2_NOMINALDIAMETER_TABLE = "NominalDiameter";
30

    
31
        const string LineProperties_TABLE = "LineProperties";
32
        const string LineTypes_TABLE = "LineTypes";
33
        const string SymbolType_TABLE = "SymbolType";
34
        const string SymbolAttribute_TABLE = "SymbolAttribute";
35
        const string Symbol_TABLE = "Symbol";
36
        const string OPCRelations_TABLE = "OPCRelations";
37
        const string DrawingAttribute_Table = "TitleBlockProperties";
38

    
39
        private static SqlConnection GetSqlConnection()
40
        {
41
            Project_Info projectInfo = Project_Info.GetInstance();
42
            SqlConnection connection = null;
43
            try
44
            {
45
                connection = new SqlConnection(string.Format(CultureInfo.CurrentCulture,
46
                    @"Data Source = {0},{1}; Initial Catalog = {2}; User ID = {3}; Password = {4};",
47
                    projectInfo.ServerIP,
48
                    projectInfo.Port,
49
                    System.IO.Path.GetFileName(projectInfo.DefaultPath),
50
                    projectInfo.DBUser,
51
                    projectInfo.DBPassword));
52

    
53
                connection.Open();
54
            }
55
            catch (Exception ex)
56
            {
57
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
58
                if (connection != null)
59
                    connection.Dispose();
60
                connection = null;
61
            }
62

    
63
            return connection;
64
        }
65

    
66
        public static bool ConnTestAndCreateTable()
67
        {
68
            bool result = false;
69
            Project_Info projectInfo = Project_Info.GetInstance();
70
            if (projectInfo.DBType == ID2DB_Type.SQLite)
71
            {
72
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath), true))
73
                {
74
                    try
75
                    {
76
                        connection.Open();
77
                        if (connection.State == ConnectionState.Open)
78
                        {
79
                            using (SQLiteCommand cmd = connection.CreateCommand())
80
                            {
81
                                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
82
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
83
                                using (DataTable dt = new DataTable())
84
                                {
85
                                    dt.Load(dr);
86

    
87
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
88
                                    {
89
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE);
90
                                        cmd.ExecuteNonQuery();
91
                                    }
92
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
93
                                    {
94
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT, SettingType TEXT)", SPPID_SETTING_TABLE);
95
                                        cmd.ExecuteNonQuery();
96
                                    }
97
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
98
                                    {
99
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, SPPID_SYMBOL_PATH TEXT, LEADERLINE BOOLEAN)", SPPID_SYMBOL_MAPPING_TABLE);
100
                                        cmd.ExecuteNonQuery();
101
                                    }
102
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
103
                                    {
104
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, SPPID_ATTRIBUTE TEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
105
                                        cmd.ExecuteNonQuery();
106
                                    }
107
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
108
                                    {
109
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BOOLEAN)", SPPID_LABEL_INFO_TABLE);
110
                                        cmd.ExecuteNonQuery();
111
                                    }
112
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
113
                                    {
114
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
115
                                        cmd.ExecuteNonQuery();
116
                                    }
117
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
118
                                    {
119
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID TEXT PRIMARY KEY, SPPID_OPC_MODELITEM_ID TEXT, ID2_DRAWING_UID TEXT, ATTRIBUTES TEXT, PAIRED BOOL)", SPPID_OPC_INFO);
120
                                        cmd.ExecuteNonQuery();
121
                                    }
122
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_PIPERUN_PROP_SETTING)).Length == 0)
123
                                    {
124
                                        cmd.CommandText = string.Format(@"CREATE TABLE {0} (UID TEXT NOT NULL PRIMARY KEY,
125
                                                                          ShowType int NOT NULL,Name TEXT NOT NULL,DisplayName TEXT NOT NULL,TableName TEXT NOT NULL,DataType TEXT NOT NULL,Value TEXT NOT NULL,
126
                                                                          Cond1_Name TEXT NOT NULL,Cond1_DisplayName TEXT NOT NULL,Cond1_TableName TEXT NOT NULL,Cond1_DataType TEXT NOT NULL,Cond1_Value TEXT NOT NULL,
127
                                                                          Cond2_Name TEXT,Cond2_DisplayName TEXT,Cond2_TableName TEXT,Cond2_DataType TEXT,Cond2_Value TEXT,SortOrder int NOT NULL)", SPPID_PIPERUN_PROP_SETTING);
128
                                        cmd.ExecuteNonQuery();
129
                                    }
130
                                    if (dt.Select(string.Format("NAME = '{0}'", ID2_DRAWINGATTRIBUTE_TABLE)).Length == 0)
131
                                    {
132
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT)", ID2_DRAWINGATTRIBUTE_TABLE);
133
                                        cmd.ExecuteNonQuery();
134

    
135
                                        List<string> datas = new List<string>() { "Unit", "Rev No", "Drawing No" };
136
                                        foreach (var data in datas)
137
                                        {
138
                                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES('{1}', '{1}')", ID2_DRAWINGATTRIBUTE_TABLE, data);
139
                                            cmd.ExecuteNonQuery();
140
                                        }
141
                                    }
142
                                }
143

    
144
                                #region Check Column 업데이트시 예비용
145
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_ATTRIBUTE_MAPPING_TABLE);
146
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
147
                                using (DataTable dt = new DataTable())
148
                                {
149
                                    dt.Load(dr);
150
                                    if (!dt.Columns.Contains("ISTEXT"))
151
                                    {
152
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN ISTEXT BOOLEAN", SPPID_ATTRIBUTE_MAPPING_TABLE);
153
                                        cmd.ExecuteNonQuery();
154
                                    }
155
                                }
156

    
157
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
158
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
159
                                using (DataTable dt = new DataTable())
160
                                {
161
                                    dt.Load(dr);
162
                                    if (!dt.Columns.Contains("LEADERLINE"))
163
                                    {
164
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN LEADERLINE BOOLEAN", SPPID_SYMBOL_MAPPING_TABLE);
165
                                        cmd.ExecuteNonQuery();
166
                                    }
167
                                }
168

    
169
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
170
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
171
                                using (DataTable dt = new DataTable())
172
                                {
173
                                    dt.Load(dr);
174
                                    if (!dt.Columns.Contains("SettingType"))
175
                                    {
176
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN SettingType Text", SPPID_SETTING_TABLE);
177
                                        cmd.ExecuteNonQuery();
178
                                    }
179
                                }
180

    
181
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
182
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
183
                                using (DataTable dt = new DataTable())
184
                                {
185
                                    dt.Load(dr);
186
                                    if (dt.Columns.Contains("DOCUMENT"))
187
                                    {
188
                                        cmd.CommandText = string.Format("DROP TABLE {0}", SPPID_DRAWING_INFO);
189
                                        cmd.ExecuteNonQuery();
190

    
191
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
192
                                        cmd.ExecuteNonQuery();
193
                                    }
194
                                }
195
                                #endregion
196
                            }
197
                            result = true;
198
                        }
199
                        connection.Close();
200
                    }
201
                    catch (Exception ex)
202
                    {
203
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
204
                    }
205
                    finally
206
                    {
207
                        connection.Dispose();
208
                    }
209
                }
210
            }
211
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
212
            {
213
                using (SqlConnection connection = GetSqlConnection())
214
                {
215
                    try
216
                    {
217
                        if (connection != null && connection.State == ConnectionState.Open)
218
                        {
219
                            using (SqlCommand cmd = connection.CreateCommand())
220
                            {
221
                                cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES";
222
                                using (SqlDataReader dr = cmd.ExecuteReader())
223
                                using (DataTable dt = new DataTable())
224
                                {
225
                                    dt.Load(dr);
226

    
227
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
228
                                    {
229
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX))", SPPID_DB_INFO_TABLE);
230
                                        cmd.ExecuteNonQuery();
231
                                    }
232
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
233
                                    {
234
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX), SettingType varchar(MAX))", SPPID_SETTING_TABLE);
235
                                        cmd.ExecuteNonQuery();
236
                                    }
237
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
238
                                    {
239
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(MAX), SPPID_SYMBOL_PATH varchar(MAX), LEADERLINE BIT)", SPPID_SYMBOL_MAPPING_TABLE);
240
                                        cmd.ExecuteNonQuery();
241
                                    }
242
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
243
                                    {
244
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, SPPID_ATTRIBUTE varchar(MAX))", SPPID_ATTRIBUTE_MAPPING_TABLE);
245
                                        cmd.ExecuteNonQuery();
246
                                    }
247
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
248
                                    {
249
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BIT)", SPPID_LABEL_INFO_TABLE);
250
                                        cmd.ExecuteNonQuery();
251
                                    }
252
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
253
                                    {
254
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID varchar(255) PRIMARY KEY, PATH varchar(MAX), DRAWINGNUMBER varchar(MAX), DRAWINGNAME varchar(MAX))", SPPID_DRAWING_INFO);
255
                                        cmd.ExecuteNonQuery();
256
                                    }
257
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
258
                                    {
259
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID varchar(255) PRIMARY KEY, SPPID_OPC_MODELITEM_ID varchar(MAX), ID2_DRAWING_UID varchar(MAX), ATTRIBUTES varchar(MAX), PAIRED BIT)", SPPID_OPC_INFO);
260
                                        cmd.ExecuteNonQuery();
261
                                    }
262
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_PIPERUN_PROP_SETTING)).Length == 0)
263
                                    {
264
                                        cmd.CommandText = string.Format(@"CREATE TABLE {0} (UID nvarchar(36) NOT NULL PRIMARY KEY,
265
                                                                          ShowType int NOT NULL,Name nvarchar(255) NOT NULL,DisplayName nvarchar(255) NOT NULL,TableName nvarchar(255) NOT NULL,DataType nvarchar(6) NOT NULL,Value nvarchar(255) NOT NULL,
266
                                                                          Cond1_Name nvarchar(255) NOT NULL,Cond1_DisplayName nvarchar(255) NOT NULL,Cond1_TableName nvarchar(255) NOT NULL,Cond1_DataType nvarchar(6) NOT NULL,Cond1_Value nvarchar(255) NOT NULL,
267
                                                                          Cond2_Name nvarchar(255),Cond2_DisplayName nvarchar(255),Cond2_TableName nvarchar(255),Cond2_DataType nvarchar(6),Cond2_Value nvarchar(255),SortOrder int NOT NULL)", SPPID_PIPERUN_PROP_SETTING);
268
                                        cmd.ExecuteNonQuery();
269
                                    }
270
                                    if (dt.Select(string.Format("NAME = '{0}'", ID2_DRAWINGATTRIBUTE_TABLE)).Length == 0)
271
                                    {
272
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(255))", ID2_DRAWINGATTRIBUTE_TABLE);
273
                                        cmd.ExecuteNonQuery();
274

    
275
                                        List<string> datas = new List<string>() { "Unit", "Rev No", "Drawing No" };
276
                                        foreach (var data in datas)
277
                                        {
278
                                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES('{1}', '{1}')", ID2_DRAWINGATTRIBUTE_TABLE, data);
279
                                            cmd.ExecuteNonQuery();
280
                                        }
281
                                    }
282
                                }
283

    
284
                                #region Check Column 업데이트시 예비용
285
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_ATTRIBUTE_MAPPING_TABLE);
286
                                using (SqlDataReader dr = cmd.ExecuteReader())
287
                                using (DataTable dt = new DataTable())
288
                                {
289
                                    dt.Load(dr);
290
                                    if (!dt.Columns.Contains("ISTEXT"))
291
                                    {
292
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD ISTEXT BIT", SPPID_ATTRIBUTE_MAPPING_TABLE);
293
                                        cmd.ExecuteNonQuery();
294
                                    }
295
                                }
296
                                #endregion
297
                            }
298
                            result = true;
299
                        }
300
                    }
301
                    catch (Exception ex)
302
                    {
303
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
304
                    }
305
                    finally
306
                    {
307
                        if (connection != null)
308
                            connection.Dispose();
309
                    }
310
                }
311
            }
312

    
313
            return result;
314
        }
315

    
316
        public static bool SaveSPPID_DB_INFO(string jsonString)
317
        {
318
            Project_Info projectInfo = Project_Info.GetInstance();
319
            if (projectInfo.DBType == ID2DB_Type.SQLite)
320
            {
321
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
322
                {
323

    
324
                    try
325
                    {
326
                        connection.Open();
327
                        using (SQLiteCommand cmd = connection.CreateCommand())
328
                        {
329
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
330
                            cmd.ExecuteNonQuery();
331

    
332
                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
333
                            cmd.Parameters.AddWithValue("@jsonString", jsonString);
334
                            cmd.ExecuteNonQuery();
335
                        }
336
                        connection.Close();
337
                    }
338
                    catch (Exception ex)
339
                    {
340
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
341
                        return false;
342
                    }
343
                    finally
344
                    {
345
                        connection.Dispose();
346
                    }
347
                }
348
            }
349
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
350
            {
351
                using (SqlConnection connection = GetSqlConnection())
352
                {
353
                    try
354
                    {
355
                        if (connection != null && connection.State == ConnectionState.Open)
356
                        {
357
                            using (SqlCommand cmd = connection.CreateCommand())
358
                            {
359
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
360
                                cmd.ExecuteNonQuery();
361

    
362
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
363
                                cmd.Parameters.AddWithValue("@jsonString", jsonString);
364
                                cmd.ExecuteNonQuery();
365
                            }
366
                            connection.Close();
367
                        }
368
                        else
369
                        {
370
                            return false;
371
                        }
372
                    }
373
                    catch (Exception ex)
374
                    {
375
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
376
                        return false;
377
                    }
378
                    finally
379
                    {
380
                        if (connection != null)
381
                            connection.Dispose();
382
                    }
383
                }
384
            }
385

    
386

    
387
            return true;
388
        }
389

    
390
        public static DataTable SelectSPPID_DB_INFO()
391
        {
392
            DataTable dt = new DataTable();
393
            Project_Info projectInfo = Project_Info.GetInstance();
394
            if (projectInfo.DBType == ID2DB_Type.SQLite)
395
            {
396
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
397
                {
398
                    try
399
                    {
400
                        connection.Open();
401
                        using (SQLiteCommand cmd = connection.CreateCommand())
402
                        {
403
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
404
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
405
                                dt.Load(dr);
406
                        }
407
                        connection.Close();
408
                    }
409
                    catch (Exception ex)
410
                    {
411
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
412
                    }
413
                    finally
414
                    {
415
                        connection.Dispose();
416
                    }
417
                }
418
            }
419
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
420
            {
421
                using (SqlConnection connection = GetSqlConnection())
422
                {
423
                    try
424
                    {
425
                        if (connection != null && connection.State == ConnectionState.Open)
426
                        {
427
                            using (SqlCommand cmd = connection.CreateCommand())
428
                            {
429
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
430
                                using (SqlDataReader dr = cmd.ExecuteReader())
431
                                    dt.Load(dr);
432
                            }
433
                            connection.Close();
434
                        }
435
                    }
436
                    catch (Exception ex)
437
                    {
438
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
439
                    }
440
                    finally
441
                    {
442
                        if (connection != null)
443
                            connection.Dispose();
444
                    }
445
                }
446
            }
447

    
448
            return dt;
449
        }
450

    
451
        public static bool SaveETCSetting(Dictionary<string,string> dicSetting)
452
        {
453
            Project_Info projectInfo = Project_Info.GetInstance();
454
            if (projectInfo.DBType == ID2DB_Type.SQLite)
455
            {
456
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
457
                {
458

    
459
                    try
460
                    {
461
                        connection.Open();
462
                        using (SQLiteCommand cmd = connection.CreateCommand())
463
                        {
464
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
465
                            cmd.ExecuteNonQuery();
466

    
467
                            foreach (var item in dicSetting)
468
                            {
469
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
470
                                cmd.Parameters.Clear();
471
                                cmd.Parameters.AddWithValue("@jsonString", item.Value);
472
                                cmd.Parameters.AddWithValue("@SettingType", item.Key);
473
                                cmd.ExecuteNonQuery();
474
                            }
475
                        }
476
                        connection.Close();
477
                    }
478
                    catch (Exception ex)
479
                    {
480
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
481
                        return false;
482
                    }
483
                    finally
484
                    {
485
                        connection.Dispose();
486
                    }
487
                }
488
            }
489
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
490
            {
491
                using (SqlConnection connection = GetSqlConnection())
492
                {
493
                    try
494
                    {
495
                        if (connection != null && connection.State == ConnectionState.Open)
496
                        {
497
                            using (SqlCommand cmd = connection.CreateCommand())
498
                            {
499
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
500
                                cmd.ExecuteNonQuery();
501

    
502
                                foreach (var item in dicSetting)
503
                                {
504
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
505
                                    cmd.Parameters.Clear();
506
                                    cmd.Parameters.AddWithValue("@jsonString", item.Value);
507
                                    cmd.Parameters.AddWithValue("@SettingType", item.Key);
508
                                    cmd.ExecuteNonQuery();
509
                                }
510
                            }
511
                            connection.Close();
512
                        }
513
                        else
514
                            return false;
515
                    }
516
                    catch (Exception ex)
517
                    {
518
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
519
                        return false;
520
                    }
521
                    finally
522
                    {
523
                        if (connection != null)
524
                            connection.Dispose();
525
                    }
526
                }
527
            }
528

    
529
            return true;
530
        }
531
        public static bool SavePipeRunPropSetting(List<SPPID_PipeRunPropSetting> setting)
532
        {
533
            Project_Info projectInfo = Project_Info.GetInstance();
534
            if (projectInfo.DBType == ID2DB_Type.SQLite)
535
            {
536
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
537
                {
538
                    try
539
                    {
540
                        connection.Open();
541
                        using (SQLiteCommand cmd = connection.CreateCommand())
542
                        {
543
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_PIPERUN_PROP_SETTING);
544
                            cmd.ExecuteNonQuery();
545

    
546
                            foreach (var item in setting)
547
                            {
548
                                cmd.CommandText = string.Format(@"INSERT INTO {0} 
549
                                                  (UID,ShowType,Name,DisplayName,TableName,DataType,Value,Cond1_Name,Cond1_DisplayName,Cond1_TableName,Cond1_DataType,Cond1_Value,Cond2_Name,Cond2_DisplayName,Cond2_TableName,Cond2_DataType,Cond2_Value,SortOrder)
550
                                                  VALUES
551
                                                  (@UID,@ShowType,@Name,@DisplayName,@TableName,@DataType,@Value,@Cond1_Name,@Cond1_DisplayName,@Cond1_TableName,@Cond1_DataType,@Cond1_Value,@Cond2_Name,@Cond2_DisplayName,@Cond2_TableName,@Cond2_DataType,@Cond2_Value,@SortOrder)"
552
                                                   , SPPID_PIPERUN_PROP_SETTING);
553
                                cmd.Parameters.Clear();
554
                                cmd.Parameters.AddWithValue("@UID"                  , item.UID);
555
                                cmd.Parameters.AddWithValue("@ShowType"             , item.ShowType);
556
                                cmd.Parameters.AddWithValue("@Name"                 , item.Name);
557
                                cmd.Parameters.AddWithValue("@DisplayName"          , item.DisplayName);
558
                                cmd.Parameters.AddWithValue("@TableName"            , item.TableName);
559
                                cmd.Parameters.AddWithValue("@DataType"             , item.DataType);
560
                                cmd.Parameters.AddWithValue("@Value"                , item.Value);
561
                                cmd.Parameters.AddWithValue("@Cond1_Name"           , item.Cond1_Name);
562
                                cmd.Parameters.AddWithValue("@Cond1_DisplayName"    , item.Cond1_DisplayName);
563
                                cmd.Parameters.AddWithValue("@Cond1_TableName"      , item.Cond1_TableName);
564
                                cmd.Parameters.AddWithValue("@Cond1_DataType"       , item.Cond1_DataType);
565
                                cmd.Parameters.AddWithValue("@Cond1_Value"          , item.Cond1_Value);
566
                                cmd.Parameters.AddWithValue("@Cond2_Name"           , item.Cond2_Name);
567
                                cmd.Parameters.AddWithValue("@Cond2_DisplayName"    , item.Cond2_DisplayName);
568
                                cmd.Parameters.AddWithValue("@Cond2_TableName"      , item.Cond2_TableName);
569
                                cmd.Parameters.AddWithValue("@Cond2_DataType"       , item.Cond2_DataType);
570
                                cmd.Parameters.AddWithValue("@Cond2_Value"          , item.Cond2_Value);
571
                                cmd.Parameters.AddWithValue("@SortOrder"            , item.SortOrder);
572
                                cmd.ExecuteNonQuery();
573
                            }
574
                        }
575
                        connection.Close();
576
                    }
577
                    catch (Exception ex)
578
                    {
579
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
580
                        return false;
581
                    }
582
                    finally
583
                    {
584
                        connection.Dispose();
585
                    }
586
                }
587
            }
588
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
589
            {
590
                using (SqlConnection connection = GetSqlConnection())
591
                {
592
                    try
593
                    {
594
                        if (connection != null && connection.State == ConnectionState.Open)
595
                        {
596
                            using (SqlCommand cmd = connection.CreateCommand())
597
                            {
598
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_PIPERUN_PROP_SETTING);
599
                                cmd.ExecuteNonQuery();
600

    
601
                                foreach (var item in setting)
602
                                {
603
                                    cmd.CommandText = string.Format(@"INSERT INTO {0} 
604
                                                  (UID,ShowType,Name,DisplayName,TableName,DataType,Value,Cond1_Name,Cond1_DisplayName,Cond1_TableName,Cond1_DataType,Cond1_Value,Cond2_Name,Cond2_DisplayName,Cond2_TableName,Cond2_DataType,Cond2_Value,SortOrder)
605
                                                  VALUES
606
                                                  (@UID,@ShowType,@Name,@DisplayName,@TableName,@DataType,@Value,@Cond1_Name,@Cond1_DisplayName,@Cond1_TableName,@Cond1_DataType,@Cond1_Value,@Cond2_Name,@Cond2_DisplayName,@Cond2_TableName,@Cond2_DataType,@Cond2_Value,@SortOrder)"
607
                                                   , SPPID_PIPERUN_PROP_SETTING);
608
                                    cmd.Parameters.Clear();
609
                                    cmd.Parameters.AddWithValue("@UID"                  , item.UID);
610
                                    cmd.Parameters.AddWithValue("@ShowType"             , item.ShowType);
611
                                    cmd.Parameters.AddWithValue("@Name"                 , item.Name);
612
                                    cmd.Parameters.AddWithValue("@DisplayName"          , item.DisplayName);
613
                                    cmd.Parameters.AddWithValue("@TableName"            , item.TableName);
614
                                    cmd.Parameters.AddWithValue("@DataType"             , item.DataType);
615
                                    cmd.Parameters.AddWithValue("@Value"                , item.Value);
616
                                    cmd.Parameters.AddWithValue("@Cond1_Name"           , item.Cond1_Name);
617
                                    cmd.Parameters.AddWithValue("@Cond1_DisplayName"    , item.Cond1_DisplayName);
618
                                    cmd.Parameters.AddWithValue("@Cond1_TableName"      , item.Cond1_TableName);
619
                                    cmd.Parameters.AddWithValue("@Cond1_DataType"       , item.Cond1_DataType);
620
                                    cmd.Parameters.AddWithValue("@Cond1_Value"          , item.Cond1_Value);
621
                                    cmd.Parameters.AddWithValue("@Cond2_Name"           , item.Cond2_Name);
622
                                    cmd.Parameters.AddWithValue("@Cond2_DisplayName"    , item.Cond2_DisplayName);
623
                                    cmd.Parameters.AddWithValue("@Cond2_TableName"      , item.Cond2_TableName);
624
                                    cmd.Parameters.AddWithValue("@Cond2_DataType"       , item.Cond2_DataType);
625
                                    cmd.Parameters.AddWithValue("@Cond2_Value"          , item.Cond2_Value);
626
                                    cmd.Parameters.AddWithValue("@SortOrder"            , item.SortOrder);
627
                                cmd.ExecuteNonQuery();
628
                                }
629
                            }
630
                            connection.Close();
631
                        }
632
                        else
633
                            return false;
634
                    }
635
                    catch (Exception ex)
636
                    {
637
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
638
                        return false;
639
                    }
640
                    finally
641
                    {
642
                        if (connection != null)
643
                            connection.Dispose();
644
                    }
645
                }
646
            }
647

    
648
            return true;
649
        }
650

    
651
        public static DataTable SelectSetting()
652
        {
653
            DataTable dt = new DataTable();
654
            Project_Info projectInfo = Project_Info.GetInstance();
655
            if (projectInfo.DBType == ID2DB_Type.SQLite)
656
            {
657
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
658
                {
659
                    try
660
                    {
661
                        connection.Open();
662
                        using (SQLiteCommand cmd = connection.CreateCommand())
663
                        {
664
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
665
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
666
                                dt.Load(dr);
667
                        }
668
                        connection.Close();
669
                    }
670
                    catch (Exception ex)
671
                    {
672
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
673
                    }
674
                    finally
675
                    {
676
                        connection.Dispose();
677
                    }
678
                }
679
            }
680
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
681
            {
682
                using (SqlConnection connection = GetSqlConnection())
683
                {
684
                    try
685
                    {
686
                        if (connection != null && connection.State == ConnectionState.Open)
687
                        {
688
                            using (SqlCommand cmd = connection.CreateCommand())
689
                            {
690
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
691
                                using (SqlDataReader dr = cmd.ExecuteReader())
692
                                    dt.Load(dr);
693
                            }
694
                            connection.Close();
695
                        }
696
                    }
697
                    catch (Exception ex)
698
                    {
699
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
700
                    }
701
                    finally
702
                    {
703
                        if (connection != null)
704
                            connection.Dispose();
705
                    }
706
                }
707
            }
708

    
709
            return dt;
710
        }
711

    
712
        public static DataTable SelectProjectSymbol()
713
        {
714
            DataTable dt = new DataTable();
715
            Project_Info projectInfo = Project_Info.GetInstance();
716
            if (projectInfo.DBType == ID2DB_Type.SQLite)
717
            {
718
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
719
                {
720
                    try
721
                    {
722
                        connection.Open();
723
                        using (SQLiteCommand cmd = connection.CreateCommand())
724
                        {
725
                            cmd.CommandText = string.Format(@"
726
                            SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
727
                                LEFT OUTER JOIN {2} as sp 
728
                                    ON s.UID = SP.UID 
729
                            WHERE s.SymbolType_UID = st.UID 
730
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
731
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
732
                                dt.Load(dr);
733

    
734
                            DataTable dtClone = dt.Clone();
735
                            dtClone.Columns["UID"].DataType = typeof(string);
736
                            foreach (DataRow row in dt.Rows)
737
                            {
738
                                dtClone.ImportRow(row);
739
                            }
740
                            dt.Dispose();
741
                            dt = dtClone;
742
                        }
743
                        connection.Close();
744
                    }
745
                    catch (Exception ex)
746
                    {
747
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
748
                    }
749
                    finally
750
                    {
751
                        connection.Dispose();
752
                    }
753
                }
754
            }
755
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
756
            {
757
                using (SqlConnection connection = GetSqlConnection())
758
                {
759
                    try
760
                    {
761
                        if (connection != null && connection.State == ConnectionState.Open)
762
                        {
763
                            using (SqlCommand cmd = connection.CreateCommand())
764
                            {
765
                                cmd.CommandText = string.Format(@"
766
                            SELECT CONVERT(VARCHAR(255), s.UID) AS UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
767
                                LEFT OUTER JOIN {2} as sp 
768
                                    ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID)
769
                            WHERE s.SymbolType_UID = st.UID 
770
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
771
                                using (SqlDataReader dr = cmd.ExecuteReader())
772
                                    dt.Load(dr);
773
                            }
774
                            connection.Close();
775
                        }
776
                    }
777
                    catch (Exception ex)
778
                    {
779
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
780
                    }
781
                    finally
782
                    {
783
                        if (connection != null)
784
                            connection.Dispose();
785
                    }
786
                }
787
            }
788

    
789
            return dt;
790
        }
791

    
792
        public static DataTable SelectProjectChildSymbol()
793
        {
794
            DataTable result = new DataTable();
795
            result.Columns.Add("UID");
796
            result.Columns.Add("Name");
797
            result.Columns.Add("Type");
798
            result.Columns.Add("SPPID_SYMBOL_PATH");
799
            
800
            Project_Info projectInfo = Project_Info.GetInstance();
801
            if (projectInfo.DBType == ID2DB_Type.SQLite)
802
            {
803
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
804
                using (DataTable dt = new DataTable())
805
                {
806
                    try
807
                    {
808
                        connection.Open();
809
                        using (SQLiteCommand cmd = connection.CreateCommand())
810
                        {
811
                            cmd.CommandText = string.Format(@"
812
                            SELECT AdditionalSymbol FROM Symbol");
813
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
814
                                dt.Load(dr);
815
                            List<string> childList = new List<string>();
816
                            foreach (DataRow row in dt.Rows)
817
                            {
818
                                if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
819
                                {
820
                                    string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
821
                                    foreach (var childString in array)
822
                                    {
823
                                        childList.Add(childString.Split(new char[] { ',' })[2]);
824
                                    }
825
                                }
826

    
827
                            }
828

    
829
                            dt.Clear();
830
                            cmd.Reset();
831
                            cmd.CommandText = string.Format(@"
832
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
833
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
834
                                dt.Load(dr);
835

    
836
                            childList = childList.Distinct().ToList();
837
                            foreach (var child in childList)
838
                            {
839
                                string mappingPath = string.Empty;
840
                                DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
841
                                if (rows.Length == 1)
842
                                    mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
843

    
844
                                DataRow newRow = result.NewRow();
845
                                newRow["UID"] = child;
846
                                newRow["Name"] = child;
847
                                newRow["Type"] = "Child Symbol";
848
                                newRow["SPPID_SYMBOL_PATH"] = mappingPath;
849
                                result.Rows.Add(newRow);
850
                            }
851
                        }
852
                        connection.Close();
853
                    }
854
                    catch (Exception ex)
855
                    {
856
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
857
                    }
858
                    finally
859
                    {
860
                        connection.Dispose();
861
                    }
862
                }
863
            }
864
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
865
            {
866
                using (SqlConnection connection = GetSqlConnection())
867
                using (DataTable dt = new DataTable())
868
                {
869
                    try
870
                    {
871
                        if (connection != null && connection.State == ConnectionState.Open)
872
                        {
873
                            using (SqlCommand cmd = connection.CreateCommand())
874
                            {
875
                                cmd.CommandText = string.Format(@"
876
                            SELECT AdditionalSymbol FROM Symbol");
877
                                using (SqlDataReader dr = cmd.ExecuteReader())
878
                                    dt.Load(dr);
879
                                List<string> childList = new List<string>();
880
                                foreach (DataRow row in dt.Rows)
881
                                {
882
                                    if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
883
                                    {
884
                                        string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
885
                                        foreach (var childString in array)
886
                                        {
887
                                            childList.Add(childString.Split(new char[] { ',' })[2]);
888
                                        }
889
                                    }
890

    
891
                                }
892

    
893
                                dt.Clear();
894
                                cmd.CommandText = string.Format(@"
895
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
896
                                using (SqlDataReader dr = cmd.ExecuteReader())
897
                                    dt.Load(dr);
898

    
899
                                childList = childList.Distinct().ToList();
900
                                foreach (var child in childList)
901
                                {
902
                                    string mappingPath = string.Empty;
903
                                    DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
904
                                    if (rows.Length == 1)
905
                                        mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
906

    
907
                                    DataRow newRow = result.NewRow();
908
                                    newRow["UID"] = child;
909
                                    newRow["Name"] = child;
910
                                    newRow["Type"] = "Child Symbol";
911
                                    newRow["SPPID_SYMBOL_PATH"] = mappingPath;
912
                                    result.Rows.Add(newRow);
913
                                }
914
                            }
915
                            connection.Close();
916
                        }
917
                    }
918
                    catch (Exception ex)
919
                    {
920
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
921
                    }
922
                    finally
923
                    {
924
                        if (connection != null)
925
                            connection.Dispose();
926
                    }
927
                }
928
            }
929
            return result;
930
        }
931

    
932
        public static DataTable SelectProjectLine()
933
        {
934
            DataTable dt = new DataTable();
935
            Project_Info projectInfo = Project_Info.GetInstance();
936
            if (projectInfo.DBType == ID2DB_Type.SQLite)
937
            {
938
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
939
                {
940
                    try
941
                    {
942
                        connection.Open();
943
                        using (SQLiteCommand cmd = connection.CreateCommand())
944
                        {
945
                            cmd.CommandText = string.Format(@"
946
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
947
                                LEFT OUTER JOIN {1} as sp 
948
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
949
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
950
                                dt.Load(dr);
951
                        }
952
                        connection.Close();
953
                    }
954
                    catch (Exception ex)
955
                    {
956
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
957
                    }
958
                    finally
959
                    {
960
                        connection.Dispose();
961
                    }
962
                }
963
            }
964
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
965
            {
966
                using (SqlConnection connection = GetSqlConnection())
967
                {
968
                    try
969
                    {
970
                        if (connection != null && connection.State == ConnectionState.Open)
971
                        {
972
                            using (SqlCommand cmd = connection.CreateCommand())
973
                            {
974
                                cmd.CommandText = string.Format(@"
975
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
976
                                LEFT OUTER JOIN {1} as sp 
977
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
978
                                using (SqlDataReader dr = cmd.ExecuteReader())
979
                                    dt.Load(dr);
980
                            }
981
                            connection.Close();
982
                        }
983
                    }
984
                    catch (Exception ex)
985
                    {
986
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
987
                    }
988
                    finally
989
                    {
990
                        if (connection != null)
991
                            connection.Dispose();
992
                    }
993
                }
994
            }
995

    
996
            return dt;
997
        }
998

    
999
        public static DataTable SelectProjectLineProperties()
1000
        {
1001
            DataTable dt = new DataTable();
1002
            Project_Info projectInfo = Project_Info.GetInstance();
1003
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1004
            {
1005
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1006
                {
1007
                    try
1008
                    {
1009
                        connection.Open();
1010
                        using (SQLiteCommand cmd = connection.CreateCommand())
1011
                        {
1012
                            cmd.CommandText = string.Format(@"
1013
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
1014
                            FROM {0} as lp 
1015
                                 LEFT OUTER JOIN {1} as sp 
1016
                                      ON lp.UID = sp.UID
1017
                                 LEFT OUTER JOIN {2} as spa 
1018
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
1019
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1020
                                dt.Load(dr);
1021
                        }
1022
                        connection.Close();
1023
                    }
1024
                    catch (Exception ex)
1025
                    {
1026
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1027
                    }
1028
                    finally
1029
                    {
1030
                        connection.Dispose();
1031
                    }
1032
                }
1033
            }
1034
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1035
            {
1036
                using (SqlConnection connection = GetSqlConnection())
1037
                {
1038
                    try
1039
                    {
1040
                        if (connection != null && connection.State == ConnectionState.Open)
1041
                        {
1042
                            using (SqlCommand cmd = connection.CreateCommand())
1043
                            {
1044
                                cmd.CommandText = string.Format(@"
1045
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
1046
                            FROM {0} as lp 
1047
                                 LEFT OUTER JOIN {1} as sp 
1048
                                      ON lp.UID = sp.UID
1049
                                 LEFT OUTER JOIN {2} as spa 
1050
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
1051
                                using (SqlDataReader dr = cmd.ExecuteReader())
1052
                                    dt.Load(dr);
1053
                            }
1054
                            connection.Close();
1055
                        }
1056
                    }
1057
                    catch (Exception ex)
1058
                    {
1059
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1060
                    }
1061
                    finally
1062
                    {
1063
                        if (connection != null)
1064
                            connection.Dispose();
1065
                    }
1066
                }
1067
            }
1068

    
1069
            return dt;
1070
        }
1071
        public static DataTable SelectProjectNominalDiameter()
1072
        {
1073
            DataTable dt = new DataTable();
1074
            Project_Info projectInfo = Project_Info.GetInstance();
1075
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1076
            {
1077
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1078
                {
1079
                    try
1080
                    {
1081
                        connection.Open();
1082
                        using (SQLiteCommand cmd = connection.CreateCommand())
1083
                        {
1084
                            cmd.CommandText = string.Format(@"SELECT * FROM {0};", ID2_NOMINALDIAMETER_TABLE);
1085
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1086
                                dt.Load(dr);
1087
                        }
1088
                        connection.Close();
1089
                    }
1090
                    catch (Exception ex)
1091
                    {
1092
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1093
                    }
1094
                    finally
1095
                    {
1096
                        connection.Dispose();
1097
                    }
1098
                }
1099
            }
1100
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1101
            {
1102
                using (SqlConnection connection = GetSqlConnection())
1103
                {
1104
                    try
1105
                    {
1106
                        if (connection != null && connection.State == ConnectionState.Open)
1107
                        {
1108
                            using (SqlCommand cmd = connection.CreateCommand())
1109
                            {
1110
                                cmd.CommandText = string.Format(@"SELECT * FROM {0};", ID2_NOMINALDIAMETER_TABLE);
1111
                                using (SqlDataReader dr = cmd.ExecuteReader())
1112
                                    dt.Load(dr);
1113
                            }
1114
                            connection.Close();
1115
                        }
1116
                    }
1117
                    catch (Exception ex)
1118
                    {
1119
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1120
                    }
1121
                    finally
1122
                    {
1123
                        if (connection != null)
1124
                            connection.Dispose();
1125
                    }
1126
                }
1127
            }
1128

    
1129
            return dt;
1130
        }
1131
        public static DataTable SelectProjectAttribute()
1132
        {
1133
            DataTable dt = new DataTable();
1134
            Project_Info projectInfo = Project_Info.GetInstance();
1135
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1136
            {
1137
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1138
                {
1139
                    try
1140
                    {
1141
                        connection.Open();
1142
                        using (SQLiteCommand cmd = connection.CreateCommand())
1143
                        {
1144
                            cmd.CommandText = string.Format(@"
1145
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spa.ISTEXT, spl.LOCATION, spl.LEADERLINE, sa.Property
1146
                            FROM {1} as sa, {0} as st 
1147
                                 LEFT OUTER JOIN {2} as sp 
1148
                                      ON sa.UID = SP.UID 
1149
                                LEFT OUTER JOIN {3} as spa 
1150
                                     ON sa.UID = spa.UID
1151
                                LEFT OUTER JOIN {4} as spl 
1152
                                     ON sa.UID = spl.UID
1153
                            WHERE sa.SymbolType_UID = st.UID AND (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE, SPPID_LABEL_INFO_TABLE);
1154
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1155
                                dt.Load(dr);
1156
                        }
1157
                        connection.Close();
1158
                    }
1159
                    catch (Exception ex)
1160
                    {
1161
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1162
                    }
1163
                    finally
1164
                    {
1165
                        connection.Dispose();
1166
                    }
1167
                }
1168
            }
1169
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1170
            {
1171
                using (SqlConnection connection = GetSqlConnection())
1172
                {
1173
                    try
1174
                    {
1175
                        if (connection != null && connection.State == ConnectionState.Open)
1176
                        {
1177
                            using (SqlCommand cmd = connection.CreateCommand())
1178
                            {
1179
                                cmd.CommandText = string.Format(@"
1180
                            SELECT sa.UID, sa.DisplayAttribute, st.Type, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spa.ISTEXT, spl.LOCATION, spl.LEADERLINE, sa.Property
1181
                            FROM {1} as sa
1182
                                 LEFT OUTER JOIN {2} as sp 
1183
                                      ON sa.UID = SP.UID 
1184
                                LEFT OUTER JOIN {3} as spa 
1185
                                     ON sa.UID = spa.UID
1186
                                LEFT OUTER JOIN {4} as spl 
1187
                                     ON sa.UID = spl.UID
1188
                                LEFT OUTER JOIN {0} as st 
1189
                                     ON sa.SymbolType_UID = st.UID 
1190
                            WHERE (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE, SPPID_LABEL_INFO_TABLE);
1191
                                using (SqlDataReader dr = cmd.ExecuteReader())
1192
                                    dt.Load(dr);
1193
                            }
1194
                            connection.Close();
1195
                        }
1196
                    }
1197
                    catch (Exception ex)
1198
                    {
1199
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1200
                    }
1201
                    finally
1202
                    {
1203
                        if (connection != null)
1204
                            connection.Dispose();
1205
                    }
1206
                }
1207
            }
1208

    
1209

    
1210
            return dt;
1211
        }
1212

    
1213
        public static DataTable SelectDrawingProjectAttribute()
1214
        {
1215
            DataTable dt = new DataTable();
1216
            Project_Info projectInfo = Project_Info.GetInstance();
1217
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1218
            {
1219
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1220
                {
1221
                    try
1222
                    {
1223
                        connection.Open();
1224
                        using (SQLiteCommand cmd = connection.CreateCommand())
1225
                        {
1226
                            cmd.CommandText = string.Format(@"
1227
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1228
                            FROM {0} as da 
1229
                                 LEFT OUTER JOIN {1} as sam 
1230
                                      ON da.UID = sam.UID;
1231
                            ", DrawingAttribute_Table, SPPID_ATTRIBUTE_MAPPING_TABLE);
1232
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1233
                                dt.Load(dr);
1234
                        }
1235
                        connection.Close();
1236
                    }
1237
                    catch (Exception ex)
1238
                    {
1239
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1240
                    }
1241
                    finally
1242
                    {
1243
                        connection.Dispose();
1244
                    }
1245
                }
1246
            }
1247
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1248
            {
1249
                using (SqlConnection connection = GetSqlConnection())
1250
                {
1251
                    try
1252
                    {
1253
                        if (connection != null && connection.State == ConnectionState.Open)
1254
                        {
1255
                            using (SqlCommand cmd = connection.CreateCommand())
1256
                            {
1257
                                cmd.CommandText = string.Format(@"
1258
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1259
                            FROM {0} as da
1260
                                 LEFT OUTER JOIN {1} as sam 
1261
                                      ON da.UID = sam.UID;
1262
                            ", DrawingAttribute_Table, SPPID_ATTRIBUTE_MAPPING_TABLE);
1263
                                using (SqlDataReader dr = cmd.ExecuteReader())
1264
                                    dt.Load(dr);
1265
                            }
1266
                            connection.Close();
1267
                        }
1268
                    }
1269
                    catch (Exception ex)
1270
                    {
1271
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1272
                    }
1273
                    finally
1274
                    {
1275
                        if (connection != null)
1276
                            connection.Dispose();
1277
                    }
1278
                }
1279
            }
1280

    
1281
            dt.Merge(SelectDrawingProjectAttribute2());
1282

    
1283
            return dt;
1284
        }
1285

    
1286
        private static DataTable SelectDrawingProjectAttribute2()
1287
        {
1288
            DataTable dt = new DataTable();
1289
            Project_Info projectInfo = Project_Info.GetInstance();
1290
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1291
            {
1292
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1293
                {
1294
                    try
1295
                    {
1296
                        connection.Open();
1297
                        using (SQLiteCommand cmd = connection.CreateCommand())
1298
                        {
1299
                            cmd.CommandText = string.Format(@"
1300
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1301
                            FROM {0} as da 
1302
                                 LEFT OUTER JOIN {1} as sam 
1303
                                      ON da.UID = sam.UID;
1304
                            ", ID2_DRAWINGATTRIBUTE_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
1305
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1306
                                dt.Load(dr);
1307
                        }
1308
                        connection.Close();
1309
                    }
1310
                    catch (Exception ex)
1311
                    {
1312
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1313
                    }
1314
                    finally
1315
                    {
1316
                        connection.Dispose();
1317
                    }
1318
                }
1319
            }
1320
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1321
            {
1322
                using (SqlConnection connection = GetSqlConnection())
1323
                {
1324
                    try
1325
                    {
1326
                        if (connection != null && connection.State == ConnectionState.Open)
1327
                        {
1328
                            using (SqlCommand cmd = connection.CreateCommand())
1329
                            {
1330
                                cmd.CommandText = string.Format(@"
1331
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1332
                            FROM {0} as da
1333
                                 LEFT OUTER JOIN {1} as sam 
1334
                                      ON da.UID = sam.UID;
1335
                            ", ID2_DRAWINGATTRIBUTE_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
1336
                                using (SqlDataReader dr = cmd.ExecuteReader())
1337
                                    dt.Load(dr);
1338
                            }
1339
                            connection.Close();
1340
                        }
1341
                    }
1342
                    catch (Exception ex)
1343
                    {
1344
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1345
                    }
1346
                    finally
1347
                    {
1348
                        if (connection != null)
1349
                            connection.Dispose();
1350
                    }
1351
                }
1352
            }
1353

    
1354
            return dt;
1355
        }
1356

    
1357
        public static DataTable SelectID2SymbolTable()
1358
        {
1359
            DataTable dt = new DataTable();
1360
            Project_Info projectInfo = Project_Info.GetInstance();
1361
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1362
            {
1363
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1364
                {
1365
                    try
1366
                    {
1367
                        connection.Open();
1368
                        using (SQLiteCommand cmd = connection.CreateCommand())
1369
                        {
1370
                            cmd.CommandText = @"SELECT * FROM Symbol";
1371
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1372
                                dt.Load(dr);
1373
                        }
1374
                        connection.Close();
1375
                    }
1376
                    catch (Exception ex)
1377
                    {
1378
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1379
                    }
1380
                    finally
1381
                    {
1382
                        connection.Dispose();
1383
                    }
1384
                }
1385
            }
1386
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1387
            {
1388
                using (SqlConnection connection = GetSqlConnection())
1389
                {
1390
                    try
1391
                    {
1392
                        if (connection != null && connection.State == ConnectionState.Open)
1393
                        {
1394
                            using (SqlCommand cmd = connection.CreateCommand())
1395
                            {
1396
                                cmd.CommandText = @"SELECT * FROM Symbol";
1397
                                using (SqlDataReader dr = cmd.ExecuteReader())
1398
                                    dt.Load(dr);
1399
                            }
1400
                            connection.Close();
1401
                        }
1402
                    }
1403
                    catch (Exception ex)
1404
                    {
1405
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1406
                    }
1407
                    finally
1408
                    {
1409
                        if (connection != null)
1410
                            connection.Dispose();
1411
                    }
1412
                }
1413
            }
1414

    
1415
            return dt;
1416
        }
1417

    
1418
        public static DataTable SelectOPCRelations()
1419
        {
1420
            DataTable dt = new DataTable();
1421
            Project_Info projectInfo = Project_Info.GetInstance();
1422
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1423
            {
1424
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1425
                {
1426
                    try
1427
                    {
1428
                        connection.Open();
1429
                        using (SQLiteCommand cmd = connection.CreateCommand())
1430
                        {
1431
                            cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1432
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1433
                                dt.Load(dr);
1434
                        }
1435
                        connection.Close();
1436
                    }
1437
                    catch (Exception ex)
1438
                    {
1439
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1440
                    }
1441
                    finally
1442
                    {
1443
                        connection.Dispose();
1444
                    }
1445
                }
1446
            }
1447
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1448
            {
1449
                using (SqlConnection connection = GetSqlConnection())
1450
                {
1451
                    try
1452
                    {
1453
                        if (connection != null && connection.State == ConnectionState.Open)
1454
                        {
1455
                            using (SqlCommand cmd = connection.CreateCommand())
1456
                            {
1457
                                cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1458
                                using (SqlDataReader dr = cmd.ExecuteReader())
1459
                                    dt.Load(dr);
1460
                            }
1461
                            connection.Close();
1462
                        }
1463
                    }
1464
                    catch (Exception ex)
1465
                    {
1466
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1467
                    }
1468
                    finally
1469
                    {
1470
                        if (connection != null)
1471
                            connection.Dispose();
1472
                    }
1473
                }
1474
            }
1475

    
1476
            return dt;
1477
        }
1478

    
1479
        public static DataTable SelectDrawings()
1480
        {
1481
            DataTable dt = new DataTable();
1482
            Project_Info projectInfo = Project_Info.GetInstance();
1483
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1484
            {
1485
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1486
                {
1487
                    try
1488
                    {
1489
                        connection.Open();
1490
                        using (SQLiteCommand cmd = connection.CreateCommand())
1491
                        {
1492
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1493
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1494
                                dt.Load(dr);
1495
                        }
1496
                        connection.Close();
1497
                    }
1498
                    catch (Exception ex)
1499
                    {
1500
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1501
                    }
1502
                    finally
1503
                    {
1504
                        connection.Dispose();
1505
                    }
1506
                }
1507
            }
1508
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1509
            {
1510
                using (SqlConnection connection = GetSqlConnection())
1511
                {
1512
                    try
1513
                    {
1514
                        if (connection != null && connection.State == ConnectionState.Open)
1515
                        {
1516
                            using (SqlCommand cmd = connection.CreateCommand())
1517
                            {
1518
                                cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1519
                                using (SqlDataReader dr = cmd.ExecuteReader())
1520
                                    dt.Load(dr);
1521
                            }
1522
                            connection.Close();
1523
                        }
1524
                    }
1525
                    catch (Exception ex)
1526
                    {
1527
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1528
                    }
1529
                    finally
1530
                    {
1531
                        if (connection != null)
1532
                            connection.Dispose();
1533
                    }
1534
                }
1535
            }
1536

    
1537
            return dt;
1538
        }
1539

    
1540
        public static DataTable SelectOPCInfo()
1541
        {
1542
            DataTable dt = new DataTable();
1543
            Project_Info projectInfo = Project_Info.GetInstance();
1544
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1545
            {
1546
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1547
                {
1548
                    try
1549
                    {
1550
                        connection.Open();
1551
                        using (SQLiteCommand cmd = connection.CreateCommand())
1552
                        {
1553
                            cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO);
1554
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1555
                                dt.Load(dr);
1556
                        }
1557
                        connection.Close();
1558
                    }
1559
                    catch (Exception ex)
1560
                    {
1561
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1562
                    }
1563
                    finally
1564
                    {
1565
                        connection.Dispose();
1566
                    }
1567
                }
1568
            }
1569
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1570
            {
1571
                using (SqlConnection connection = GetSqlConnection())
1572
                {
1573
                    try
1574
                    {
1575
                        if (connection != null && connection.State == ConnectionState.Open)
1576
                        {
1577
                            using (SqlCommand cmd = connection.CreateCommand())
1578
                            {
1579
                                cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = 0", SPPID_OPC_INFO);
1580
                                using (SqlDataReader dr = cmd.ExecuteReader())
1581
                                    dt.Load(dr);
1582
                            }
1583
                            connection.Close();
1584
                        }
1585
                    }
1586
                    catch (Exception ex)
1587
                    {
1588
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1589
                    }
1590
                    finally
1591
                    {
1592
                        if (connection != null)
1593
                            connection.Dispose();
1594
                    }
1595
                }
1596
            }
1597

    
1598
            return dt;
1599
        }
1600

    
1601
        public static DataTable SelectPipeRunPropSetting()
1602
        {
1603
            DataTable dt = new DataTable();
1604
            Project_Info projectInfo = Project_Info.GetInstance();
1605
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1606
            {
1607
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1608
                {
1609
                    try
1610
                    {
1611
                        connection.Open();
1612
                        using (SQLiteCommand cmd = connection.CreateCommand())
1613
                        {
1614
                            cmd.CommandText = string.Format(@"
1615
                            SELECT s.UID, s.ShowType, s.Name, s.DisplayName, s.TableName, s.DataType, s.Value, s.Cond1_Name, s.Cond1_DisplayName, s.Cond1_TableName, s.Cond1_DataType, s.Cond1_Value, s.Cond2_Name, s.Cond2_DisplayName, s.Cond2_TableName, s.Cond2_DataType, s.Cond2_Value, s.SortOrder
1616
                              FROM {0} as s
1617
                              ORDER BY s.SortOrder ASC;", SPPID_PIPERUN_PROP_SETTING);
1618
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1619
                                dt.Load(dr);
1620
                        }
1621
                        connection.Close();
1622
                    }
1623
                    catch (Exception ex)
1624
                    {
1625
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1626
                    }
1627
                    finally
1628
                    {
1629
                        connection.Dispose();
1630
                    }
1631
                }
1632
            }
1633
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1634
            {
1635
                using (SqlConnection connection = GetSqlConnection())
1636
                {
1637
                    try
1638
                    {
1639
                        if (connection != null && connection.State == ConnectionState.Open)
1640
                        {
1641
                            using (SqlCommand cmd = connection.CreateCommand())
1642
                            {
1643
                                cmd.CommandText = string.Format(@"
1644
                            SELECT s.UID, s.ShowType, s.Name, s.DisplayName, s.TableName, s.DataType, s.Value, s.Cond1_Name, s.Cond1_DisplayName, s.Cond1_TableName, s.Cond1_DataType, s.Cond1_Value, s.Cond2_Name, s.Cond2_DisplayName, s.Cond2_TableName, s.Cond2_DataType, s.Cond2_Value, s.SortOrder
1645
                              FROM {0} as s
1646
                              ORDER BY s.SortOrder ASC;", SPPID_PIPERUN_PROP_SETTING);
1647
                                using (SqlDataReader dr = cmd.ExecuteReader())
1648
                                    dt.Load(dr);
1649
                            }
1650
                            connection.Close();
1651
                        }
1652
                    }
1653
                    catch (Exception ex)
1654
                    {
1655
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1656
                    }
1657
                    finally
1658
                    {
1659
                        if (connection != null)
1660
                            connection.Dispose();
1661
                    }
1662
                }
1663
            }
1664

    
1665
            return dt;
1666
        }
1667

    
1668
        public static DataTable SelectMappedPipeProperties()
1669
        {
1670
            DataTable dt = new DataTable();
1671
            Project_Info projectInfo = Project_Info.GetInstance();
1672
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1673
            {
1674
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1675
                {
1676
                    try
1677
                    {
1678
                        connection.Open();
1679
                        using (SQLiteCommand cmd = connection.CreateCommand())
1680
                        {
1681
                            cmd.CommandText = string.Format(@"
1682
                            SELECT DISTINCT sam.SPPID_ATTRIBUTE
1683
                              FROM {0} sam
1684
                              LEFT JOIN {1} sa 
1685
                              ON sam.UID = sa.UID 
1686
                              LEFT JOIN {2} st
1687
                              ON sa.SymbolType_UID = st.UID 
1688
                              LEFT JOIN {3} as lp
1689
                              ON sam.UID = lp.UID 
1690
                            WHERE sam.SPPID_ATTRIBUTE is not null 
1691
                            AND (st.Category = 'Line' or lp.UID is not null);", SPPID_ATTRIBUTE_MAPPING_TABLE, SymbolAttribute_TABLE, SymbolType_TABLE, LineProperties_TABLE);
1692
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1693
                                dt.Load(dr);
1694
                        }
1695
                        connection.Close();
1696
                    }
1697
                    catch (Exception ex)
1698
                    {
1699
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1700
                    }
1701
                    finally
1702
                    {
1703
                        connection.Dispose();
1704
                    }
1705
                }
1706
            }
1707
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1708
            {
1709
                using (SqlConnection connection = GetSqlConnection())
1710
                {
1711
                    try
1712
                    {
1713
                        if (connection != null && connection.State == ConnectionState.Open)
1714
                        {
1715
                            using (SqlCommand cmd = connection.CreateCommand())
1716
                            {
1717
                                cmd.CommandText = string.Format(@"
1718
                            SELECT DISTINCT sam.SPPID_ATTRIBUTE
1719
                              FROM {0} sam
1720
                              LEFT JOIN {1} sa 
1721
                              ON sam.UID = sa.UID 
1722
                              LEFT JOIN {2} st
1723
                              ON sa.SymbolType_UID = st.UID 
1724
                              LEFT JOIN {3} as lp
1725
                              ON sam.UID = lp.UID 
1726
                            WHERE sam.SPPID_ATTRIBUTE is not null 
1727
                            AND (st.Category = 'Line' or lp.UID is not null);", SPPID_ATTRIBUTE_MAPPING_TABLE, SymbolAttribute_TABLE, SymbolType_TABLE, LineProperties_TABLE);
1728
                            using (SqlDataReader dr = cmd.ExecuteReader())
1729
                                    dt.Load(dr);
1730
                            }
1731
                            connection.Close();
1732
                        }
1733
                    }
1734
                    catch (Exception ex)
1735
                    {
1736
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1737
                    }
1738
                    finally
1739
                    {
1740
                        if (connection != null)
1741
                            connection.Dispose();
1742
                    }
1743
                }
1744
            }
1745

    
1746
            DataTable dtPipeRun = SPPID_DB.GetPipeRunProperties(false, true);
1747
            DataTable resultDT = dtPipeRun.Clone();
1748
            if (dt != null)
1749
            {
1750
                List<string> filterList = new List<string>();
1751
                foreach (DataRow dr in dt.Rows)
1752
                {
1753
                    filterList.Add("'" + dr["SPPID_ATTRIBUTE"].ToString() + "'");
1754
                }
1755
                string filter = string.Join(",", filterList.ToArray());
1756
                if (!string.IsNullOrWhiteSpace(filter))
1757
                {
1758
                    DataRow[] dataRows = dtPipeRun.Select(string.Format("NAME IN ({0})", filter));
1759

    
1760
                    resultDT.Rows.Add(resultDT.NewRow());
1761
                    foreach (DataRow dr in dataRows)
1762
                    {
1763
                        resultDT.Rows.Add(dr.ItemArray);
1764
                    }
1765
                }
1766
            }
1767
            return resultDT;
1768
        }
1769

    
1770
        public static DataTable SelectSymbolType()
1771
        {
1772
            DataTable dt = new DataTable();
1773
            Project_Info projectInfo = Project_Info.GetInstance();
1774
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1775
            {
1776
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1777
                {
1778
                    try
1779
                    {
1780
                        connection.Open();
1781
                        using (SQLiteCommand cmd = connection.CreateCommand())
1782
                        {
1783
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1784
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1785
                                dt.Load(dr);
1786
                        }
1787
                        connection.Close();
1788
                    }
1789
                    catch (Exception ex)
1790
                    {
1791
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1792
                    }
1793
                    finally
1794
                    {
1795
                        connection.Dispose();
1796
                    }
1797
                }
1798
            }
1799
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1800
            {
1801
                using (SqlConnection connection = GetSqlConnection())
1802
                {
1803
                    try
1804
                    {
1805
                        if (connection != null && connection.State == ConnectionState.Open)
1806
                        {
1807
                            using (SqlCommand cmd = connection.CreateCommand())
1808
                            {
1809
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1810
                                using (SqlDataReader dr = cmd.ExecuteReader())
1811
                                    dt.Load(dr);
1812
                            }
1813
                            connection.Close();
1814
                        }
1815
                    }
1816
                    catch (Exception ex)
1817
                    {
1818
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1819
                    }
1820
                    finally
1821
                    {
1822
                        if (connection != null)
1823
                            connection.Dispose();
1824
                    }
1825
                }
1826
            }
1827

    
1828
            return dt;
1829
        }
1830

    
1831
        public static DataTable SelectDrawingInfo()
1832
        {
1833
            DataTable dt = new DataTable();
1834
            Project_Info projectInfo = Project_Info.GetInstance();
1835
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1836
            {
1837
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1838
                {
1839
                    try
1840
                    {
1841
                        connection.Open();
1842
                        using (SQLiteCommand cmd = connection.CreateCommand())
1843
                        {
1844
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1845
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1846
                                dt.Load(dr);
1847
                        }
1848
                        connection.Close();
1849
                    }
1850
                    catch (Exception ex)
1851
                    {
1852
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1853
                    }
1854
                    finally
1855
                    {
1856
                        connection.Dispose();
1857
                    }
1858
                }
1859
            }
1860
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1861
            {
1862
                using (SqlConnection connection = GetSqlConnection())
1863
                {
1864
                    try
1865
                    {
1866
                        if (connection != null && connection.State == ConnectionState.Open)
1867
                        {
1868
                            using (SqlCommand cmd = connection.CreateCommand())
1869
                            {
1870
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1871
                                using (SqlDataReader dr = cmd.ExecuteReader())
1872
                                    dt.Load(dr);
1873
                            }
1874
                            connection.Close();
1875
                        }
1876
                    }
1877
                    catch (Exception ex)
1878
                    {
1879
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1880
                    }
1881
                    finally
1882
                    {
1883
                        if (connection != null)
1884
                            connection.Dispose();
1885
                    }
1886
                }
1887
            }
1888

    
1889
            return dt;
1890
        }
1891

    
1892
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas)
1893
        {
1894
            Project_Info projectInfo = Project_Info.GetInstance();
1895
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1896
            {
1897
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1898
                {
1899
                    try
1900
                    {
1901
                        connection.Open();
1902
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1903
                        {
1904
                            try
1905
                            {
1906
                                using (SQLiteCommand cmd = connection.CreateCommand())
1907
                                {
1908
                                    foreach (var item in datas)
1909
                                    {
1910
                                        cmd.Parameters.Clear();
1911
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE);
1912
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1913
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1914
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1915
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1916
                                        cmd.ExecuteNonQuery();
1917
                                    }
1918
                                }
1919
                                transaction.Commit();
1920
                                connection.Close();
1921
                            }
1922
                            catch (Exception ex)
1923
                            {
1924
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1925
                                transaction.Rollback();
1926
                                return false;
1927
                            }
1928
                            finally
1929
                            {
1930
                                transaction.Dispose();
1931
                            }
1932
                        }
1933
                    }
1934
                    catch (Exception ex)
1935
                    {
1936
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1937
                        return false;
1938
                    }
1939
                    finally
1940
                    {
1941
                        connection.Dispose();
1942
                    }
1943
                }
1944
            }
1945
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1946
            {
1947
                using (SqlConnection connection = GetSqlConnection())
1948
                {
1949
                    try
1950
                    {
1951
                        if (connection != null && connection.State == ConnectionState.Open)
1952
                        {
1953
                            using (SqlCommand cmd = connection.CreateCommand())
1954
                            {
1955
                                foreach (var item in datas)
1956
                                {
1957
                                    cmd.Parameters.Clear();
1958
                                    cmd.CommandText = string.Format(@"
1959
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1960
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
1961
                                    ELSE
1962
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, item.Item1);
1963
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1964
                                    if (string.IsNullOrEmpty(item.Item2))
1965
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
1966
                                    else
1967
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1968
                                    if (string.IsNullOrEmpty(item.Item3))
1969
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
1970
                                    else
1971
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1972
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1973
                                    cmd.ExecuteNonQuery();
1974
                                }
1975
                            }
1976
                            connection.Close();
1977
                        }
1978
                    }
1979
                    catch (Exception ex)
1980
                    {
1981
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1982
                        return false;
1983
                    }
1984
                    finally
1985
                    {
1986
                        if (connection != null)
1987
                            connection.Dispose();
1988
                    }
1989
                }
1990
            }
1991

    
1992
            return true;
1993
        }
1994

    
1995
        public static bool InsertAttributeMapping(List<Tuple<string, string, bool>> datas)
1996
        {
1997
            Project_Info projectInfo = Project_Info.GetInstance();
1998
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1999
            {
2000
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2001
                {
2002
                    try
2003
                    {
2004
                        connection.Open();
2005
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2006
                        {
2007
                            try
2008
                            {
2009
                                using (SQLiteCommand cmd = connection.CreateCommand())
2010
                                {
2011
                                    foreach (var item in datas)
2012
                                    {
2013
                                        cmd.Parameters.Clear();
2014
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE, ISTEXT) VALUES (@UID, @SPPID_ATTRIBUTE, @ISTEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
2015
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
2016
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
2017
                                        cmd.Parameters.AddWithValue("@ISTEXT", item.Item3);
2018
                                        cmd.ExecuteNonQuery();
2019
                                    }
2020
                                }
2021
                                transaction.Commit();
2022
                                connection.Close();
2023
                            }
2024
                            catch (Exception ex)
2025
                            {
2026
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2027
                                transaction.Rollback();
2028
                            }
2029
                            finally
2030
                            {
2031
                                transaction.Dispose();
2032
                            }
2033
                        }
2034
                    }
2035
                    catch (Exception ex)
2036
                    {
2037
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2038
                        return false;
2039
                    }
2040
                    finally
2041
                    {
2042
                        connection.Dispose();
2043
                    }
2044
                }
2045
            }
2046
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2047
            {
2048
                using (SqlConnection connection = GetSqlConnection())
2049
                {
2050
                    try
2051
                    {
2052
                        if (connection != null && connection.State == ConnectionState.Open)
2053
                        {
2054
                            using (SqlCommand cmd = connection.CreateCommand())
2055
                            {
2056
                                foreach (var item in datas)
2057
                                {
2058
                                    cmd.Parameters.Clear();
2059
                                    cmd.CommandText = string.Format(@"
2060
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2061
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE, ISTEXT = @ISTEXT WHERE UID = @UID
2062
                                    ELSE
2063
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE, ISTEXT) VALUES (@UID, @SPPID_ATTRIBUTE, @ISTEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE, item.Item1);
2064

    
2065
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
2066
                                    if (string.IsNullOrEmpty(item.Item2))
2067
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
2068
                                    else
2069
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
2070
                                    cmd.Parameters.AddWithValue("@ISTEXT", item.Item3);
2071
                                    cmd.ExecuteNonQuery();
2072
                                }
2073
                            }
2074
                            connection.Close();
2075
                        }
2076
                    }
2077
                    catch (Exception ex)
2078
                    {
2079
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2080
                        return false;
2081
                    }
2082
                    finally
2083
                    {
2084
                        if (connection != null)
2085
                            connection.Dispose();
2086
                    }
2087
                }
2088
            }
2089
            return true;
2090
        }
2091

    
2092
        public static bool InsertLabelInfoMapping(List<Tuple<string, int, bool>> datas)
2093
        {
2094
            Project_Info projectInfo = Project_Info.GetInstance();
2095
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2096
            {
2097
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2098
                {
2099
                    try
2100
                    {
2101
                        connection.Open();
2102
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2103
                        {
2104
                            try
2105
                            {
2106
                                using (SQLiteCommand cmd = connection.CreateCommand())
2107
                                {
2108
                                    foreach (var item in datas)
2109
                                    {
2110
                                        cmd.Parameters.Clear();
2111
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE);
2112
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
2113
                                        cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
2114
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
2115
                                        cmd.ExecuteNonQuery();
2116
                                    }
2117
                                }
2118
                                transaction.Commit();
2119
                                connection.Close();
2120
                            }
2121
                            catch (Exception ex)
2122
                            {
2123
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2124
                                transaction.Rollback();
2125
                                return false;
2126
                            }
2127
                            finally
2128
                            {
2129
                                transaction.Dispose();
2130
                            }
2131
                        }
2132
                    }
2133
                    catch (Exception ex)
2134
                    {
2135
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2136
                        return false;
2137
                    }
2138
                    finally
2139
                    {
2140
                        connection.Dispose();
2141
                    }
2142
                }
2143
            }
2144
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2145
            {
2146
                using (SqlConnection connection = GetSqlConnection())
2147
                {
2148
                    try
2149
                    {
2150
                        if (connection != null && connection.State == ConnectionState.Open)
2151
                        {
2152
                            using (SqlCommand cmd = connection.CreateCommand())
2153
                            {
2154
                                foreach (var item in datas)
2155
                                {
2156
                                    cmd.Parameters.Clear();
2157
                                    cmd.CommandText = string.Format(@"
2158
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2159
                                        UPDATE {0} SET LOCATION = @LOCATION, LEADERLINE = @LEADERLINE WHERE UID = @UID
2160
                                    ELSE
2161
                                        INSERT INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE, item.Item1);
2162

    
2163
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
2164
                                    cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
2165
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
2166
                                    cmd.ExecuteNonQuery();
2167
                                }
2168
                            }
2169
                            connection.Close();
2170
                        }
2171
                    }
2172
                    catch (Exception ex)
2173
                    {
2174
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2175
                        return false;
2176
                    }
2177
                    finally
2178
                    {
2179
                        if (connection != null)
2180
                            connection.Dispose();
2181
                    }
2182
                }
2183
            }
2184
            return true;
2185
        }
2186

    
2187
        public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document)
2188
        {
2189
            Project_Info projectInfo = Project_Info.GetInstance();
2190
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2191
            {
2192
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2193
                {
2194
                    try
2195
                    {
2196
                        connection.Open();
2197
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2198
                        {
2199
                            try
2200
                            {
2201
                                using (SQLiteCommand cmd = connection.CreateCommand())
2202
                                {
2203
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO);
2204
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
2205
                                    cmd.Parameters.AddWithValue("@PATH", path);
2206
                                    cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
2207
                                    cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
2208
                                    cmd.ExecuteNonQuery();
2209
                                }
2210

    
2211
                                List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
2212
                                foreach (var item in OPCs)
2213
                                {
2214
                                    using (SQLiteCommand cmd = connection.CreateCommand())
2215
                                    {
2216
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, ATTRIBUTES, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @ATTRIBUTES, @PAIRED)", SPPID_OPC_INFO);
2217
                                        cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
2218
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
2219
                                        cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
2220
                                        cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
2221
                                        cmd.Parameters.AddWithValue("@PAIRED", false);
2222
                                        cmd.ExecuteNonQuery();
2223
                                    }
2224
                                }
2225

    
2226
                                transaction.Commit();
2227
                                connection.Close();
2228
                            }
2229
                            catch (Exception ex)
2230
                            {
2231
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2232
                                transaction.Rollback();
2233
                                return false;
2234
                            }
2235
                            finally
2236
                            {
2237
                                transaction.Dispose();
2238
                            }
2239
                        }
2240
                    }
2241
                    catch (Exception ex)
2242
                    {
2243
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2244
                        return false;
2245
                    }
2246
                    finally
2247
                    {
2248
                        connection.Dispose();
2249
                    }
2250
                }
2251
            }
2252
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2253
            {
2254
                using (SqlConnection connection = GetSqlConnection())
2255
                {
2256
                    try
2257
                    {
2258
                        if (connection != null && connection.State == ConnectionState.Open)
2259
                        {
2260
                            using (SqlCommand cmd = connection.CreateCommand())
2261
                            {
2262
                                cmd.Parameters.Clear();
2263
                                cmd.CommandText = string.Format(@"
2264
                                    IF EXISTS (SELECT * FROM {0} WHERE ID2_DRAWING_UID = '{1}')
2265
                                        UPDATE {0} SET PATH = @PATH, DRAWINGNUMBER = @DRAWINGNUMBER, DRAWINGNAME = @DRAWINGNAME WHERE ID2_DRAWING_UID = @ID2_DRAWING_UID
2266
                                    ELSE
2267
                                        INSERT INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO, document.UID);
2268

    
2269
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
2270
                                cmd.Parameters.AddWithValue("@PATH", path);
2271
                                cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
2272
                                cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
2273
                                cmd.ExecuteNonQuery();
2274
                            }
2275

    
2276
                            List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
2277
                            foreach (var item in OPCs)
2278
                            {
2279
                                using (SqlCommand cmd = connection.CreateCommand())
2280
                                {
2281
                                    cmd.Parameters.Clear();
2282
                                    cmd.CommandText = string.Format(@"
2283
                                    IF EXISTS (SELECT * FROM {0} WHERE ID2_OPC_UID = '{1}')
2284
                                        UPDATE {0} SET SPPID_OPC_MODELITEM_ID = @SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID = @ID2_DRAWING_UID, ATTRIBUTES = @ATTRIBUTES, PAIRED = @PAIRED WHERE ID2_OPC_UID = @ID2_OPC_UID
2285
                                    ELSE
2286
                                        INSERT INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, ATTRIBUTES, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @ATTRIBUTES, @PAIRED)", SPPID_OPC_INFO, item.UID);
2287

    
2288
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
2289
                                    if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
2290
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value);
2291
                                    else
2292
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
2293
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
2294
                                    cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
2295
                                    cmd.Parameters.AddWithValue("@PAIRED", false);
2296
                                    cmd.ExecuteNonQuery();
2297
                                }
2298
                            }
2299
                            connection.Close();
2300
                        }
2301
                    }
2302
                    catch (Exception ex)
2303
                    {
2304
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2305
                        return false;
2306
                    }
2307
                    finally
2308
                    {
2309
                        if (connection != null)
2310
                            connection.Dispose();
2311
                    }
2312
                }
2313
            }
2314
            return true;
2315
        }
2316

    
2317
        public static bool InsertLineNumberInfo(string path, string drawingNumber, string drawingName, SPPID_Document document)
2318
        {
2319
            Project_Info projectInfo = Project_Info.GetInstance();
2320
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2321
            {
2322
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2323
                {
2324
                    try
2325
                    {
2326
                        connection.Open();
2327
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2328
                        {
2329
                            try
2330
                            {
2331
                                using (SQLiteCommand cmd = connection.CreateCommand())
2332
                                {
2333
                                    foreach (var lineNumber in document.LINENUMBERS)
2334
                                    {
2335
                                        foreach (var attribute in lineNumber.ATTRIBUTES)
2336
                                        {
2337
                                            LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2338
                                            if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2339
                                            {
2340
                                                if (mapping.SPPIDATTRIBUTENAME == "PlantGroup.Name")
2341
                                                {
2342
                                                    cmd.Parameters.Clear();
2343

    
2344

    
2345

    
2346
                                                    cmd.ExecuteNonQuery();
2347
                                                }
2348
                                            }
2349
                                        }
2350
                                    }
2351
                                }
2352

    
2353
                                transaction.Commit();
2354
                                connection.Close();
2355
                            }
2356
                            catch (Exception ex)
2357
                            {
2358
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2359
                                transaction.Rollback();
2360
                                return false;
2361
                            }
2362
                            finally
2363
                            {
2364
                                transaction.Dispose();
2365
                            }
2366
                        }
2367
                    }
2368
                    catch (Exception ex)
2369
                    {
2370
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2371
                        return false;
2372
                    }
2373
                    finally
2374
                    {
2375
                        connection.Dispose();
2376
                    }
2377
                }
2378
            }
2379
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2380
            {
2381
                using (SqlConnection connection = GetSqlConnection())
2382
                {
2383
                    try
2384
                    {
2385
                        if (connection != null && connection.State == ConnectionState.Open)
2386
                        {
2387
                            using (SqlCommand cmd = connection.CreateCommand())
2388
                            {
2389
                                foreach (var lineNumber in document.LINENUMBERS)
2390
                                {
2391
                                    foreach (var attribute in lineNumber.ATTRIBUTES)
2392
                                    {
2393
                                        LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2394
                                        if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2395
                                        {
2396
                                            if (mapping.SPPIDATTRIBUTENAME == "PlantGroup.Name")
2397
                                            {
2398
                                                cmd.Parameters.Clear();
2399

    
2400
                                                cmd.ExecuteNonQuery();
2401
                                            }
2402
                                        }
2403
                                    }
2404
                                }
2405
                            }
2406
                            connection.Close();
2407
                        }
2408
                    }
2409
                    catch (Exception ex)
2410
                    {
2411
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2412
                        return false;
2413
                    }
2414
                    finally
2415
                    {
2416
                        if (connection != null)
2417
                            connection.Dispose();
2418
                    }
2419
                }
2420
            }
2421
            return true;
2422
        }
2423
        public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired)
2424
        {
2425
            Project_Info projectInfo = Project_Info.GetInstance();
2426
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2427
            {
2428
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2429
                {
2430
                    try
2431
                    {
2432
                        connection.Open();
2433
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2434
                        {
2435
                            try
2436
                            {
2437
                                using (SQLiteCommand cmd = connection.CreateCommand())
2438
                                {
2439
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @PAIRED)", SPPID_OPC_INFO);
2440
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
2441
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
2442
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
2443
                                    cmd.Parameters.AddWithValue("@PAIRED", Paired);
2444
                                    cmd.ExecuteNonQuery();
2445
                                }
2446

    
2447
                                transaction.Commit();
2448
                                connection.Close();
2449
                            }
2450
                            catch (Exception ex)
2451
                            {
2452
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2453
                                transaction.Rollback();
2454
                                return false;
2455
                            }
2456
                            finally
2457
                            {
2458
                                transaction.Dispose();
2459
                            }
2460
                        }
2461
                    }
2462
                    catch (Exception ex)
2463
                    {
2464
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2465
                        return false;
2466
                    }
2467
                    finally
2468
                    {
2469
                        connection.Dispose();
2470
                    }
2471
                }
2472
            }
2473
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2474
            {
2475
                using (SqlConnection connection = GetSqlConnection())
2476
                {
2477
                    try
2478
                    {
2479
                        if (connection != null && connection.State == ConnectionState.Open)
2480
                        {
2481
                            using (SqlCommand cmd = connection.CreateCommand())
2482
                            {
2483
                                cmd.Parameters.Clear();
2484
                                cmd.CommandText = string.Format(@"
2485
                                    UPDATE {0} SET SPPID_OPC_MODELITEM_ID = @SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID = @ID2_DRAWING_UID, PAIRED = @PAIRED WHERE ID2_OPC_UID = @ID2_OPC_UID", SPPID_OPC_INFO);
2486
                                cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
2487
                                if (string.IsNullOrEmpty(ModelItemID))
2488
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value);
2489
                                else
2490
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
2491
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
2492
                                cmd.Parameters.AddWithValue("@PAIRED", Paired);
2493
                                cmd.ExecuteNonQuery();
2494
                            }
2495
                            connection.Close();
2496
                        }
2497
                    }
2498
                    catch (Exception ex)
2499
                    {
2500
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2501
                        return false;
2502
                    }
2503
                    finally
2504
                    {
2505
                        if (connection != null)
2506
                            connection.Dispose();
2507
                    }
2508
                }
2509
            }
2510
            return true;
2511
        }
2512

    
2513
        public static bool ExportMappingData()
2514
        {
2515
            bool result = true;
2516
            try
2517
            {
2518
                DataSet dataSet = new DataSet();
2519
                DataTable symbolMappingDT = GetTable(SPPID_SYMBOL_MAPPING_TABLE);
2520
                if (symbolMappingDT != null)
2521
                {
2522
                    symbolMappingDT.TableName = SPPID_SYMBOL_MAPPING_TABLE;
2523
                    dataSet.Tables.Add(symbolMappingDT);
2524
                }
2525
                else
2526
                    result = false;
2527

    
2528
                DataTable attributeMappingDT = GetTable(SPPID_ATTRIBUTE_MAPPING_TABLE);
2529
                if (attributeMappingDT != null)
2530
                {
2531
                    attributeMappingDT.TableName = SPPID_ATTRIBUTE_MAPPING_TABLE;
2532
                    dataSet.Tables.Add(attributeMappingDT);
2533
                }
2534
                else
2535
                    result = false;
2536

    
2537
                if (result)
2538
                {
2539
                    string text = JsonConvert.SerializeObject(dataSet);
2540
                    using (StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf", false))
2541
                    {
2542
                        sw.Write(text);
2543
                        sw.Close();
2544
                        sw.Dispose();
2545
                    }
2546
                }
2547
            }
2548
            catch (Exception ex)
2549
            {
2550
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2551
                result = false;
2552
            }
2553

    
2554
            return result;
2555
        }
2556

    
2557
        public static bool ImportMappingData()
2558
        {
2559
            bool result = true;
2560
            try
2561
            {
2562
                string sJson = string.Empty;
2563
                using (StreamReader sw = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf"))
2564
                {
2565
                    sJson = sw.ReadToEnd();
2566
                    sw.Close();
2567
                    sw.Dispose();
2568
                }
2569

    
2570
                DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(sJson);
2571
                DataTable symbolMappingDT = dataSet.Tables[SPPID_SYMBOL_MAPPING_TABLE];
2572
                if (symbolMappingDT == null || !ImportSymbolMappingTable(symbolMappingDT))
2573
                    result = false;
2574

    
2575
                DataTable attributeMappingDT = dataSet.Tables[SPPID_ATTRIBUTE_MAPPING_TABLE];
2576
                if (attributeMappingDT == null || !ImportAttributeMappingTable(attributeMappingDT))
2577
                    result = false;
2578
            }
2579
            catch (Exception ex)
2580
            {
2581
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2582
                result = false;
2583
            }
2584

    
2585
            return result;
2586
        }
2587

    
2588
        private static DataTable GetTable(string tableName)
2589
        {
2590
            DataTable dt = new DataTable();
2591
            Project_Info projectInfo = Project_Info.GetInstance();
2592
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2593
            {
2594
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2595
                {
2596
                    try
2597
                    {
2598
                        connection.Open();
2599
                        using (SQLiteCommand cmd = connection.CreateCommand())
2600
                        {
2601
                            cmd.CommandText = string.Format("SELECT * FROM {0}", tableName);
2602
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
2603
                                dt.Load(dr);
2604
                        }
2605
                        connection.Close();
2606
                    }
2607
                    catch (Exception ex)
2608
                    {
2609
                        dt = null;
2610
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2611
                    }
2612
                    finally
2613
                    {
2614
                        connection.Dispose();
2615
                    }
2616
                }
2617
            }
2618
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2619
            {
2620
                using (SqlConnection connection = GetSqlConnection())
2621
                {
2622
                    try
2623
                    {
2624
                        if (connection != null && connection.State == ConnectionState.Open)
2625
                        {
2626
                            using (SqlCommand cmd = connection.CreateCommand())
2627
                            {
2628
                                cmd.CommandText = string.Format("SELECT * FROM {0}", tableName);
2629
                                using (SqlDataReader dr = cmd.ExecuteReader())
2630
                                    dt.Load(dr);
2631
                            }
2632
                            connection.Close();
2633
                        }
2634
                    }
2635
                    catch (Exception ex)
2636
                    {
2637
                        dt = null;
2638
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2639
                    }
2640
                    finally
2641
                    {
2642
                        if (connection != null)
2643
                            connection.Dispose();
2644
                    }
2645
                }
2646
            }
2647

    
2648
            return dt;
2649
        }
2650

    
2651
        private static bool ImportSymbolMappingTable(DataTable dt)
2652
        {
2653
            bool result = false;
2654

    
2655
            Project_Info projectInfo = Project_Info.GetInstance();
2656
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2657
            {
2658
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2659
                {
2660
                    try
2661
                    {
2662
                        connection.Open();
2663
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2664
                        {
2665
                            try
2666
                            {
2667
                                using (SQLiteCommand cmd = connection.CreateCommand())
2668
                                {
2669
                                    foreach (DataRow item in dt.Rows)
2670
                                    {
2671
                                        string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2672
                                        string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"];
2673
                                        string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"];
2674
                                        bool LEADERLINE = Convert.ToBoolean(item["LEADERLINE"]);
2675

    
2676
                                        cmd.Parameters.Clear();
2677
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE);
2678
                                        cmd.Parameters.AddWithValue("@UID", UID);
2679
                                        cmd.Parameters.AddWithValue("@NAME", NAME);
2680
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH);
2681
                                        cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE);
2682
                                        cmd.ExecuteNonQuery();
2683
                                    }
2684
                                }
2685
                                transaction.Commit();
2686
                                connection.Close();
2687
                                result = true;
2688
                            }
2689
                            catch (Exception ex)
2690
                            {
2691
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2692
                                transaction.Rollback();
2693
                            }
2694
                            finally
2695
                            {
2696
                                transaction.Dispose();
2697
                            }
2698
                        }
2699
                    }
2700
                    catch (Exception ex)
2701
                    {
2702
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2703
                    }
2704
                    finally
2705
                    {
2706
                        connection.Dispose();
2707
                    }
2708
                }
2709
            }
2710
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2711
            {
2712
                using (SqlConnection connection = GetSqlConnection())
2713
                {
2714
                    try
2715
                    {
2716
                        if (connection != null && connection.State == ConnectionState.Open)
2717
                        {
2718
                            using (SqlCommand cmd = connection.CreateCommand())
2719
                            {
2720
                                foreach (DataRow item in dt.Rows)
2721
                                {
2722
                                    string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2723
                                    string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"];
2724
                                    string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"];
2725
                                    bool LEADERLINE = DBNull.Value.Equals(item["LEADERLINE"]) ? false : Convert.ToBoolean(item["LEADERLINE"]);
2726

    
2727
                                    cmd.Parameters.Clear();
2728
                                    cmd.CommandText = string.Format(@"
2729
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2730
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
2731
                                    ELSE
2732
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, UID);
2733
                                    cmd.Parameters.AddWithValue("@UID", UID);
2734
                                    if (string.IsNullOrEmpty(NAME))
2735
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
2736
                                    else
2737
                                        cmd.Parameters.AddWithValue("@NAME", NAME);
2738
                                    if (string.IsNullOrEmpty(SPPID_SYMBOL_PATH))
2739
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
2740
                                    else
2741
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH);
2742
                                    cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE);
2743
                                    cmd.ExecuteNonQuery();
2744
                                }
2745
                            }
2746
                            connection.Close();
2747
                            result = true;
2748
                        }
2749
                    }
2750
                    catch (Exception ex)
2751
                    {
2752
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2753
                    }
2754
                    finally
2755
                    {
2756
                        if (connection != null)
2757
                            connection.Dispose();
2758
                    }
2759
                }
2760
            }
2761

    
2762
            return result;
2763
        }
2764

    
2765
        private static bool ImportAttributeMappingTable(DataTable dt)
2766
        {
2767
            bool result = false;
2768

    
2769
            Project_Info projectInfo = Project_Info.GetInstance();
2770
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2771
            {
2772
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2773
                {
2774
                    try
2775
                    {
2776
                        connection.Open();
2777
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2778
                        {
2779
                            try
2780
                            {
2781
                                using (SQLiteCommand cmd = connection.CreateCommand())
2782
                                {
2783
                                    foreach (DataRow item in dt.Rows)
2784
                                    {
2785
                                        string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2786
                                        string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"];
2787

    
2788
                                        cmd.Parameters.Clear();
2789
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
2790
                                        cmd.Parameters.AddWithValue("@UID", UID);
2791
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE);
2792
                                        cmd.ExecuteNonQuery();
2793
                                    }
2794
                                }
2795
                                transaction.Commit();
2796
                                connection.Close();
2797
                                result = true;
2798
                            }
2799
                            catch (Exception ex)
2800
                            {
2801
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2802
                                transaction.Rollback();
2803
                            }
2804
                            finally
2805
                            {
2806
                                transaction.Dispose();
2807
                            }
2808
                        }
2809
                    }
2810
                    catch (Exception ex)
2811
                    {
2812
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2813
                    }
2814
                    finally
2815
                    {
2816
                        connection.Dispose();
2817
                    }
2818
                }
2819
            }
2820
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2821
            {
2822
                using (SqlConnection connection = GetSqlConnection())
2823
                {
2824
                    try
2825
                    {
2826
                        if (connection != null && connection.State == ConnectionState.Open)
2827
                        {
2828
                            using (SqlCommand cmd = connection.CreateCommand())
2829
                            {
2830
                                foreach (DataRow item in dt.Rows)
2831
                                {
2832
                                    string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2833
                                    string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"];
2834

    
2835
                                    cmd.Parameters.Clear();
2836
                                    cmd.CommandText = string.Format(@"
2837
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2838
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE WHERE UID = @UID
2839
                                    ELSE
2840
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE, UID);
2841

    
2842
                                    cmd.Parameters.AddWithValue("@UID", UID);
2843
                                    if (string.IsNullOrEmpty(SPPID_ATTRIBUTE))
2844
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
2845
                                    else
2846
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE);
2847
                                    cmd.ExecuteNonQuery();
2848
                                }
2849
                            }
2850
                            connection.Close();
2851
                            result = true;
2852
                        }
2853
                    }
2854
                    catch (Exception ex)
2855
                    {
2856
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2857
                    }
2858
                    finally
2859
                    {
2860
                        if (connection != null)
2861
                            connection.Dispose();
2862
                    }
2863
                }
2864
            }
2865

    
2866

    
2867

    
2868
            return result;
2869
        }
2870
    }
2871
}
클립보드 이미지 추가 (최대 크기: 500 MB)