프로젝트

일반

사용자정보

통계
| 개정판:

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

이력 | 보기 | 이력해설 | 다운로드 (119 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

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

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

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

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

    
61
            return connection;
62
        }
63

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

    
85
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
86
                                    {
87
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE);
88
                                        cmd.ExecuteNonQuery();
89
                                    }
90
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
91
                                    {
92
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT, SettingType TEXT)", SPPID_SETTING_TABLE);
93
                                        cmd.ExecuteNonQuery();
94
                                    }
95
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
96
                                    {
97
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, SPPID_SYMBOL_PATH TEXT, LEADERLINE BOOLEAN)", SPPID_SYMBOL_MAPPING_TABLE);
98
                                        cmd.ExecuteNonQuery();
99
                                    }
100
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
101
                                    {
102
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, SPPID_ATTRIBUTE TEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
103
                                        cmd.ExecuteNonQuery();
104
                                    }
105
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
106
                                    {
107
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BOOLEAN)", SPPID_LABEL_INFO_TABLE);
108
                                        cmd.ExecuteNonQuery();
109
                                    }
110
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
111
                                    {
112
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
113
                                        cmd.ExecuteNonQuery();
114
                                    }
115
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
116
                                    {
117
                                        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);
118
                                        cmd.ExecuteNonQuery();
119
                                    }
120
                                    if (dt.Select(string.Format("NAME = '{0}'", ID2_DRAWINGATTRIBUTE_TABLE)).Length == 0)
121
                                    {
122
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT)", ID2_DRAWINGATTRIBUTE_TABLE);
123
                                        cmd.ExecuteNonQuery();
124

    
125
                                        List<string> datas = new List<string>() { "Unit", "Rev No", "Drawing No" };
126
                                        foreach (var data in datas)
127
                                        {
128
                                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES('{1}', '{1}')", ID2_DRAWINGATTRIBUTE_TABLE, data);
129
                                            cmd.ExecuteNonQuery();
130
                                        }
131
                                    }
132
                                }
133

    
134
                                #region Check Column 업데이트시 예비용
135
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_ATTRIBUTE_MAPPING_TABLE);
136
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
137
                                using (DataTable dt = new DataTable())
138
                                {
139
                                    dt.Load(dr);
140
                                    if (!dt.Columns.Contains("ISTEXT"))
141
                                    {
142
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN ISTEXT BOOLEAN", SPPID_ATTRIBUTE_MAPPING_TABLE);
143
                                        cmd.ExecuteNonQuery();
144
                                    }
145
                                }
146

    
147
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
148
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
149
                                using (DataTable dt = new DataTable())
150
                                {
151
                                    dt.Load(dr);
152
                                    if (!dt.Columns.Contains("LEADERLINE"))
153
                                    {
154
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN LEADERLINE BOOLEAN", SPPID_SYMBOL_MAPPING_TABLE);
155
                                        cmd.ExecuteNonQuery();
156
                                    }
157
                                }
158

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

    
171
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
172
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
173
                                using (DataTable dt = new DataTable())
174
                                {
175
                                    dt.Load(dr);
176
                                    if (dt.Columns.Contains("DOCUMENT"))
177
                                    {
178
                                        cmd.CommandText = string.Format("DROP TABLE {0}", SPPID_DRAWING_INFO);
179
                                        cmd.ExecuteNonQuery();
180

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

    
217
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
218
                                    {
219
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX))", SPPID_DB_INFO_TABLE);
220
                                        cmd.ExecuteNonQuery();
221
                                    }
222
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
223
                                    {
224
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX), SettingType varchar(MAX))", SPPID_SETTING_TABLE);
225
                                        cmd.ExecuteNonQuery();
226
                                    }
227
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
228
                                    {
229
                                        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);
230
                                        cmd.ExecuteNonQuery();
231
                                    }
232
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
233
                                    {
234
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, SPPID_ATTRIBUTE varchar(MAX))", SPPID_ATTRIBUTE_MAPPING_TABLE);
235
                                        cmd.ExecuteNonQuery();
236
                                    }
237
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
238
                                    {
239
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BIT)", SPPID_LABEL_INFO_TABLE);
240
                                        cmd.ExecuteNonQuery();
241
                                    }
242
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
243
                                    {
244
                                        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);
245
                                        cmd.ExecuteNonQuery();
246
                                    }
247
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
248
                                    {
249
                                        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);
250
                                        cmd.ExecuteNonQuery();
251
                                    }
252
                                    if (dt.Select(string.Format("NAME = '{0}'", ID2_DRAWINGATTRIBUTE_TABLE)).Length == 0)
253
                                    {
254
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(255))", ID2_DRAWINGATTRIBUTE_TABLE);
255
                                        cmd.ExecuteNonQuery();
256

    
257
                                        List<string> datas = new List<string>() { "Unit", "Rev No", "Drawing No" };
258
                                        foreach (var data in datas)
259
                                        {
260
                                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES('{1}', '{1}')", ID2_DRAWINGATTRIBUTE_TABLE, data);
261
                                            cmd.ExecuteNonQuery();
262
                                        }
263
                                    }
264
                                }
265

    
266
                                #region Check Column 업데이트시 예비용
267
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_ATTRIBUTE_MAPPING_TABLE);
268
                                using (SqlDataReader dr = cmd.ExecuteReader())
269
                                using (DataTable dt = new DataTable())
270
                                {
271
                                    dt.Load(dr);
272
                                    if (!dt.Columns.Contains("ISTEXT"))
273
                                    {
274
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD ISTEXT BIT", SPPID_ATTRIBUTE_MAPPING_TABLE);
275
                                        cmd.ExecuteNonQuery();
276
                                    }
277
                                }
278
                                #endregion
279
                            }
280
                            result = true;
281
                        }
282
                    }
283
                    catch (Exception ex)
284
                    {
285
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
286
                    }
287
                    finally
288
                    {
289
                        if (connection != null)
290
                            connection.Dispose();
291
                    }
292
                }
293
            }
294

    
295
            return result;
296
        }
297

    
298
        public static bool SaveSPPID_DB_INFO(string jsonString)
299
        {
300
            Project_Info projectInfo = Project_Info.GetInstance();
301
            if (projectInfo.DBType == ID2DB_Type.SQLite)
302
            {
303
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
304
                {
305

    
306
                    try
307
                    {
308
                        connection.Open();
309
                        using (SQLiteCommand cmd = connection.CreateCommand())
310
                        {
311
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
312
                            cmd.ExecuteNonQuery();
313

    
314
                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
315
                            cmd.Parameters.AddWithValue("@jsonString", jsonString);
316
                            cmd.ExecuteNonQuery();
317
                        }
318
                        connection.Close();
319
                    }
320
                    catch (Exception ex)
321
                    {
322
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
323
                        return false;
324
                    }
325
                    finally
326
                    {
327
                        connection.Dispose();
328
                    }
329
                }
330
            }
331
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
332
            {
333
                using (SqlConnection connection = GetSqlConnection())
334
                {
335
                    try
336
                    {
337
                        if (connection != null && connection.State == ConnectionState.Open)
338
                        {
339
                            using (SqlCommand cmd = connection.CreateCommand())
340
                            {
341
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
342
                                cmd.ExecuteNonQuery();
343

    
344
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
345
                                cmd.Parameters.AddWithValue("@jsonString", jsonString);
346
                                cmd.ExecuteNonQuery();
347
                            }
348
                            connection.Close();
349
                        }
350
                        else
351
                        {
352
                            return false;
353
                        }
354
                    }
355
                    catch (Exception ex)
356
                    {
357
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
358
                        return false;
359
                    }
360
                    finally
361
                    {
362
                        if (connection != null)
363
                            connection.Dispose();
364
                    }
365
                }
366
            }
367

    
368

    
369
            return true;
370
        }
371

    
372
        public static DataTable SelectSPPID_DB_INFO()
373
        {
374
            DataTable dt = new DataTable();
375
            Project_Info projectInfo = Project_Info.GetInstance();
376
            if (projectInfo.DBType == ID2DB_Type.SQLite)
377
            {
378
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
379
                {
380
                    try
381
                    {
382
                        connection.Open();
383
                        using (SQLiteCommand cmd = connection.CreateCommand())
384
                        {
385
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
386
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
387
                                dt.Load(dr);
388
                        }
389
                        connection.Close();
390
                    }
391
                    catch (Exception ex)
392
                    {
393
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
394
                    }
395
                    finally
396
                    {
397
                        connection.Dispose();
398
                    }
399
                }
400
            }
401
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
402
            {
403
                using (SqlConnection connection = GetSqlConnection())
404
                {
405
                    try
406
                    {
407
                        if (connection != null && connection.State == ConnectionState.Open)
408
                        {
409
                            using (SqlCommand cmd = connection.CreateCommand())
410
                            {
411
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
412
                                using (SqlDataReader dr = cmd.ExecuteReader())
413
                                    dt.Load(dr);
414
                            }
415
                            connection.Close();
416
                        }
417
                    }
418
                    catch (Exception ex)
419
                    {
420
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
421
                    }
422
                    finally
423
                    {
424
                        if (connection != null)
425
                            connection.Dispose();
426
                    }
427
                }
428
            }
429

    
430
            return dt;
431
        }
432

    
433
        public static bool SaveETCSetting(Dictionary<string,string> dicSetting)
434
        {
435
            Project_Info projectInfo = Project_Info.GetInstance();
436
            if (projectInfo.DBType == ID2DB_Type.SQLite)
437
            {
438
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
439
                {
440

    
441
                    try
442
                    {
443
                        connection.Open();
444
                        using (SQLiteCommand cmd = connection.CreateCommand())
445
                        {
446
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
447
                            cmd.ExecuteNonQuery();
448

    
449
                            foreach (var item in dicSetting)
450
                            {
451
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
452
                                cmd.Parameters.Clear();
453
                                cmd.Parameters.AddWithValue("@jsonString", item.Value);
454
                                cmd.Parameters.AddWithValue("@SettingType", item.Key);
455
                                cmd.ExecuteNonQuery();
456
                            }
457
                        }
458
                        connection.Close();
459
                    }
460
                    catch (Exception ex)
461
                    {
462
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
463
                        return false;
464
                    }
465
                    finally
466
                    {
467
                        connection.Dispose();
468
                    }
469
                }
470
            }
471
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
472
            {
473
                using (SqlConnection connection = GetSqlConnection())
474
                {
475
                    try
476
                    {
477
                        if (connection != null && connection.State == ConnectionState.Open)
478
                        {
479
                            using (SqlCommand cmd = connection.CreateCommand())
480
                            {
481
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
482
                                cmd.ExecuteNonQuery();
483

    
484
                                foreach (var item in dicSetting)
485
                                {
486
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
487
                                    cmd.Parameters.Clear();
488
                                    cmd.Parameters.AddWithValue("@jsonString", item.Value);
489
                                    cmd.Parameters.AddWithValue("@SettingType", item.Key);
490
                                    cmd.ExecuteNonQuery();
491
                                }
492
                            }
493
                            connection.Close();
494
                        }
495
                        else
496
                            return false;
497
                    }
498
                    catch (Exception ex)
499
                    {
500
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
501
                        return false;
502
                    }
503
                    finally
504
                    {
505
                        if (connection != null)
506
                            connection.Dispose();
507
                    }
508
                }
509
            }
510

    
511
            return true;
512
        }
513

    
514
        public static DataTable SelectSetting()
515
        {
516
            DataTable dt = new DataTable();
517
            Project_Info projectInfo = Project_Info.GetInstance();
518
            if (projectInfo.DBType == ID2DB_Type.SQLite)
519
            {
520
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
521
                {
522
                    try
523
                    {
524
                        connection.Open();
525
                        using (SQLiteCommand cmd = connection.CreateCommand())
526
                        {
527
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
528
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
529
                                dt.Load(dr);
530
                        }
531
                        connection.Close();
532
                    }
533
                    catch (Exception ex)
534
                    {
535
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
536
                    }
537
                    finally
538
                    {
539
                        connection.Dispose();
540
                    }
541
                }
542
            }
543
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
544
            {
545
                using (SqlConnection connection = GetSqlConnection())
546
                {
547
                    try
548
                    {
549
                        if (connection != null && connection.State == ConnectionState.Open)
550
                        {
551
                            using (SqlCommand cmd = connection.CreateCommand())
552
                            {
553
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
554
                                using (SqlDataReader dr = cmd.ExecuteReader())
555
                                    dt.Load(dr);
556
                            }
557
                            connection.Close();
558
                        }
559
                    }
560
                    catch (Exception ex)
561
                    {
562
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
563
                    }
564
                    finally
565
                    {
566
                        if (connection != null)
567
                            connection.Dispose();
568
                    }
569
                }
570
            }
571

    
572
            return dt;
573
        }
574

    
575
        public static DataTable SelectProjectSymbol()
576
        {
577
            DataTable dt = new DataTable();
578
            Project_Info projectInfo = Project_Info.GetInstance();
579
            if (projectInfo.DBType == ID2DB_Type.SQLite)
580
            {
581
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
582
                {
583
                    try
584
                    {
585
                        connection.Open();
586
                        using (SQLiteCommand cmd = connection.CreateCommand())
587
                        {
588
                            cmd.CommandText = string.Format(@"
589
                            SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
590
                                LEFT OUTER JOIN {2} as sp 
591
                                    ON s.UID = SP.UID 
592
                            WHERE s.SymbolType_UID = st.UID 
593
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
594
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
595
                                dt.Load(dr);
596

    
597
                            DataTable dtClone = dt.Clone();
598
                            dtClone.Columns["UID"].DataType = typeof(string);
599
                            foreach (DataRow row in dt.Rows)
600
                            {
601
                                dtClone.ImportRow(row);
602
                            }
603
                            dt.Dispose();
604
                            dt = dtClone;
605
                        }
606
                        connection.Close();
607
                    }
608
                    catch (Exception ex)
609
                    {
610
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
611
                    }
612
                    finally
613
                    {
614
                        connection.Dispose();
615
                    }
616
                }
617
            }
618
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
619
            {
620
                using (SqlConnection connection = GetSqlConnection())
621
                {
622
                    try
623
                    {
624
                        if (connection != null && connection.State == ConnectionState.Open)
625
                        {
626
                            using (SqlCommand cmd = connection.CreateCommand())
627
                            {
628
                                cmd.CommandText = string.Format(@"
629
                            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 
630
                                LEFT OUTER JOIN {2} as sp 
631
                                    ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID)
632
                            WHERE s.SymbolType_UID = st.UID 
633
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
634
                                using (SqlDataReader dr = cmd.ExecuteReader())
635
                                    dt.Load(dr);
636
                            }
637
                            connection.Close();
638
                        }
639
                    }
640
                    catch (Exception ex)
641
                    {
642
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
643
                    }
644
                    finally
645
                    {
646
                        if (connection != null)
647
                            connection.Dispose();
648
                    }
649
                }
650
            }
651

    
652
            return dt;
653
        }
654

    
655
        public static DataTable SelectProjectChildSymbol()
656
        {
657
            DataTable result = new DataTable();
658
            result.Columns.Add("UID");
659
            result.Columns.Add("Name");
660
            result.Columns.Add("Type");
661
            result.Columns.Add("SPPID_SYMBOL_PATH");
662
            
663
            Project_Info projectInfo = Project_Info.GetInstance();
664
            if (projectInfo.DBType == ID2DB_Type.SQLite)
665
            {
666
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
667
                using (DataTable dt = new DataTable())
668
                {
669
                    try
670
                    {
671
                        connection.Open();
672
                        using (SQLiteCommand cmd = connection.CreateCommand())
673
                        {
674
                            cmd.CommandText = string.Format(@"
675
                            SELECT AdditionalSymbol FROM Symbol");
676
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
677
                                dt.Load(dr);
678
                            List<string> childList = new List<string>();
679
                            foreach (DataRow row in dt.Rows)
680
                            {
681
                                if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
682
                                {
683
                                    string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
684
                                    foreach (var childString in array)
685
                                    {
686
                                        childList.Add(childString.Split(new char[] { ',' })[2]);
687
                                    }
688
                                }
689

    
690
                            }
691

    
692
                            dt.Clear();
693
                            cmd.Reset();
694
                            cmd.CommandText = string.Format(@"
695
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
696
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
697
                                dt.Load(dr);
698

    
699
                            childList = childList.Distinct().ToList();
700
                            foreach (var child in childList)
701
                            {
702
                                string mappingPath = string.Empty;
703
                                DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
704
                                if (rows.Length == 1)
705
                                    mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
706

    
707
                                DataRow newRow = result.NewRow();
708
                                newRow["UID"] = child;
709
                                newRow["Name"] = child;
710
                                newRow["Type"] = "Child Symbol";
711
                                newRow["SPPID_SYMBOL_PATH"] = mappingPath;
712
                                result.Rows.Add(newRow);
713
                            }
714
                        }
715
                        connection.Close();
716
                    }
717
                    catch (Exception ex)
718
                    {
719
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
720
                    }
721
                    finally
722
                    {
723
                        connection.Dispose();
724
                    }
725
                }
726
            }
727
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
728
            {
729
                using (SqlConnection connection = GetSqlConnection())
730
                using (DataTable dt = new DataTable())
731
                {
732
                    try
733
                    {
734
                        if (connection != null && connection.State == ConnectionState.Open)
735
                        {
736
                            using (SqlCommand cmd = connection.CreateCommand())
737
                            {
738
                                cmd.CommandText = string.Format(@"
739
                            SELECT AdditionalSymbol FROM Symbol");
740
                                using (SqlDataReader dr = cmd.ExecuteReader())
741
                                    dt.Load(dr);
742
                                List<string> childList = new List<string>();
743
                                foreach (DataRow row in dt.Rows)
744
                                {
745
                                    if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
746
                                    {
747
                                        string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
748
                                        foreach (var childString in array)
749
                                        {
750
                                            childList.Add(childString.Split(new char[] { ',' })[2]);
751
                                        }
752
                                    }
753

    
754
                                }
755

    
756
                                dt.Clear();
757
                                cmd.CommandText = string.Format(@"
758
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
759
                                using (SqlDataReader dr = cmd.ExecuteReader())
760
                                    dt.Load(dr);
761

    
762
                                childList = childList.Distinct().ToList();
763
                                foreach (var child in childList)
764
                                {
765
                                    string mappingPath = string.Empty;
766
                                    DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
767
                                    if (rows.Length == 1)
768
                                        mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
769

    
770
                                    DataRow newRow = result.NewRow();
771
                                    newRow["UID"] = child;
772
                                    newRow["Name"] = child;
773
                                    newRow["Type"] = "Child Symbol";
774
                                    newRow["SPPID_SYMBOL_PATH"] = mappingPath;
775
                                    result.Rows.Add(newRow);
776
                                }
777
                            }
778
                            connection.Close();
779
                        }
780
                    }
781
                    catch (Exception ex)
782
                    {
783
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
784
                    }
785
                    finally
786
                    {
787
                        if (connection != null)
788
                            connection.Dispose();
789
                    }
790
                }
791
            }
792
            return result;
793
        }
794

    
795
        public static DataTable SelectProjectLine()
796
        {
797
            DataTable dt = new DataTable();
798
            Project_Info projectInfo = Project_Info.GetInstance();
799
            if (projectInfo.DBType == ID2DB_Type.SQLite)
800
            {
801
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
802
                {
803
                    try
804
                    {
805
                        connection.Open();
806
                        using (SQLiteCommand cmd = connection.CreateCommand())
807
                        {
808
                            cmd.CommandText = string.Format(@"
809
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
810
                                LEFT OUTER JOIN {1} as sp 
811
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
812
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
813
                                dt.Load(dr);
814
                        }
815
                        connection.Close();
816
                    }
817
                    catch (Exception ex)
818
                    {
819
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
820
                    }
821
                    finally
822
                    {
823
                        connection.Dispose();
824
                    }
825
                }
826
            }
827
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
828
            {
829
                using (SqlConnection connection = GetSqlConnection())
830
                {
831
                    try
832
                    {
833
                        if (connection != null && connection.State == ConnectionState.Open)
834
                        {
835
                            using (SqlCommand cmd = connection.CreateCommand())
836
                            {
837
                                cmd.CommandText = string.Format(@"
838
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
839
                                LEFT OUTER JOIN {1} as sp 
840
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
841
                                using (SqlDataReader dr = cmd.ExecuteReader())
842
                                    dt.Load(dr);
843
                            }
844
                            connection.Close();
845
                        }
846
                    }
847
                    catch (Exception ex)
848
                    {
849
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
850
                    }
851
                    finally
852
                    {
853
                        if (connection != null)
854
                            connection.Dispose();
855
                    }
856
                }
857
            }
858

    
859
            return dt;
860
        }
861

    
862
        public static DataTable SelectProjectLineProperties()
863
        {
864
            DataTable dt = new DataTable();
865
            Project_Info projectInfo = Project_Info.GetInstance();
866
            if (projectInfo.DBType == ID2DB_Type.SQLite)
867
            {
868
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
869
                {
870
                    try
871
                    {
872
                        connection.Open();
873
                        using (SQLiteCommand cmd = connection.CreateCommand())
874
                        {
875
                            cmd.CommandText = string.Format(@"
876
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
877
                            FROM {0} as lp 
878
                                 LEFT OUTER JOIN {1} as sp 
879
                                      ON lp.UID = sp.UID
880
                                 LEFT OUTER JOIN {2} as spa 
881
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
882
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
883
                                dt.Load(dr);
884
                        }
885
                        connection.Close();
886
                    }
887
                    catch (Exception ex)
888
                    {
889
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
890
                    }
891
                    finally
892
                    {
893
                        connection.Dispose();
894
                    }
895
                }
896
            }
897
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
898
            {
899
                using (SqlConnection connection = GetSqlConnection())
900
                {
901
                    try
902
                    {
903
                        if (connection != null && connection.State == ConnectionState.Open)
904
                        {
905
                            using (SqlCommand cmd = connection.CreateCommand())
906
                            {
907
                                cmd.CommandText = string.Format(@"
908
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
909
                            FROM {0} as lp 
910
                                 LEFT OUTER JOIN {1} as sp 
911
                                      ON lp.UID = sp.UID
912
                                 LEFT OUTER JOIN {2} as spa 
913
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
914
                                using (SqlDataReader dr = cmd.ExecuteReader())
915
                                    dt.Load(dr);
916
                            }
917
                            connection.Close();
918
                        }
919
                    }
920
                    catch (Exception ex)
921
                    {
922
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
923
                    }
924
                    finally
925
                    {
926
                        if (connection != null)
927
                            connection.Dispose();
928
                    }
929
                }
930
            }
931

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

    
992
            return dt;
993
        }
994
        public static DataTable SelectProjectAttribute()
995
        {
996
            DataTable dt = new DataTable();
997
            Project_Info projectInfo = Project_Info.GetInstance();
998
            if (projectInfo.DBType == ID2DB_Type.SQLite)
999
            {
1000
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1001
                {
1002
                    try
1003
                    {
1004
                        connection.Open();
1005
                        using (SQLiteCommand cmd = connection.CreateCommand())
1006
                        {
1007
                            cmd.CommandText = string.Format(@"
1008
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spa.ISTEXT, spl.LOCATION, spl.LEADERLINE, sa.Property
1009
                            FROM {1} as sa, {0} as st 
1010
                                 LEFT OUTER JOIN {2} as sp 
1011
                                      ON sa.UID = SP.UID 
1012
                                LEFT OUTER JOIN {3} as spa 
1013
                                     ON sa.UID = spa.UID
1014
                                LEFT OUTER JOIN {4} as spl 
1015
                                     ON sa.UID = spl.UID
1016
                            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);
1017
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1018
                                dt.Load(dr);
1019
                        }
1020
                        connection.Close();
1021
                    }
1022
                    catch (Exception ex)
1023
                    {
1024
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1025
                    }
1026
                    finally
1027
                    {
1028
                        connection.Dispose();
1029
                    }
1030
                }
1031
            }
1032
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1033
            {
1034
                using (SqlConnection connection = GetSqlConnection())
1035
                {
1036
                    try
1037
                    {
1038
                        if (connection != null && connection.State == ConnectionState.Open)
1039
                        {
1040
                            using (SqlCommand cmd = connection.CreateCommand())
1041
                            {
1042
                                cmd.CommandText = string.Format(@"
1043
                            SELECT sa.UID, sa.DisplayAttribute, st.Type, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spa.ISTEXT, spl.LOCATION, spl.LEADERLINE, sa.Property
1044
                            FROM {1} as sa
1045
                                 LEFT OUTER JOIN {2} as sp 
1046
                                      ON sa.UID = SP.UID 
1047
                                LEFT OUTER JOIN {3} as spa 
1048
                                     ON sa.UID = spa.UID
1049
                                LEFT OUTER JOIN {4} as spl 
1050
                                     ON sa.UID = spl.UID
1051
                                LEFT OUTER JOIN {0} as st 
1052
                                     ON sa.SymbolType_UID = st.UID 
1053
                            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);
1054
                                using (SqlDataReader dr = cmd.ExecuteReader())
1055
                                    dt.Load(dr);
1056
                            }
1057
                            connection.Close();
1058
                        }
1059
                    }
1060
                    catch (Exception ex)
1061
                    {
1062
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1063
                    }
1064
                    finally
1065
                    {
1066
                        if (connection != null)
1067
                            connection.Dispose();
1068
                    }
1069
                }
1070
            }
1071

    
1072

    
1073
            return dt;
1074
        }
1075

    
1076
        public static DataTable SelectDrawingProjectAttribute()
1077
        {
1078
            DataTable dt = new DataTable();
1079
            Project_Info projectInfo = Project_Info.GetInstance();
1080
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1081
            {
1082
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1083
                {
1084
                    try
1085
                    {
1086
                        connection.Open();
1087
                        using (SQLiteCommand cmd = connection.CreateCommand())
1088
                        {
1089
                            cmd.CommandText = string.Format(@"
1090
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1091
                            FROM {0} as da 
1092
                                 LEFT OUTER JOIN {1} as sam 
1093
                                      ON da.UID = sam.UID;
1094
                            ", DrawingAttribute_Table, SPPID_ATTRIBUTE_MAPPING_TABLE);
1095
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1096
                                dt.Load(dr);
1097
                        }
1098
                        connection.Close();
1099
                    }
1100
                    catch (Exception ex)
1101
                    {
1102
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1103
                    }
1104
                    finally
1105
                    {
1106
                        connection.Dispose();
1107
                    }
1108
                }
1109
            }
1110
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1111
            {
1112
                using (SqlConnection connection = GetSqlConnection())
1113
                {
1114
                    try
1115
                    {
1116
                        if (connection != null && connection.State == ConnectionState.Open)
1117
                        {
1118
                            using (SqlCommand cmd = connection.CreateCommand())
1119
                            {
1120
                                cmd.CommandText = string.Format(@"
1121
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1122
                            FROM {0} as da
1123
                                 LEFT OUTER JOIN {1} as sam 
1124
                                      ON da.UID = sam.UID;
1125
                            ", DrawingAttribute_Table, SPPID_ATTRIBUTE_MAPPING_TABLE);
1126
                                using (SqlDataReader dr = cmd.ExecuteReader())
1127
                                    dt.Load(dr);
1128
                            }
1129
                            connection.Close();
1130
                        }
1131
                    }
1132
                    catch (Exception ex)
1133
                    {
1134
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1135
                    }
1136
                    finally
1137
                    {
1138
                        if (connection != null)
1139
                            connection.Dispose();
1140
                    }
1141
                }
1142
            }
1143

    
1144
            dt.Merge(SelectDrawingProjectAttribute2());
1145

    
1146
            return dt;
1147
        }
1148

    
1149
        private static DataTable SelectDrawingProjectAttribute2()
1150
        {
1151
            DataTable dt = new DataTable();
1152
            Project_Info projectInfo = Project_Info.GetInstance();
1153
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1154
            {
1155
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1156
                {
1157
                    try
1158
                    {
1159
                        connection.Open();
1160
                        using (SQLiteCommand cmd = connection.CreateCommand())
1161
                        {
1162
                            cmd.CommandText = string.Format(@"
1163
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1164
                            FROM {0} as da 
1165
                                 LEFT OUTER JOIN {1} as sam 
1166
                                      ON da.UID = sam.UID;
1167
                            ", ID2_DRAWINGATTRIBUTE_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
1168
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1169
                                dt.Load(dr);
1170
                        }
1171
                        connection.Close();
1172
                    }
1173
                    catch (Exception ex)
1174
                    {
1175
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1176
                    }
1177
                    finally
1178
                    {
1179
                        connection.Dispose();
1180
                    }
1181
                }
1182
            }
1183
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1184
            {
1185
                using (SqlConnection connection = GetSqlConnection())
1186
                {
1187
                    try
1188
                    {
1189
                        if (connection != null && connection.State == ConnectionState.Open)
1190
                        {
1191
                            using (SqlCommand cmd = connection.CreateCommand())
1192
                            {
1193
                                cmd.CommandText = string.Format(@"
1194
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1195
                            FROM {0} as da
1196
                                 LEFT OUTER JOIN {1} as sam 
1197
                                      ON da.UID = sam.UID;
1198
                            ", ID2_DRAWINGATTRIBUTE_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
1199
                                using (SqlDataReader dr = cmd.ExecuteReader())
1200
                                    dt.Load(dr);
1201
                            }
1202
                            connection.Close();
1203
                        }
1204
                    }
1205
                    catch (Exception ex)
1206
                    {
1207
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1208
                    }
1209
                    finally
1210
                    {
1211
                        if (connection != null)
1212
                            connection.Dispose();
1213
                    }
1214
                }
1215
            }
1216

    
1217
            return dt;
1218
        }
1219

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

    
1278
            return dt;
1279
        }
1280

    
1281
        public static DataTable SelectOPCRelations()
1282
        {
1283
            DataTable dt = new DataTable();
1284
            Project_Info projectInfo = Project_Info.GetInstance();
1285
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1286
            {
1287
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1288
                {
1289
                    try
1290
                    {
1291
                        connection.Open();
1292
                        using (SQLiteCommand cmd = connection.CreateCommand())
1293
                        {
1294
                            cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1295
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1296
                                dt.Load(dr);
1297
                        }
1298
                        connection.Close();
1299
                    }
1300
                    catch (Exception ex)
1301
                    {
1302
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1303
                    }
1304
                    finally
1305
                    {
1306
                        connection.Dispose();
1307
                    }
1308
                }
1309
            }
1310
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1311
            {
1312
                using (SqlConnection connection = GetSqlConnection())
1313
                {
1314
                    try
1315
                    {
1316
                        if (connection != null && connection.State == ConnectionState.Open)
1317
                        {
1318
                            using (SqlCommand cmd = connection.CreateCommand())
1319
                            {
1320
                                cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1321
                                using (SqlDataReader dr = cmd.ExecuteReader())
1322
                                    dt.Load(dr);
1323
                            }
1324
                            connection.Close();
1325
                        }
1326
                    }
1327
                    catch (Exception ex)
1328
                    {
1329
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1330
                    }
1331
                    finally
1332
                    {
1333
                        if (connection != null)
1334
                            connection.Dispose();
1335
                    }
1336
                }
1337
            }
1338

    
1339
            return dt;
1340
        }
1341

    
1342
        public static DataTable SelectDrawings()
1343
        {
1344
            DataTable dt = new DataTable();
1345
            Project_Info projectInfo = Project_Info.GetInstance();
1346
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1347
            {
1348
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1349
                {
1350
                    try
1351
                    {
1352
                        connection.Open();
1353
                        using (SQLiteCommand cmd = connection.CreateCommand())
1354
                        {
1355
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1356
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1357
                                dt.Load(dr);
1358
                        }
1359
                        connection.Close();
1360
                    }
1361
                    catch (Exception ex)
1362
                    {
1363
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1364
                    }
1365
                    finally
1366
                    {
1367
                        connection.Dispose();
1368
                    }
1369
                }
1370
            }
1371
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1372
            {
1373
                using (SqlConnection connection = GetSqlConnection())
1374
                {
1375
                    try
1376
                    {
1377
                        if (connection != null && connection.State == ConnectionState.Open)
1378
                        {
1379
                            using (SqlCommand cmd = connection.CreateCommand())
1380
                            {
1381
                                cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1382
                                using (SqlDataReader dr = cmd.ExecuteReader())
1383
                                    dt.Load(dr);
1384
                            }
1385
                            connection.Close();
1386
                        }
1387
                    }
1388
                    catch (Exception ex)
1389
                    {
1390
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1391
                    }
1392
                    finally
1393
                    {
1394
                        if (connection != null)
1395
                            connection.Dispose();
1396
                    }
1397
                }
1398
            }
1399

    
1400
            return dt;
1401
        }
1402

    
1403
        public static DataTable SelectOPCInfo()
1404
        {
1405
            DataTable dt = new DataTable();
1406
            Project_Info projectInfo = Project_Info.GetInstance();
1407
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1408
            {
1409
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1410
                {
1411
                    try
1412
                    {
1413
                        connection.Open();
1414
                        using (SQLiteCommand cmd = connection.CreateCommand())
1415
                        {
1416
                            cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO);
1417
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1418
                                dt.Load(dr);
1419
                        }
1420
                        connection.Close();
1421
                    }
1422
                    catch (Exception ex)
1423
                    {
1424
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1425
                    }
1426
                    finally
1427
                    {
1428
                        connection.Dispose();
1429
                    }
1430
                }
1431
            }
1432
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1433
            {
1434
                using (SqlConnection connection = GetSqlConnection())
1435
                {
1436
                    try
1437
                    {
1438
                        if (connection != null && connection.State == ConnectionState.Open)
1439
                        {
1440
                            using (SqlCommand cmd = connection.CreateCommand())
1441
                            {
1442
                                cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = 0", SPPID_OPC_INFO);
1443
                                using (SqlDataReader dr = cmd.ExecuteReader())
1444
                                    dt.Load(dr);
1445
                            }
1446
                            connection.Close();
1447
                        }
1448
                    }
1449
                    catch (Exception ex)
1450
                    {
1451
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1452
                    }
1453
                    finally
1454
                    {
1455
                        if (connection != null)
1456
                            connection.Dispose();
1457
                    }
1458
                }
1459
            }
1460

    
1461
            return dt;
1462
        }
1463

    
1464
        public static DataTable SelectSymbolType()
1465
        {
1466
            DataTable dt = new DataTable();
1467
            Project_Info projectInfo = Project_Info.GetInstance();
1468
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1469
            {
1470
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1471
                {
1472
                    try
1473
                    {
1474
                        connection.Open();
1475
                        using (SQLiteCommand cmd = connection.CreateCommand())
1476
                        {
1477
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1478
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1479
                                dt.Load(dr);
1480
                        }
1481
                        connection.Close();
1482
                    }
1483
                    catch (Exception ex)
1484
                    {
1485
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1486
                    }
1487
                    finally
1488
                    {
1489
                        connection.Dispose();
1490
                    }
1491
                }
1492
            }
1493
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1494
            {
1495
                using (SqlConnection connection = GetSqlConnection())
1496
                {
1497
                    try
1498
                    {
1499
                        if (connection != null && connection.State == ConnectionState.Open)
1500
                        {
1501
                            using (SqlCommand cmd = connection.CreateCommand())
1502
                            {
1503
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1504
                                using (SqlDataReader dr = cmd.ExecuteReader())
1505
                                    dt.Load(dr);
1506
                            }
1507
                            connection.Close();
1508
                        }
1509
                    }
1510
                    catch (Exception ex)
1511
                    {
1512
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1513
                    }
1514
                    finally
1515
                    {
1516
                        if (connection != null)
1517
                            connection.Dispose();
1518
                    }
1519
                }
1520
            }
1521

    
1522
            return dt;
1523
        }
1524

    
1525
        public static DataTable SelectDrawingInfo()
1526
        {
1527
            DataTable dt = new DataTable();
1528
            Project_Info projectInfo = Project_Info.GetInstance();
1529
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1530
            {
1531
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1532
                {
1533
                    try
1534
                    {
1535
                        connection.Open();
1536
                        using (SQLiteCommand cmd = connection.CreateCommand())
1537
                        {
1538
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1539
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1540
                                dt.Load(dr);
1541
                        }
1542
                        connection.Close();
1543
                    }
1544
                    catch (Exception ex)
1545
                    {
1546
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1547
                    }
1548
                    finally
1549
                    {
1550
                        connection.Dispose();
1551
                    }
1552
                }
1553
            }
1554
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1555
            {
1556
                using (SqlConnection connection = GetSqlConnection())
1557
                {
1558
                    try
1559
                    {
1560
                        if (connection != null && connection.State == ConnectionState.Open)
1561
                        {
1562
                            using (SqlCommand cmd = connection.CreateCommand())
1563
                            {
1564
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1565
                                using (SqlDataReader dr = cmd.ExecuteReader())
1566
                                    dt.Load(dr);
1567
                            }
1568
                            connection.Close();
1569
                        }
1570
                    }
1571
                    catch (Exception ex)
1572
                    {
1573
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1574
                    }
1575
                    finally
1576
                    {
1577
                        if (connection != null)
1578
                            connection.Dispose();
1579
                    }
1580
                }
1581
            }
1582

    
1583
            return dt;
1584
        }
1585

    
1586
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas)
1587
        {
1588
            Project_Info projectInfo = Project_Info.GetInstance();
1589
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1590
            {
1591
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1592
                {
1593
                    try
1594
                    {
1595
                        connection.Open();
1596
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1597
                        {
1598
                            try
1599
                            {
1600
                                using (SQLiteCommand cmd = connection.CreateCommand())
1601
                                {
1602
                                    foreach (var item in datas)
1603
                                    {
1604
                                        cmd.Parameters.Clear();
1605
                                        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);
1606
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1607
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1608
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1609
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1610
                                        cmd.ExecuteNonQuery();
1611
                                    }
1612
                                }
1613
                                transaction.Commit();
1614
                                connection.Close();
1615
                            }
1616
                            catch (Exception ex)
1617
                            {
1618
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1619
                                transaction.Rollback();
1620
                                return false;
1621
                            }
1622
                            finally
1623
                            {
1624
                                transaction.Dispose();
1625
                            }
1626
                        }
1627
                    }
1628
                    catch (Exception ex)
1629
                    {
1630
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1631
                        return false;
1632
                    }
1633
                    finally
1634
                    {
1635
                        connection.Dispose();
1636
                    }
1637
                }
1638
            }
1639
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1640
            {
1641
                using (SqlConnection connection = GetSqlConnection())
1642
                {
1643
                    try
1644
                    {
1645
                        if (connection != null && connection.State == ConnectionState.Open)
1646
                        {
1647
                            using (SqlCommand cmd = connection.CreateCommand())
1648
                            {
1649
                                foreach (var item in datas)
1650
                                {
1651
                                    cmd.Parameters.Clear();
1652
                                    cmd.CommandText = string.Format(@"
1653
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1654
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
1655
                                    ELSE
1656
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, item.Item1);
1657
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1658
                                    if (string.IsNullOrEmpty(item.Item2))
1659
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
1660
                                    else
1661
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1662
                                    if (string.IsNullOrEmpty(item.Item3))
1663
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
1664
                                    else
1665
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1666
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1667
                                    cmd.ExecuteNonQuery();
1668
                                }
1669
                            }
1670
                            connection.Close();
1671
                        }
1672
                    }
1673
                    catch (Exception ex)
1674
                    {
1675
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1676
                        return false;
1677
                    }
1678
                    finally
1679
                    {
1680
                        if (connection != null)
1681
                            connection.Dispose();
1682
                    }
1683
                }
1684
            }
1685

    
1686
            return true;
1687
        }
1688

    
1689
        public static bool InsertAttributeMapping(List<Tuple<string, string, bool>> datas)
1690
        {
1691
            Project_Info projectInfo = Project_Info.GetInstance();
1692
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1693
            {
1694
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1695
                {
1696
                    try
1697
                    {
1698
                        connection.Open();
1699
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1700
                        {
1701
                            try
1702
                            {
1703
                                using (SQLiteCommand cmd = connection.CreateCommand())
1704
                                {
1705
                                    foreach (var item in datas)
1706
                                    {
1707
                                        cmd.Parameters.Clear();
1708
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE, ISTEXT) VALUES (@UID, @SPPID_ATTRIBUTE, @ISTEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
1709
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1710
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1711
                                        cmd.Parameters.AddWithValue("@ISTEXT", item.Item3);
1712
                                        cmd.ExecuteNonQuery();
1713
                                    }
1714
                                }
1715
                                transaction.Commit();
1716
                                connection.Close();
1717
                            }
1718
                            catch (Exception ex)
1719
                            {
1720
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1721
                                transaction.Rollback();
1722
                            }
1723
                            finally
1724
                            {
1725
                                transaction.Dispose();
1726
                            }
1727
                        }
1728
                    }
1729
                    catch (Exception ex)
1730
                    {
1731
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1732
                        return false;
1733
                    }
1734
                    finally
1735
                    {
1736
                        connection.Dispose();
1737
                    }
1738
                }
1739
            }
1740
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1741
            {
1742
                using (SqlConnection connection = GetSqlConnection())
1743
                {
1744
                    try
1745
                    {
1746
                        if (connection != null && connection.State == ConnectionState.Open)
1747
                        {
1748
                            using (SqlCommand cmd = connection.CreateCommand())
1749
                            {
1750
                                foreach (var item in datas)
1751
                                {
1752
                                    cmd.Parameters.Clear();
1753
                                    cmd.CommandText = string.Format(@"
1754
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1755
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE, ISTEXT = @ISTEXT WHERE UID = @UID
1756
                                    ELSE
1757
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE, ISTEXT) VALUES (@UID, @SPPID_ATTRIBUTE, @ISTEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE, item.Item1);
1758

    
1759
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1760
                                    if (string.IsNullOrEmpty(item.Item2))
1761
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
1762
                                    else
1763
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1764
                                    cmd.Parameters.AddWithValue("@ISTEXT", item.Item3);
1765
                                    cmd.ExecuteNonQuery();
1766
                                }
1767
                            }
1768
                            connection.Close();
1769
                        }
1770
                    }
1771
                    catch (Exception ex)
1772
                    {
1773
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1774
                        return false;
1775
                    }
1776
                    finally
1777
                    {
1778
                        if (connection != null)
1779
                            connection.Dispose();
1780
                    }
1781
                }
1782
            }
1783
            return true;
1784
        }
1785

    
1786
        public static bool InsertLabelInfoMapping(List<Tuple<string, int, bool>> datas)
1787
        {
1788
            Project_Info projectInfo = Project_Info.GetInstance();
1789
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1790
            {
1791
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1792
                {
1793
                    try
1794
                    {
1795
                        connection.Open();
1796
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1797
                        {
1798
                            try
1799
                            {
1800
                                using (SQLiteCommand cmd = connection.CreateCommand())
1801
                                {
1802
                                    foreach (var item in datas)
1803
                                    {
1804
                                        cmd.Parameters.Clear();
1805
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE);
1806
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1807
                                        cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
1808
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
1809
                                        cmd.ExecuteNonQuery();
1810
                                    }
1811
                                }
1812
                                transaction.Commit();
1813
                                connection.Close();
1814
                            }
1815
                            catch (Exception ex)
1816
                            {
1817
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1818
                                transaction.Rollback();
1819
                                return false;
1820
                            }
1821
                            finally
1822
                            {
1823
                                transaction.Dispose();
1824
                            }
1825
                        }
1826
                    }
1827
                    catch (Exception ex)
1828
                    {
1829
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1830
                        return false;
1831
                    }
1832
                    finally
1833
                    {
1834
                        connection.Dispose();
1835
                    }
1836
                }
1837
            }
1838
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1839
            {
1840
                using (SqlConnection connection = GetSqlConnection())
1841
                {
1842
                    try
1843
                    {
1844
                        if (connection != null && connection.State == ConnectionState.Open)
1845
                        {
1846
                            using (SqlCommand cmd = connection.CreateCommand())
1847
                            {
1848
                                foreach (var item in datas)
1849
                                {
1850
                                    cmd.Parameters.Clear();
1851
                                    cmd.CommandText = string.Format(@"
1852
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1853
                                        UPDATE {0} SET LOCATION = @LOCATION, LEADERLINE = @LEADERLINE WHERE UID = @UID
1854
                                    ELSE
1855
                                        INSERT INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE, item.Item1);
1856

    
1857
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1858
                                    cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
1859
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
1860
                                    cmd.ExecuteNonQuery();
1861
                                }
1862
                            }
1863
                            connection.Close();
1864
                        }
1865
                    }
1866
                    catch (Exception ex)
1867
                    {
1868
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1869
                        return false;
1870
                    }
1871
                    finally
1872
                    {
1873
                        if (connection != null)
1874
                            connection.Dispose();
1875
                    }
1876
                }
1877
            }
1878
            return true;
1879
        }
1880

    
1881
        public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document)
1882
        {
1883
            Project_Info projectInfo = Project_Info.GetInstance();
1884
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1885
            {
1886
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1887
                {
1888
                    try
1889
                    {
1890
                        connection.Open();
1891
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1892
                        {
1893
                            try
1894
                            {
1895
                                using (SQLiteCommand cmd = connection.CreateCommand())
1896
                                {
1897
                                    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);
1898
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1899
                                    cmd.Parameters.AddWithValue("@PATH", path);
1900
                                    cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1901
                                    cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1902
                                    cmd.ExecuteNonQuery();
1903
                                }
1904

    
1905
                                List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1906
                                foreach (var item in OPCs)
1907
                                {
1908
                                    using (SQLiteCommand cmd = connection.CreateCommand())
1909
                                    {
1910
                                        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);
1911
                                        cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1912
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1913
                                        cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1914
                                        cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1915
                                        cmd.Parameters.AddWithValue("@PAIRED", false);
1916
                                        cmd.ExecuteNonQuery();
1917
                                    }
1918
                                }
1919

    
1920
                                transaction.Commit();
1921
                                connection.Close();
1922
                            }
1923
                            catch (Exception ex)
1924
                            {
1925
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1926
                                transaction.Rollback();
1927
                                return false;
1928
                            }
1929
                            finally
1930
                            {
1931
                                transaction.Dispose();
1932
                            }
1933
                        }
1934
                    }
1935
                    catch (Exception ex)
1936
                    {
1937
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1938
                        return false;
1939
                    }
1940
                    finally
1941
                    {
1942
                        connection.Dispose();
1943
                    }
1944
                }
1945
            }
1946
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1947
            {
1948
                using (SqlConnection connection = GetSqlConnection())
1949
                {
1950
                    try
1951
                    {
1952
                        if (connection != null && connection.State == ConnectionState.Open)
1953
                        {
1954
                            using (SqlCommand cmd = connection.CreateCommand())
1955
                            {
1956
                                cmd.Parameters.Clear();
1957
                                cmd.CommandText = string.Format(@"
1958
                                    IF EXISTS (SELECT * FROM {0} WHERE ID2_DRAWING_UID = '{1}')
1959
                                        UPDATE {0} SET PATH = @PATH, DRAWINGNUMBER = @DRAWINGNUMBER, DRAWINGNAME = @DRAWINGNAME WHERE ID2_DRAWING_UID = @ID2_DRAWING_UID
1960
                                    ELSE
1961
                                        INSERT INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO, document.UID);
1962

    
1963
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1964
                                cmd.Parameters.AddWithValue("@PATH", path);
1965
                                cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1966
                                cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1967
                                cmd.ExecuteNonQuery();
1968
                            }
1969

    
1970
                            List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1971
                            foreach (var item in OPCs)
1972
                            {
1973
                                using (SqlCommand cmd = connection.CreateCommand())
1974
                                {
1975
                                    cmd.Parameters.Clear();
1976
                                    cmd.CommandText = string.Format(@"
1977
                                    IF EXISTS (SELECT * FROM {0} WHERE ID2_OPC_UID = '{1}')
1978
                                        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
1979
                                    ELSE
1980
                                        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);
1981

    
1982
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1983
                                    if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
1984
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value);
1985
                                    else
1986
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1987
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1988
                                    cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1989
                                    cmd.Parameters.AddWithValue("@PAIRED", false);
1990
                                    cmd.ExecuteNonQuery();
1991
                                }
1992
                            }
1993
                            connection.Close();
1994
                        }
1995
                    }
1996
                    catch (Exception ex)
1997
                    {
1998
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1999
                        return false;
2000
                    }
2001
                    finally
2002
                    {
2003
                        if (connection != null)
2004
                            connection.Dispose();
2005
                    }
2006
                }
2007
            }
2008
            return true;
2009
        }
2010

    
2011
        public static bool InsertLineNumberInfo(string path, string drawingNumber, string drawingName, SPPID_Document document)
2012
        {
2013
            Project_Info projectInfo = Project_Info.GetInstance();
2014
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2015
            {
2016
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2017
                {
2018
                    try
2019
                    {
2020
                        connection.Open();
2021
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2022
                        {
2023
                            try
2024
                            {
2025
                                using (SQLiteCommand cmd = connection.CreateCommand())
2026
                                {
2027
                                    foreach (var lineNumber in document.LINENUMBERS)
2028
                                    {
2029
                                        foreach (var attribute in lineNumber.ATTRIBUTES)
2030
                                        {
2031
                                            LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2032
                                            if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2033
                                            {
2034
                                                if (mapping.SPPIDATTRIBUTENAME == "PlantGroup.Name")
2035
                                                {
2036
                                                    cmd.Parameters.Clear();
2037

    
2038

    
2039

    
2040
                                                    cmd.ExecuteNonQuery();
2041
                                                }
2042
                                            }
2043
                                        }
2044
                                    }
2045
                                }
2046

    
2047
                                transaction.Commit();
2048
                                connection.Close();
2049
                            }
2050
                            catch (Exception ex)
2051
                            {
2052
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2053
                                transaction.Rollback();
2054
                                return false;
2055
                            }
2056
                            finally
2057
                            {
2058
                                transaction.Dispose();
2059
                            }
2060
                        }
2061
                    }
2062
                    catch (Exception ex)
2063
                    {
2064
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2065
                        return false;
2066
                    }
2067
                    finally
2068
                    {
2069
                        connection.Dispose();
2070
                    }
2071
                }
2072
            }
2073
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2074
            {
2075
                using (SqlConnection connection = GetSqlConnection())
2076
                {
2077
                    try
2078
                    {
2079
                        if (connection != null && connection.State == ConnectionState.Open)
2080
                        {
2081
                            using (SqlCommand cmd = connection.CreateCommand())
2082
                            {
2083
                                foreach (var lineNumber in document.LINENUMBERS)
2084
                                {
2085
                                    foreach (var attribute in lineNumber.ATTRIBUTES)
2086
                                    {
2087
                                        LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2088
                                        if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2089
                                        {
2090
                                            if (mapping.SPPIDATTRIBUTENAME == "PlantGroup.Name")
2091
                                            {
2092
                                                cmd.Parameters.Clear();
2093

    
2094
                                                cmd.ExecuteNonQuery();
2095
                                            }
2096
                                        }
2097
                                    }
2098
                                }
2099
                            }
2100
                            connection.Close();
2101
                        }
2102
                    }
2103
                    catch (Exception ex)
2104
                    {
2105
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2106
                        return false;
2107
                    }
2108
                    finally
2109
                    {
2110
                        if (connection != null)
2111
                            connection.Dispose();
2112
                    }
2113
                }
2114
            }
2115
            return true;
2116
        }
2117
        public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired)
2118
        {
2119
            Project_Info projectInfo = Project_Info.GetInstance();
2120
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2121
            {
2122
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2123
                {
2124
                    try
2125
                    {
2126
                        connection.Open();
2127
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2128
                        {
2129
                            try
2130
                            {
2131
                                using (SQLiteCommand cmd = connection.CreateCommand())
2132
                                {
2133
                                    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);
2134
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
2135
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
2136
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
2137
                                    cmd.Parameters.AddWithValue("@PAIRED", Paired);
2138
                                    cmd.ExecuteNonQuery();
2139
                                }
2140

    
2141
                                transaction.Commit();
2142
                                connection.Close();
2143
                            }
2144
                            catch (Exception ex)
2145
                            {
2146
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2147
                                transaction.Rollback();
2148
                                return false;
2149
                            }
2150
                            finally
2151
                            {
2152
                                transaction.Dispose();
2153
                            }
2154
                        }
2155
                    }
2156
                    catch (Exception ex)
2157
                    {
2158
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2159
                        return false;
2160
                    }
2161
                    finally
2162
                    {
2163
                        connection.Dispose();
2164
                    }
2165
                }
2166
            }
2167
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2168
            {
2169
                using (SqlConnection connection = GetSqlConnection())
2170
                {
2171
                    try
2172
                    {
2173
                        if (connection != null && connection.State == ConnectionState.Open)
2174
                        {
2175
                            using (SqlCommand cmd = connection.CreateCommand())
2176
                            {
2177
                                cmd.Parameters.Clear();
2178
                                cmd.CommandText = string.Format(@"
2179
                                    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);
2180
                                cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
2181
                                if (string.IsNullOrEmpty(ModelItemID))
2182
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value);
2183
                                else
2184
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
2185
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
2186
                                cmd.Parameters.AddWithValue("@PAIRED", Paired);
2187
                                cmd.ExecuteNonQuery();
2188
                            }
2189
                            connection.Close();
2190
                        }
2191
                    }
2192
                    catch (Exception ex)
2193
                    {
2194
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2195
                        return false;
2196
                    }
2197
                    finally
2198
                    {
2199
                        if (connection != null)
2200
                            connection.Dispose();
2201
                    }
2202
                }
2203
            }
2204
            return true;
2205
        }
2206

    
2207
        public static bool ExportMappingData()
2208
        {
2209
            bool result = true;
2210
            try
2211
            {
2212
                DataSet dataSet = new DataSet();
2213
                DataTable symbolMappingDT = GetTable(SPPID_SYMBOL_MAPPING_TABLE);
2214
                if (symbolMappingDT != null)
2215
                {
2216
                    symbolMappingDT.TableName = SPPID_SYMBOL_MAPPING_TABLE;
2217
                    dataSet.Tables.Add(symbolMappingDT);
2218
                }
2219
                else
2220
                    result = false;
2221

    
2222
                DataTable attributeMappingDT = GetTable(SPPID_ATTRIBUTE_MAPPING_TABLE);
2223
                if (attributeMappingDT != null)
2224
                {
2225
                    attributeMappingDT.TableName = SPPID_ATTRIBUTE_MAPPING_TABLE;
2226
                    dataSet.Tables.Add(attributeMappingDT);
2227
                }
2228
                else
2229
                    result = false;
2230

    
2231
                if (result)
2232
                {
2233
                    string text = JsonConvert.SerializeObject(dataSet);
2234
                    using (StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf", false))
2235
                    {
2236
                        sw.Write(text);
2237
                        sw.Close();
2238
                        sw.Dispose();
2239
                    }
2240
                }
2241
            }
2242
            catch (Exception ex)
2243
            {
2244
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2245
                result = false;
2246
            }
2247

    
2248
            return result;
2249
        }
2250

    
2251
        public static bool ImportMappingData()
2252
        {
2253
            bool result = true;
2254
            try
2255
            {
2256
                string sJson = string.Empty;
2257
                using (StreamReader sw = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf"))
2258
                {
2259
                    sJson = sw.ReadToEnd();
2260
                    sw.Close();
2261
                    sw.Dispose();
2262
                }
2263

    
2264
                DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(sJson);
2265
                DataTable symbolMappingDT = dataSet.Tables[SPPID_SYMBOL_MAPPING_TABLE];
2266
                if (symbolMappingDT == null || !ImportSymbolMappingTable(symbolMappingDT))
2267
                    result = false;
2268

    
2269
                DataTable attributeMappingDT = dataSet.Tables[SPPID_ATTRIBUTE_MAPPING_TABLE];
2270
                if (attributeMappingDT == null || !ImportAttributeMappingTable(attributeMappingDT))
2271
                    result = false;
2272
            }
2273
            catch (Exception ex)
2274
            {
2275
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2276
                result = false;
2277
            }
2278

    
2279
            return result;
2280
        }
2281

    
2282
        private static DataTable GetTable(string tableName)
2283
        {
2284
            DataTable dt = new DataTable();
2285
            Project_Info projectInfo = Project_Info.GetInstance();
2286
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2287
            {
2288
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2289
                {
2290
                    try
2291
                    {
2292
                        connection.Open();
2293
                        using (SQLiteCommand cmd = connection.CreateCommand())
2294
                        {
2295
                            cmd.CommandText = string.Format("SELECT * FROM {0}", tableName);
2296
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
2297
                                dt.Load(dr);
2298
                        }
2299
                        connection.Close();
2300
                    }
2301
                    catch (Exception ex)
2302
                    {
2303
                        dt = null;
2304
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2305
                    }
2306
                    finally
2307
                    {
2308
                        connection.Dispose();
2309
                    }
2310
                }
2311
            }
2312
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2313
            {
2314
                using (SqlConnection connection = GetSqlConnection())
2315
                {
2316
                    try
2317
                    {
2318
                        if (connection != null && connection.State == ConnectionState.Open)
2319
                        {
2320
                            using (SqlCommand cmd = connection.CreateCommand())
2321
                            {
2322
                                cmd.CommandText = string.Format("SELECT * FROM {0}", tableName);
2323
                                using (SqlDataReader dr = cmd.ExecuteReader())
2324
                                    dt.Load(dr);
2325
                            }
2326
                            connection.Close();
2327
                        }
2328
                    }
2329
                    catch (Exception ex)
2330
                    {
2331
                        dt = null;
2332
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2333
                    }
2334
                    finally
2335
                    {
2336
                        if (connection != null)
2337
                            connection.Dispose();
2338
                    }
2339
                }
2340
            }
2341

    
2342
            return dt;
2343
        }
2344

    
2345
        private static bool ImportSymbolMappingTable(DataTable dt)
2346
        {
2347
            bool result = false;
2348

    
2349
            Project_Info projectInfo = Project_Info.GetInstance();
2350
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2351
            {
2352
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2353
                {
2354
                    try
2355
                    {
2356
                        connection.Open();
2357
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2358
                        {
2359
                            try
2360
                            {
2361
                                using (SQLiteCommand cmd = connection.CreateCommand())
2362
                                {
2363
                                    foreach (DataRow item in dt.Rows)
2364
                                    {
2365
                                        string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2366
                                        string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"];
2367
                                        string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"];
2368
                                        bool LEADERLINE = Convert.ToBoolean(item["LEADERLINE"]);
2369

    
2370
                                        cmd.Parameters.Clear();
2371
                                        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);
2372
                                        cmd.Parameters.AddWithValue("@UID", UID);
2373
                                        cmd.Parameters.AddWithValue("@NAME", NAME);
2374
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH);
2375
                                        cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE);
2376
                                        cmd.ExecuteNonQuery();
2377
                                    }
2378
                                }
2379
                                transaction.Commit();
2380
                                connection.Close();
2381
                                result = true;
2382
                            }
2383
                            catch (Exception ex)
2384
                            {
2385
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2386
                                transaction.Rollback();
2387
                            }
2388
                            finally
2389
                            {
2390
                                transaction.Dispose();
2391
                            }
2392
                        }
2393
                    }
2394
                    catch (Exception ex)
2395
                    {
2396
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2397
                    }
2398
                    finally
2399
                    {
2400
                        connection.Dispose();
2401
                    }
2402
                }
2403
            }
2404
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2405
            {
2406
                using (SqlConnection connection = GetSqlConnection())
2407
                {
2408
                    try
2409
                    {
2410
                        if (connection != null && connection.State == ConnectionState.Open)
2411
                        {
2412
                            using (SqlCommand cmd = connection.CreateCommand())
2413
                            {
2414
                                foreach (DataRow item in dt.Rows)
2415
                                {
2416
                                    string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2417
                                    string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"];
2418
                                    string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"];
2419
                                    bool LEADERLINE = DBNull.Value.Equals(item["LEADERLINE"]) ? false : Convert.ToBoolean(item["LEADERLINE"]);
2420

    
2421
                                    cmd.Parameters.Clear();
2422
                                    cmd.CommandText = string.Format(@"
2423
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2424
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
2425
                                    ELSE
2426
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, UID);
2427
                                    cmd.Parameters.AddWithValue("@UID", UID);
2428
                                    if (string.IsNullOrEmpty(NAME))
2429
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
2430
                                    else
2431
                                        cmd.Parameters.AddWithValue("@NAME", NAME);
2432
                                    if (string.IsNullOrEmpty(SPPID_SYMBOL_PATH))
2433
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
2434
                                    else
2435
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH);
2436
                                    cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE);
2437
                                    cmd.ExecuteNonQuery();
2438
                                }
2439
                            }
2440
                            connection.Close();
2441
                            result = true;
2442
                        }
2443
                    }
2444
                    catch (Exception ex)
2445
                    {
2446
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2447
                    }
2448
                    finally
2449
                    {
2450
                        if (connection != null)
2451
                            connection.Dispose();
2452
                    }
2453
                }
2454
            }
2455

    
2456
            return result;
2457
        }
2458

    
2459
        private static bool ImportAttributeMappingTable(DataTable dt)
2460
        {
2461
            bool result = false;
2462

    
2463
            Project_Info projectInfo = Project_Info.GetInstance();
2464
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2465
            {
2466
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2467
                {
2468
                    try
2469
                    {
2470
                        connection.Open();
2471
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2472
                        {
2473
                            try
2474
                            {
2475
                                using (SQLiteCommand cmd = connection.CreateCommand())
2476
                                {
2477
                                    foreach (DataRow item in dt.Rows)
2478
                                    {
2479
                                        string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2480
                                        string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"];
2481

    
2482
                                        cmd.Parameters.Clear();
2483
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
2484
                                        cmd.Parameters.AddWithValue("@UID", UID);
2485
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE);
2486
                                        cmd.ExecuteNonQuery();
2487
                                    }
2488
                                }
2489
                                transaction.Commit();
2490
                                connection.Close();
2491
                                result = true;
2492
                            }
2493
                            catch (Exception ex)
2494
                            {
2495
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2496
                                transaction.Rollback();
2497
                            }
2498
                            finally
2499
                            {
2500
                                transaction.Dispose();
2501
                            }
2502
                        }
2503
                    }
2504
                    catch (Exception ex)
2505
                    {
2506
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2507
                    }
2508
                    finally
2509
                    {
2510
                        connection.Dispose();
2511
                    }
2512
                }
2513
            }
2514
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2515
            {
2516
                using (SqlConnection connection = GetSqlConnection())
2517
                {
2518
                    try
2519
                    {
2520
                        if (connection != null && connection.State == ConnectionState.Open)
2521
                        {
2522
                            using (SqlCommand cmd = connection.CreateCommand())
2523
                            {
2524
                                foreach (DataRow item in dt.Rows)
2525
                                {
2526
                                    string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2527
                                    string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"];
2528

    
2529
                                    cmd.Parameters.Clear();
2530
                                    cmd.CommandText = string.Format(@"
2531
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2532
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE WHERE UID = @UID
2533
                                    ELSE
2534
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE, UID);
2535

    
2536
                                    cmd.Parameters.AddWithValue("@UID", UID);
2537
                                    if (string.IsNullOrEmpty(SPPID_ATTRIBUTE))
2538
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
2539
                                    else
2540
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE);
2541
                                    cmd.ExecuteNonQuery();
2542
                                }
2543
                            }
2544
                            connection.Close();
2545
                            result = true;
2546
                        }
2547
                    }
2548
                    catch (Exception ex)
2549
                    {
2550
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2551
                    }
2552
                    finally
2553
                    {
2554
                        if (connection != null)
2555
                            connection.Dispose();
2556
                    }
2557
                }
2558
            }
2559

    
2560

    
2561

    
2562
            return result;
2563
        }
2564
    }
2565
}
클립보드 이미지 추가 (최대 크기: 500 MB)