프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / DB / Project_DB.cs @ 306a0af9

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

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

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

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

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

    
59
            return connection;
60
        }
61

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

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

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

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

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

    
157
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
158
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
159
                                using (DataTable dt = new DataTable())
160
                                {
161
                                    dt.Load(dr);
162
                                    if (dt.Columns.Contains("DOCUMENT"))
163
                                    {
164
                                        cmd.CommandText = string.Format("DROP TABLE {0}", SPPID_DRAWING_INFO);
165
                                        cmd.ExecuteNonQuery();
166

    
167
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
168
                                        cmd.ExecuteNonQuery();
169
                                    }
170
                                }
171
                                #endregion
172
                            }
173
                            result = true;
174
                        }
175
                        connection.Close();
176
                    }
177
                    catch (Exception ex)
178
                    {
179
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
180
                    }
181
                    finally
182
                    {
183
                        connection.Dispose();
184
                    }
185
                }
186
            }
187
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
188
            {
189
                using (SqlConnection connection = GetSqlConnection())
190
                {
191
                    try
192
                    {
193
                        if (connection != null && connection.State == ConnectionState.Open)
194
                        {
195
                            using (SqlCommand cmd = connection.CreateCommand())
196
                            {
197
                                cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES";
198
                                using (SqlDataReader dr = cmd.ExecuteReader())
199
                                using (DataTable dt = new DataTable())
200
                                {
201
                                    dt.Load(dr);
202

    
203
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
204
                                    {
205
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX))", SPPID_DB_INFO_TABLE);
206
                                        cmd.ExecuteNonQuery();
207
                                    }
208
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
209
                                    {
210
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX), SettingType varchar(MAX))", SPPID_SETTING_TABLE);
211
                                        cmd.ExecuteNonQuery();
212
                                    }
213
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
214
                                    {
215
                                        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);
216
                                        cmd.ExecuteNonQuery();
217
                                    }
218
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
219
                                    {
220
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, SPPID_ATTRIBUTE varchar(MAX))", SPPID_ATTRIBUTE_MAPPING_TABLE);
221
                                        cmd.ExecuteNonQuery();
222
                                    }
223
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
224
                                    {
225
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BIT)", SPPID_LABEL_INFO_TABLE);
226
                                        cmd.ExecuteNonQuery();
227
                                    }
228
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
229
                                    {
230
                                        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);
231
                                        cmd.ExecuteNonQuery();
232
                                    }
233
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
234
                                    {
235
                                        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);
236
                                        cmd.ExecuteNonQuery();
237
                                    }
238
                                    if (dt.Select(string.Format("NAME = '{0}'", ID2_DRAWINGATTRIBUTE_TABLE)).Length == 0)
239
                                    {
240
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(255))", ID2_DRAWINGATTRIBUTE_TABLE);
241
                                        cmd.ExecuteNonQuery();
242

    
243
                                        List<string> datas = new List<string>() { "Unit", "Rev No", "Drawing No" };
244
                                        foreach (var data in datas)
245
                                        {
246
                                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES('{1}', '{1}')", ID2_DRAWINGATTRIBUTE_TABLE, data);
247
                                            cmd.ExecuteNonQuery();
248
                                        }
249
                                    }
250
                                }
251

    
252
                                #region Check Column 업데이트시 예비용
253
                                #endregion
254
                            }
255
                            result = true;
256
                        }
257
                    }
258
                    catch (Exception ex)
259
                    {
260
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
261
                    }
262
                    finally
263
                    {
264
                        if (connection != null)
265
                            connection.Dispose();
266
                    }
267
                }
268
            }
269

    
270
            return result;
271
        }
272

    
273
        public static bool SaveSPPID_DB_INFO(string jsonString)
274
        {
275
            Project_Info projectInfo = Project_Info.GetInstance();
276
            if (projectInfo.DBType == ID2DB_Type.SQLite)
277
            {
278
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
279
                {
280

    
281
                    try
282
                    {
283
                        connection.Open();
284
                        using (SQLiteCommand cmd = connection.CreateCommand())
285
                        {
286
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
287
                            cmd.ExecuteNonQuery();
288

    
289
                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
290
                            cmd.Parameters.AddWithValue("@jsonString", jsonString);
291
                            cmd.ExecuteNonQuery();
292
                        }
293
                        connection.Close();
294
                    }
295
                    catch (Exception ex)
296
                    {
297
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
298
                        return false;
299
                    }
300
                    finally
301
                    {
302
                        connection.Dispose();
303
                    }
304
                }
305
            }
306
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
307
            {
308
                using (SqlConnection connection = GetSqlConnection())
309
                {
310
                    try
311
                    {
312
                        if (connection != null && connection.State == ConnectionState.Open)
313
                        {
314
                            using (SqlCommand cmd = connection.CreateCommand())
315
                            {
316
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
317
                                cmd.ExecuteNonQuery();
318

    
319
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
320
                                cmd.Parameters.AddWithValue("@jsonString", jsonString);
321
                                cmd.ExecuteNonQuery();
322
                            }
323
                            connection.Close();
324
                        }
325
                        else
326
                        {
327
                            return false;
328
                        }
329
                    }
330
                    catch (Exception ex)
331
                    {
332
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
333
                        return false;
334
                    }
335
                    finally
336
                    {
337
                        if (connection != null)
338
                            connection.Dispose();
339
                    }
340
                }
341
            }
342

    
343

    
344
            return true;
345
        }
346

    
347
        public static DataTable SelectSPPID_DB_INFO()
348
        {
349
            DataTable dt = new DataTable();
350
            Project_Info projectInfo = Project_Info.GetInstance();
351
            if (projectInfo.DBType == ID2DB_Type.SQLite)
352
            {
353
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
354
                {
355
                    try
356
                    {
357
                        connection.Open();
358
                        using (SQLiteCommand cmd = connection.CreateCommand())
359
                        {
360
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
361
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
362
                                dt.Load(dr);
363
                        }
364
                        connection.Close();
365
                    }
366
                    catch (Exception ex)
367
                    {
368
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
369
                    }
370
                    finally
371
                    {
372
                        connection.Dispose();
373
                    }
374
                }
375
            }
376
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
377
            {
378
                using (SqlConnection connection = GetSqlConnection())
379
                {
380
                    try
381
                    {
382
                        if (connection != null && connection.State == ConnectionState.Open)
383
                        {
384
                            using (SqlCommand cmd = connection.CreateCommand())
385
                            {
386
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
387
                                using (SqlDataReader dr = cmd.ExecuteReader())
388
                                    dt.Load(dr);
389
                            }
390
                            connection.Close();
391
                        }
392
                    }
393
                    catch (Exception ex)
394
                    {
395
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
396
                    }
397
                    finally
398
                    {
399
                        if (connection != null)
400
                            connection.Dispose();
401
                    }
402
                }
403
            }
404

    
405
            return dt;
406
        }
407

    
408
        public static bool SaveETCSetting(Dictionary<string,string> dicSetting)
409
        {
410
            Project_Info projectInfo = Project_Info.GetInstance();
411
            if (projectInfo.DBType == ID2DB_Type.SQLite)
412
            {
413
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
414
                {
415

    
416
                    try
417
                    {
418
                        connection.Open();
419
                        using (SQLiteCommand cmd = connection.CreateCommand())
420
                        {
421
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
422
                            cmd.ExecuteNonQuery();
423

    
424
                            foreach (var item in dicSetting)
425
                            {
426
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
427
                                cmd.Parameters.Clear();
428
                                cmd.Parameters.AddWithValue("@jsonString", item.Value);
429
                                cmd.Parameters.AddWithValue("@SettingType", item.Key);
430
                                cmd.ExecuteNonQuery();
431
                            }
432
                        }
433
                        connection.Close();
434
                    }
435
                    catch (Exception ex)
436
                    {
437
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
438
                        return false;
439
                    }
440
                    finally
441
                    {
442
                        connection.Dispose();
443
                    }
444
                }
445
            }
446
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
447
            {
448
                using (SqlConnection connection = GetSqlConnection())
449
                {
450
                    try
451
                    {
452
                        if (connection != null && connection.State == ConnectionState.Open)
453
                        {
454
                            using (SqlCommand cmd = connection.CreateCommand())
455
                            {
456
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
457
                                cmd.ExecuteNonQuery();
458

    
459
                                foreach (var item in dicSetting)
460
                                {
461
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
462
                                    cmd.Parameters.Clear();
463
                                    cmd.Parameters.AddWithValue("@jsonString", item.Value);
464
                                    cmd.Parameters.AddWithValue("@SettingType", item.Key);
465
                                    cmd.ExecuteNonQuery();
466
                                }
467
                            }
468
                            connection.Close();
469
                        }
470
                        else
471
                            return false;
472
                    }
473
                    catch (Exception ex)
474
                    {
475
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
476
                        return false;
477
                    }
478
                    finally
479
                    {
480
                        if (connection != null)
481
                            connection.Dispose();
482
                    }
483
                }
484
            }
485

    
486
            return true;
487
        }
488

    
489
        public static DataTable SelectSetting()
490
        {
491
            DataTable dt = new DataTable();
492
            Project_Info projectInfo = Project_Info.GetInstance();
493
            if (projectInfo.DBType == ID2DB_Type.SQLite)
494
            {
495
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
496
                {
497
                    try
498
                    {
499
                        connection.Open();
500
                        using (SQLiteCommand cmd = connection.CreateCommand())
501
                        {
502
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
503
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
504
                                dt.Load(dr);
505
                        }
506
                        connection.Close();
507
                    }
508
                    catch (Exception ex)
509
                    {
510
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
511
                    }
512
                    finally
513
                    {
514
                        connection.Dispose();
515
                    }
516
                }
517
            }
518
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
519
            {
520
                using (SqlConnection connection = GetSqlConnection())
521
                {
522
                    try
523
                    {
524
                        if (connection != null && connection.State == ConnectionState.Open)
525
                        {
526
                            using (SqlCommand cmd = connection.CreateCommand())
527
                            {
528
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
529
                                using (SqlDataReader dr = cmd.ExecuteReader())
530
                                    dt.Load(dr);
531
                            }
532
                            connection.Close();
533
                        }
534
                    }
535
                    catch (Exception ex)
536
                    {
537
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
538
                    }
539
                    finally
540
                    {
541
                        if (connection != null)
542
                            connection.Dispose();
543
                    }
544
                }
545
            }
546

    
547
            return dt;
548
        }
549

    
550
        public static DataTable SelectProjectSymbol()
551
        {
552
            DataTable dt = new DataTable();
553
            Project_Info projectInfo = Project_Info.GetInstance();
554
            if (projectInfo.DBType == ID2DB_Type.SQLite)
555
            {
556
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
557
                {
558
                    try
559
                    {
560
                        connection.Open();
561
                        using (SQLiteCommand cmd = connection.CreateCommand())
562
                        {
563
                            cmd.CommandText = string.Format(@"
564
                            SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
565
                                LEFT OUTER JOIN {2} as sp 
566
                                    ON s.UID = SP.UID 
567
                            WHERE s.SymbolType_UID = st.UID 
568
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
569
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
570
                                dt.Load(dr);
571

    
572
                            DataTable dtClone = dt.Clone();
573
                            dtClone.Columns["UID"].DataType = typeof(string);
574
                            foreach (DataRow row in dt.Rows)
575
                            {
576
                                dtClone.ImportRow(row);
577
                            }
578
                            dt.Dispose();
579
                            dt = dtClone;
580
                        }
581
                        connection.Close();
582
                    }
583
                    catch (Exception ex)
584
                    {
585
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
586
                    }
587
                    finally
588
                    {
589
                        connection.Dispose();
590
                    }
591
                }
592
            }
593
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
594
            {
595
                using (SqlConnection connection = GetSqlConnection())
596
                {
597
                    try
598
                    {
599
                        if (connection != null && connection.State == ConnectionState.Open)
600
                        {
601
                            using (SqlCommand cmd = connection.CreateCommand())
602
                            {
603
                                cmd.CommandText = string.Format(@"
604
                            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 
605
                                LEFT OUTER JOIN {2} as sp 
606
                                    ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID)
607
                            WHERE s.SymbolType_UID = st.UID 
608
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
609
                                using (SqlDataReader dr = cmd.ExecuteReader())
610
                                    dt.Load(dr);
611
                            }
612
                            connection.Close();
613
                        }
614
                    }
615
                    catch (Exception ex)
616
                    {
617
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
618
                    }
619
                    finally
620
                    {
621
                        if (connection != null)
622
                            connection.Dispose();
623
                    }
624
                }
625
            }
626

    
627
            return dt;
628
        }
629

    
630
        public static DataTable SelectProjectChildSymbol()
631
        {
632
            DataTable result = new DataTable();
633
            result.Columns.Add("UID");
634
            result.Columns.Add("Name");
635
            result.Columns.Add("Type");
636
            result.Columns.Add("SPPID_SYMBOL_PATH");
637
            
638
            Project_Info projectInfo = Project_Info.GetInstance();
639
            if (projectInfo.DBType == ID2DB_Type.SQLite)
640
            {
641
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
642
                using (DataTable dt = new DataTable())
643
                {
644
                    try
645
                    {
646
                        connection.Open();
647
                        using (SQLiteCommand cmd = connection.CreateCommand())
648
                        {
649
                            cmd.CommandText = string.Format(@"
650
                            SELECT AdditionalSymbol FROM Symbol");
651
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
652
                                dt.Load(dr);
653
                            List<string> childList = new List<string>();
654
                            foreach (DataRow row in dt.Rows)
655
                            {
656
                                if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
657
                                {
658
                                    string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
659
                                    foreach (var childString in array)
660
                                    {
661
                                        childList.Add(childString.Split(new char[] { ',' })[2]);
662
                                    }
663
                                }
664

    
665
                            }
666

    
667
                            dt.Clear();
668
                            cmd.Reset();
669
                            cmd.CommandText = string.Format(@"
670
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
671
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
672
                                dt.Load(dr);
673

    
674
                            childList = childList.Distinct().ToList();
675
                            foreach (var child in childList)
676
                            {
677
                                string mappingPath = string.Empty;
678
                                DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
679
                                if (rows.Length == 1)
680
                                    mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
681

    
682
                                DataRow newRow = result.NewRow();
683
                                newRow["UID"] = child;
684
                                newRow["Name"] = child;
685
                                newRow["Type"] = "Child Symbol";
686
                                newRow["SPPID_SYMBOL_PATH"] = mappingPath;
687
                                result.Rows.Add(newRow);
688
                            }
689
                        }
690
                        connection.Close();
691
                    }
692
                    catch (Exception ex)
693
                    {
694
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
695
                    }
696
                    finally
697
                    {
698
                        connection.Dispose();
699
                    }
700
                }
701
            }
702
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
703
            {
704
                using (SqlConnection connection = GetSqlConnection())
705
                using (DataTable dt = new DataTable())
706
                {
707
                    try
708
                    {
709
                        if (connection != null && connection.State == ConnectionState.Open)
710
                        {
711
                            using (SqlCommand cmd = connection.CreateCommand())
712
                            {
713
                                cmd.CommandText = string.Format(@"
714
                            SELECT AdditionalSymbol FROM Symbol");
715
                                using (SqlDataReader dr = cmd.ExecuteReader())
716
                                    dt.Load(dr);
717
                                List<string> childList = new List<string>();
718
                                foreach (DataRow row in dt.Rows)
719
                                {
720
                                    if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
721
                                    {
722
                                        string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
723
                                        foreach (var childString in array)
724
                                        {
725
                                            childList.Add(childString.Split(new char[] { ',' })[2]);
726
                                        }
727
                                    }
728

    
729
                                }
730

    
731
                                dt.Clear();
732
                                cmd.CommandText = string.Format(@"
733
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
734
                                using (SqlDataReader dr = cmd.ExecuteReader())
735
                                    dt.Load(dr);
736

    
737
                                childList = childList.Distinct().ToList();
738
                                foreach (var child in childList)
739
                                {
740
                                    string mappingPath = string.Empty;
741
                                    DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
742
                                    if (rows.Length == 1)
743
                                        mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
744

    
745
                                    DataRow newRow = result.NewRow();
746
                                    newRow["UID"] = child;
747
                                    newRow["Name"] = child;
748
                                    newRow["Type"] = "Child Symbol";
749
                                    newRow["SPPID_SYMBOL_PATH"] = mappingPath;
750
                                    result.Rows.Add(newRow);
751
                                }
752
                            }
753
                            connection.Close();
754
                        }
755
                    }
756
                    catch (Exception ex)
757
                    {
758
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
759
                    }
760
                    finally
761
                    {
762
                        if (connection != null)
763
                            connection.Dispose();
764
                    }
765
                }
766
            }
767
            return result;
768
        }
769

    
770
        public static DataTable SelectProjectLine()
771
        {
772
            DataTable dt = new DataTable();
773
            Project_Info projectInfo = Project_Info.GetInstance();
774
            if (projectInfo.DBType == ID2DB_Type.SQLite)
775
            {
776
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
777
                {
778
                    try
779
                    {
780
                        connection.Open();
781
                        using (SQLiteCommand cmd = connection.CreateCommand())
782
                        {
783
                            cmd.CommandText = string.Format(@"
784
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
785
                                LEFT OUTER JOIN {1} as sp 
786
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
787
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
788
                                dt.Load(dr);
789
                        }
790
                        connection.Close();
791
                    }
792
                    catch (Exception ex)
793
                    {
794
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
795
                    }
796
                    finally
797
                    {
798
                        connection.Dispose();
799
                    }
800
                }
801
            }
802
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
803
            {
804
                using (SqlConnection connection = GetSqlConnection())
805
                {
806
                    try
807
                    {
808
                        if (connection != null && connection.State == ConnectionState.Open)
809
                        {
810
                            using (SqlCommand cmd = connection.CreateCommand())
811
                            {
812
                                cmd.CommandText = string.Format(@"
813
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
814
                                LEFT OUTER JOIN {1} as sp 
815
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
816
                                using (SqlDataReader dr = cmd.ExecuteReader())
817
                                    dt.Load(dr);
818
                            }
819
                            connection.Close();
820
                        }
821
                    }
822
                    catch (Exception ex)
823
                    {
824
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
825
                    }
826
                    finally
827
                    {
828
                        if (connection != null)
829
                            connection.Dispose();
830
                    }
831
                }
832
            }
833

    
834
            return dt;
835
        }
836

    
837
        public static DataTable SelectProjectLineProperties()
838
        {
839
            DataTable dt = new DataTable();
840
            Project_Info projectInfo = Project_Info.GetInstance();
841
            if (projectInfo.DBType == ID2DB_Type.SQLite)
842
            {
843
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
844
                {
845
                    try
846
                    {
847
                        connection.Open();
848
                        using (SQLiteCommand cmd = connection.CreateCommand())
849
                        {
850
                            cmd.CommandText = string.Format(@"
851
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
852
                            FROM {0} as lp 
853
                                 LEFT OUTER JOIN {1} as sp 
854
                                      ON lp.UID = sp.UID
855
                                 LEFT OUTER JOIN {2} as spa 
856
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
857
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
858
                                dt.Load(dr);
859
                        }
860
                        connection.Close();
861
                    }
862
                    catch (Exception ex)
863
                    {
864
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
865
                    }
866
                    finally
867
                    {
868
                        connection.Dispose();
869
                    }
870
                }
871
            }
872
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
873
            {
874
                using (SqlConnection connection = GetSqlConnection())
875
                {
876
                    try
877
                    {
878
                        if (connection != null && connection.State == ConnectionState.Open)
879
                        {
880
                            using (SqlCommand cmd = connection.CreateCommand())
881
                            {
882
                                cmd.CommandText = string.Format(@"
883
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
884
                            FROM {0} as lp 
885
                                 LEFT OUTER JOIN {1} as sp 
886
                                      ON lp.UID = sp.UID
887
                                 LEFT OUTER JOIN {2} as spa 
888
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
889
                                using (SqlDataReader dr = cmd.ExecuteReader())
890
                                    dt.Load(dr);
891
                            }
892
                            connection.Close();
893
                        }
894
                    }
895
                    catch (Exception ex)
896
                    {
897
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
898
                    }
899
                    finally
900
                    {
901
                        if (connection != null)
902
                            connection.Dispose();
903
                    }
904
                }
905
            }
906

    
907
            return dt;
908
        }
909

    
910
        public static DataTable SelectProjectAttribute()
911
        {
912
            DataTable dt = new DataTable();
913
            Project_Info projectInfo = Project_Info.GetInstance();
914
            if (projectInfo.DBType == ID2DB_Type.SQLite)
915
            {
916
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
917
                {
918
                    try
919
                    {
920
                        connection.Open();
921
                        using (SQLiteCommand cmd = connection.CreateCommand())
922
                        {
923
                            cmd.CommandText = string.Format(@"
924
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spl.LOCATION, spl.LEADERLINE, sa.Property
925
                            FROM {1} as sa, {0} as st 
926
                                 LEFT OUTER JOIN {2} as sp 
927
                                      ON sa.UID = SP.UID 
928
                                LEFT OUTER JOIN {3} as spa 
929
                                     ON sa.UID = spa.UID
930
                                LEFT OUTER JOIN {4} as spl 
931
                                     ON sa.UID = spl.UID
932
                            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);
933
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
934
                                dt.Load(dr);
935
                        }
936
                        connection.Close();
937
                    }
938
                    catch (Exception ex)
939
                    {
940
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
941
                    }
942
                    finally
943
                    {
944
                        connection.Dispose();
945
                    }
946
                }
947
            }
948
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
949
            {
950
                using (SqlConnection connection = GetSqlConnection())
951
                {
952
                    try
953
                    {
954
                        if (connection != null && connection.State == ConnectionState.Open)
955
                        {
956
                            using (SqlCommand cmd = connection.CreateCommand())
957
                            {
958
                                cmd.CommandText = string.Format(@"
959
                            SELECT sa.UID, sa.DisplayAttribute, st.Type, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spl.LOCATION, spl.LEADERLINE, sa.Property
960
                            FROM {1} as sa
961
                                 LEFT OUTER JOIN {2} as sp 
962
                                      ON sa.UID = SP.UID 
963
                                LEFT OUTER JOIN {3} as spa 
964
                                     ON sa.UID = spa.UID
965
                                LEFT OUTER JOIN {4} as spl 
966
                                     ON sa.UID = spl.UID
967
                                LEFT OUTER JOIN {0} as st 
968
                                     ON sa.SymbolType_UID = st.UID 
969
                            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);
970
                                using (SqlDataReader dr = cmd.ExecuteReader())
971
                                    dt.Load(dr);
972
                            }
973
                            connection.Close();
974
                        }
975
                    }
976
                    catch (Exception ex)
977
                    {
978
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
979
                    }
980
                    finally
981
                    {
982
                        if (connection != null)
983
                            connection.Dispose();
984
                    }
985
                }
986
            }
987

    
988

    
989
            return dt;
990
        }
991

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

    
1060
            dt.Merge(SelectDrawingProjectAttribute2());
1061

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

    
1132
            return dt;
1133
        }
1134

    
1135
        public static DataTable SelectID2SymbolTable()
1136
        {
1137
            DataTable dt = new DataTable();
1138
            Project_Info projectInfo = Project_Info.GetInstance();
1139
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1140
            {
1141
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1142
                {
1143
                    try
1144
                    {
1145
                        connection.Open();
1146
                        using (SQLiteCommand cmd = connection.CreateCommand())
1147
                        {
1148
                            cmd.CommandText = @"SELECT * FROM Symbol";
1149
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1150
                                dt.Load(dr);
1151
                        }
1152
                        connection.Close();
1153
                    }
1154
                    catch (Exception ex)
1155
                    {
1156
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1157
                    }
1158
                    finally
1159
                    {
1160
                        connection.Dispose();
1161
                    }
1162
                }
1163
            }
1164
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1165
            {
1166
                using (SqlConnection connection = GetSqlConnection())
1167
                {
1168
                    try
1169
                    {
1170
                        if (connection != null && connection.State == ConnectionState.Open)
1171
                        {
1172
                            using (SqlCommand cmd = connection.CreateCommand())
1173
                            {
1174
                                cmd.CommandText = @"SELECT * FROM Symbol";
1175
                                using (SqlDataReader dr = cmd.ExecuteReader())
1176
                                    dt.Load(dr);
1177
                            }
1178
                            connection.Close();
1179
                        }
1180
                    }
1181
                    catch (Exception ex)
1182
                    {
1183
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1184
                    }
1185
                    finally
1186
                    {
1187
                        if (connection != null)
1188
                            connection.Dispose();
1189
                    }
1190
                }
1191
            }
1192

    
1193
            return dt;
1194
        }
1195

    
1196
        public static DataTable SelectOPCRelations()
1197
        {
1198
            DataTable dt = new DataTable();
1199
            Project_Info projectInfo = Project_Info.GetInstance();
1200
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1201
            {
1202
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1203
                {
1204
                    try
1205
                    {
1206
                        connection.Open();
1207
                        using (SQLiteCommand cmd = connection.CreateCommand())
1208
                        {
1209
                            cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1210
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1211
                                dt.Load(dr);
1212
                        }
1213
                        connection.Close();
1214
                    }
1215
                    catch (Exception ex)
1216
                    {
1217
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1218
                    }
1219
                    finally
1220
                    {
1221
                        connection.Dispose();
1222
                    }
1223
                }
1224
            }
1225
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1226
            {
1227
                using (SqlConnection connection = GetSqlConnection())
1228
                {
1229
                    try
1230
                    {
1231
                        if (connection != null && connection.State == ConnectionState.Open)
1232
                        {
1233
                            using (SqlCommand cmd = connection.CreateCommand())
1234
                            {
1235
                                cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1236
                                using (SqlDataReader dr = cmd.ExecuteReader())
1237
                                    dt.Load(dr);
1238
                            }
1239
                            connection.Close();
1240
                        }
1241
                    }
1242
                    catch (Exception ex)
1243
                    {
1244
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1245
                    }
1246
                    finally
1247
                    {
1248
                        if (connection != null)
1249
                            connection.Dispose();
1250
                    }
1251
                }
1252
            }
1253

    
1254
            return dt;
1255
        }
1256

    
1257
        public static DataTable SelectDrawings()
1258
        {
1259
            DataTable dt = new DataTable();
1260
            Project_Info projectInfo = Project_Info.GetInstance();
1261
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1262
            {
1263
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1264
                {
1265
                    try
1266
                    {
1267
                        connection.Open();
1268
                        using (SQLiteCommand cmd = connection.CreateCommand())
1269
                        {
1270
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1271
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1272
                                dt.Load(dr);
1273
                        }
1274
                        connection.Close();
1275
                    }
1276
                    catch (Exception ex)
1277
                    {
1278
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1279
                    }
1280
                    finally
1281
                    {
1282
                        connection.Dispose();
1283
                    }
1284
                }
1285
            }
1286
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1287
            {
1288
                using (SqlConnection connection = GetSqlConnection())
1289
                {
1290
                    try
1291
                    {
1292
                        if (connection != null && connection.State == ConnectionState.Open)
1293
                        {
1294
                            using (SqlCommand cmd = connection.CreateCommand())
1295
                            {
1296
                                cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1297
                                using (SqlDataReader dr = cmd.ExecuteReader())
1298
                                    dt.Load(dr);
1299
                            }
1300
                            connection.Close();
1301
                        }
1302
                    }
1303
                    catch (Exception ex)
1304
                    {
1305
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1306
                    }
1307
                    finally
1308
                    {
1309
                        if (connection != null)
1310
                            connection.Dispose();
1311
                    }
1312
                }
1313
            }
1314

    
1315
            return dt;
1316
        }
1317

    
1318
        public static DataTable SelectOPCInfo()
1319
        {
1320
            DataTable dt = new DataTable();
1321
            Project_Info projectInfo = Project_Info.GetInstance();
1322
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1323
            {
1324
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1325
                {
1326
                    try
1327
                    {
1328
                        connection.Open();
1329
                        using (SQLiteCommand cmd = connection.CreateCommand())
1330
                        {
1331
                            cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO);
1332
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1333
                                dt.Load(dr);
1334
                        }
1335
                        connection.Close();
1336
                    }
1337
                    catch (Exception ex)
1338
                    {
1339
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1340
                    }
1341
                    finally
1342
                    {
1343
                        connection.Dispose();
1344
                    }
1345
                }
1346
            }
1347
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1348
            {
1349
                using (SqlConnection connection = GetSqlConnection())
1350
                {
1351
                    try
1352
                    {
1353
                        if (connection != null && connection.State == ConnectionState.Open)
1354
                        {
1355
                            using (SqlCommand cmd = connection.CreateCommand())
1356
                            {
1357
                                cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = 0", SPPID_OPC_INFO);
1358
                                using (SqlDataReader dr = cmd.ExecuteReader())
1359
                                    dt.Load(dr);
1360
                            }
1361
                            connection.Close();
1362
                        }
1363
                    }
1364
                    catch (Exception ex)
1365
                    {
1366
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1367
                    }
1368
                    finally
1369
                    {
1370
                        if (connection != null)
1371
                            connection.Dispose();
1372
                    }
1373
                }
1374
            }
1375

    
1376
            return dt;
1377
        }
1378

    
1379
        public static DataTable SelectSymbolType()
1380
        {
1381
            DataTable dt = new DataTable();
1382
            Project_Info projectInfo = Project_Info.GetInstance();
1383
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1384
            {
1385
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1386
                {
1387
                    try
1388
                    {
1389
                        connection.Open();
1390
                        using (SQLiteCommand cmd = connection.CreateCommand())
1391
                        {
1392
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1393
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1394
                                dt.Load(dr);
1395
                        }
1396
                        connection.Close();
1397
                    }
1398
                    catch (Exception ex)
1399
                    {
1400
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1401
                    }
1402
                    finally
1403
                    {
1404
                        connection.Dispose();
1405
                    }
1406
                }
1407
            }
1408
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1409
            {
1410
                using (SqlConnection connection = GetSqlConnection())
1411
                {
1412
                    try
1413
                    {
1414
                        if (connection != null && connection.State == ConnectionState.Open)
1415
                        {
1416
                            using (SqlCommand cmd = connection.CreateCommand())
1417
                            {
1418
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1419
                                using (SqlDataReader dr = cmd.ExecuteReader())
1420
                                    dt.Load(dr);
1421
                            }
1422
                            connection.Close();
1423
                        }
1424
                    }
1425
                    catch (Exception ex)
1426
                    {
1427
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1428
                    }
1429
                    finally
1430
                    {
1431
                        if (connection != null)
1432
                            connection.Dispose();
1433
                    }
1434
                }
1435
            }
1436

    
1437
            return dt;
1438
        }
1439

    
1440
        public static DataTable SelectDrawingInfo()
1441
        {
1442
            DataTable dt = new DataTable();
1443
            Project_Info projectInfo = Project_Info.GetInstance();
1444
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1445
            {
1446
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1447
                {
1448
                    try
1449
                    {
1450
                        connection.Open();
1451
                        using (SQLiteCommand cmd = connection.CreateCommand())
1452
                        {
1453
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1454
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1455
                                dt.Load(dr);
1456
                        }
1457
                        connection.Close();
1458
                    }
1459
                    catch (Exception ex)
1460
                    {
1461
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1462
                    }
1463
                    finally
1464
                    {
1465
                        connection.Dispose();
1466
                    }
1467
                }
1468
            }
1469
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1470
            {
1471
                using (SqlConnection connection = GetSqlConnection())
1472
                {
1473
                    try
1474
                    {
1475
                        if (connection != null && connection.State == ConnectionState.Open)
1476
                        {
1477
                            using (SqlCommand cmd = connection.CreateCommand())
1478
                            {
1479
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1480
                                using (SqlDataReader dr = cmd.ExecuteReader())
1481
                                    dt.Load(dr);
1482
                            }
1483
                            connection.Close();
1484
                        }
1485
                    }
1486
                    catch (Exception ex)
1487
                    {
1488
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1489
                    }
1490
                    finally
1491
                    {
1492
                        if (connection != null)
1493
                            connection.Dispose();
1494
                    }
1495
                }
1496
            }
1497

    
1498
            return dt;
1499
        }
1500

    
1501
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas)
1502
        {
1503
            Project_Info projectInfo = Project_Info.GetInstance();
1504
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1505
            {
1506
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1507
                {
1508
                    try
1509
                    {
1510
                        connection.Open();
1511
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1512
                        {
1513
                            try
1514
                            {
1515
                                using (SQLiteCommand cmd = connection.CreateCommand())
1516
                                {
1517
                                    foreach (var item in datas)
1518
                                    {
1519
                                        cmd.Parameters.Clear();
1520
                                        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);
1521
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1522
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1523
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1524
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1525
                                        cmd.ExecuteNonQuery();
1526
                                    }
1527
                                }
1528
                                transaction.Commit();
1529
                                connection.Close();
1530
                            }
1531
                            catch (Exception ex)
1532
                            {
1533
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1534
                                transaction.Rollback();
1535
                                return false;
1536
                            }
1537
                            finally
1538
                            {
1539
                                transaction.Dispose();
1540
                            }
1541
                        }
1542
                    }
1543
                    catch (Exception ex)
1544
                    {
1545
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1546
                        return false;
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
                                foreach (var item in datas)
1565
                                {
1566
                                    cmd.Parameters.Clear();
1567
                                    cmd.CommandText = string.Format(@"
1568
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1569
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
1570
                                    ELSE
1571
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, item.Item1);
1572
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1573
                                    if (string.IsNullOrEmpty(item.Item2))
1574
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
1575
                                    else
1576
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1577
                                    if (string.IsNullOrEmpty(item.Item3))
1578
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
1579
                                    else
1580
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1581
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1582
                                    cmd.ExecuteNonQuery();
1583
                                }
1584
                            }
1585
                            connection.Close();
1586
                        }
1587
                    }
1588
                    catch (Exception ex)
1589
                    {
1590
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1591
                        return false;
1592
                    }
1593
                    finally
1594
                    {
1595
                        if (connection != null)
1596
                            connection.Dispose();
1597
                    }
1598
                }
1599
            }
1600

    
1601
            return true;
1602
        }
1603

    
1604
        public static bool InsertAttributeMapping(List<Tuple<string, string>> datas)
1605
        {
1606
            Project_Info projectInfo = Project_Info.GetInstance();
1607
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1608
            {
1609
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1610
                {
1611
                    try
1612
                    {
1613
                        connection.Open();
1614
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1615
                        {
1616
                            try
1617
                            {
1618
                                using (SQLiteCommand cmd = connection.CreateCommand())
1619
                                {
1620
                                    foreach (var item in datas)
1621
                                    {
1622
                                        cmd.Parameters.Clear();
1623
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
1624
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1625
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1626
                                        cmd.ExecuteNonQuery();
1627
                                    }
1628
                                }
1629
                                transaction.Commit();
1630
                                connection.Close();
1631
                            }
1632
                            catch (Exception ex)
1633
                            {
1634
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1635
                                transaction.Rollback();
1636
                            }
1637
                            finally
1638
                            {
1639
                                transaction.Dispose();
1640
                            }
1641
                        }
1642
                    }
1643
                    catch (Exception ex)
1644
                    {
1645
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1646
                        return false;
1647
                    }
1648
                    finally
1649
                    {
1650
                        connection.Dispose();
1651
                    }
1652
                }
1653
            }
1654
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1655
            {
1656
                using (SqlConnection connection = GetSqlConnection())
1657
                {
1658
                    try
1659
                    {
1660
                        if (connection != null && connection.State == ConnectionState.Open)
1661
                        {
1662
                            using (SqlCommand cmd = connection.CreateCommand())
1663
                            {
1664
                                foreach (var item in datas)
1665
                                {
1666
                                    cmd.Parameters.Clear();
1667
                                    cmd.CommandText = string.Format(@"
1668
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1669
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE WHERE UID = @UID
1670
                                    ELSE
1671
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE, item.Item1);
1672

    
1673
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1674
                                    if (string.IsNullOrEmpty(item.Item2))
1675
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
1676
                                    else
1677
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1678
                                    cmd.ExecuteNonQuery();
1679
                                }
1680
                            }
1681
                            connection.Close();
1682
                        }
1683
                    }
1684
                    catch (Exception ex)
1685
                    {
1686
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1687
                        return false;
1688
                    }
1689
                    finally
1690
                    {
1691
                        if (connection != null)
1692
                            connection.Dispose();
1693
                    }
1694
                }
1695
            }
1696
            return true;
1697
        }
1698

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

    
1770
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1771
                                    cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
1772
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
1773
                                    cmd.ExecuteNonQuery();
1774
                                }
1775
                            }
1776
                            connection.Close();
1777
                        }
1778
                    }
1779
                    catch (Exception ex)
1780
                    {
1781
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1782
                        return false;
1783
                    }
1784
                    finally
1785
                    {
1786
                        if (connection != null)
1787
                            connection.Dispose();
1788
                    }
1789
                }
1790
            }
1791
            return true;
1792
        }
1793

    
1794
        public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document)
1795
        {
1796
            Project_Info projectInfo = Project_Info.GetInstance();
1797
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1798
            {
1799
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1800
                {
1801
                    try
1802
                    {
1803
                        connection.Open();
1804
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1805
                        {
1806
                            try
1807
                            {
1808
                                using (SQLiteCommand cmd = connection.CreateCommand())
1809
                                {
1810
                                    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);
1811
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1812
                                    cmd.Parameters.AddWithValue("@PATH", path);
1813
                                    cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1814
                                    cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1815
                                    cmd.ExecuteNonQuery();
1816
                                }
1817

    
1818
                                List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1819
                                foreach (var item in OPCs)
1820
                                {
1821
                                    using (SQLiteCommand cmd = connection.CreateCommand())
1822
                                    {
1823
                                        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);
1824
                                        cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1825
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1826
                                        cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1827
                                        cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1828
                                        cmd.Parameters.AddWithValue("@PAIRED", false);
1829
                                        cmd.ExecuteNonQuery();
1830
                                    }
1831
                                }
1832

    
1833
                                transaction.Commit();
1834
                                connection.Close();
1835
                            }
1836
                            catch (Exception ex)
1837
                            {
1838
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1839
                                transaction.Rollback();
1840
                                return false;
1841
                            }
1842
                            finally
1843
                            {
1844
                                transaction.Dispose();
1845
                            }
1846
                        }
1847
                    }
1848
                    catch (Exception ex)
1849
                    {
1850
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1851
                        return false;
1852
                    }
1853
                    finally
1854
                    {
1855
                        connection.Dispose();
1856
                    }
1857
                }
1858
            }
1859
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1860
            {
1861
                using (SqlConnection connection = GetSqlConnection())
1862
                {
1863
                    try
1864
                    {
1865
                        if (connection != null && connection.State == ConnectionState.Open)
1866
                        {
1867
                            using (SqlCommand cmd = connection.CreateCommand())
1868
                            {
1869
                                cmd.Parameters.Clear();
1870
                                cmd.CommandText = string.Format(@"
1871
                                    IF EXISTS (SELECT * FROM {0} WHERE ID2_DRAWING_UID = '{1}')
1872
                                        UPDATE {0} SET PATH = @PATH, DRAWINGNUMBER = @DRAWINGNUMBER, DRAWINGNAME = @DRAWINGNAME WHERE ID2_DRAWING_UID = @ID2_DRAWING_UID
1873
                                    ELSE
1874
                                        INSERT INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO, document.UID);
1875

    
1876
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1877
                                cmd.Parameters.AddWithValue("@PATH", path);
1878
                                cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1879
                                cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1880
                                cmd.ExecuteNonQuery();
1881
                            }
1882

    
1883
                            List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1884
                            foreach (var item in OPCs)
1885
                            {
1886
                                using (SqlCommand cmd = connection.CreateCommand())
1887
                                {
1888
                                    cmd.Parameters.Clear();
1889
                                    cmd.CommandText = string.Format(@"
1890
                                    IF EXISTS (SELECT * FROM {0} WHERE ID2_OPC_UID = '{1}')
1891
                                        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
1892
                                    ELSE
1893
                                        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);
1894

    
1895
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1896
                                    if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
1897
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value);
1898
                                    else
1899
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1900
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1901
                                    cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1902
                                    cmd.Parameters.AddWithValue("@PAIRED", false);
1903
                                    cmd.ExecuteNonQuery();
1904
                                }
1905
                            }
1906
                            connection.Close();
1907
                        }
1908
                    }
1909
                    catch (Exception ex)
1910
                    {
1911
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1912
                        return false;
1913
                    }
1914
                    finally
1915
                    {
1916
                        if (connection != null)
1917
                            connection.Dispose();
1918
                    }
1919
                }
1920
            }
1921
            return true;
1922
        }
1923

    
1924
        public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired)
1925
        {
1926
            Project_Info projectInfo = Project_Info.GetInstance();
1927
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1928
            {
1929
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
1930
                {
1931
                    try
1932
                    {
1933
                        connection.Open();
1934
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1935
                        {
1936
                            try
1937
                            {
1938
                                using (SQLiteCommand cmd = connection.CreateCommand())
1939
                                {
1940
                                    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);
1941
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
1942
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
1943
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
1944
                                    cmd.Parameters.AddWithValue("@PAIRED", Paired);
1945
                                    cmd.ExecuteNonQuery();
1946
                                }
1947

    
1948
                                transaction.Commit();
1949
                                connection.Close();
1950
                            }
1951
                            catch (Exception ex)
1952
                            {
1953
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1954
                                transaction.Rollback();
1955
                                return false;
1956
                            }
1957
                            finally
1958
                            {
1959
                                transaction.Dispose();
1960
                            }
1961
                        }
1962
                    }
1963
                    catch (Exception ex)
1964
                    {
1965
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1966
                        return false;
1967
                    }
1968
                    finally
1969
                    {
1970
                        connection.Dispose();
1971
                    }
1972
                }
1973
            }
1974
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1975
            {
1976
                using (SqlConnection connection = GetSqlConnection())
1977
                {
1978
                    try
1979
                    {
1980
                        if (connection != null && connection.State == ConnectionState.Open)
1981
                        {
1982
                            using (SqlCommand cmd = connection.CreateCommand())
1983
                            {
1984
                                cmd.Parameters.Clear();
1985
                                cmd.CommandText = string.Format(@"
1986
                                    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);
1987
                                cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
1988
                                if (string.IsNullOrEmpty(ModelItemID))
1989
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value);
1990
                                else
1991
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
1992
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
1993
                                cmd.Parameters.AddWithValue("@PAIRED", Paired);
1994
                                cmd.ExecuteNonQuery();
1995
                            }
1996
                            connection.Close();
1997
                        }
1998
                    }
1999
                    catch (Exception ex)
2000
                    {
2001
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2002
                        return false;
2003
                    }
2004
                    finally
2005
                    {
2006
                        if (connection != null)
2007
                            connection.Dispose();
2008
                    }
2009
                }
2010
            }
2011
            return true;
2012
        }
2013

    
2014
        public static bool ExportMappingData()
2015
        {
2016
            bool result = true;
2017
            try
2018
            {
2019
                DataSet dataSet = new DataSet();
2020
                DataTable symbolMappingDT = GetTable(SPPID_SYMBOL_MAPPING_TABLE);
2021
                if (symbolMappingDT != null)
2022
                {
2023
                    symbolMappingDT.TableName = SPPID_SYMBOL_MAPPING_TABLE;
2024
                    dataSet.Tables.Add(symbolMappingDT);
2025
                }
2026
                else
2027
                    result = false;
2028

    
2029
                DataTable attributeMappingDT = GetTable(SPPID_ATTRIBUTE_MAPPING_TABLE);
2030
                if (attributeMappingDT != null)
2031
                {
2032
                    attributeMappingDT.TableName = SPPID_ATTRIBUTE_MAPPING_TABLE;
2033
                    dataSet.Tables.Add(attributeMappingDT);
2034
                }
2035
                else
2036
                    result = false;
2037

    
2038
                if (result)
2039
                {
2040
                    string text = JsonConvert.SerializeObject(dataSet);
2041
                    using (StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf", false))
2042
                    {
2043
                        sw.Write(text);
2044
                        sw.Close();
2045
                        sw.Dispose();
2046
                    }
2047
                }
2048
            }
2049
            catch (Exception ex)
2050
            {
2051
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2052
                result = false;
2053
            }
2054

    
2055
            return result;
2056
        }
2057

    
2058
        public static bool ImportMappingData()
2059
        {
2060
            bool result = true;
2061
            try
2062
            {
2063
                string sJson = string.Empty;
2064
                using (StreamReader sw = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf"))
2065
                {
2066
                    sJson = sw.ReadToEnd();
2067
                    sw.Close();
2068
                    sw.Dispose();
2069
                }
2070

    
2071
                DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(sJson);
2072
                DataTable symbolMappingDT = dataSet.Tables[SPPID_SYMBOL_MAPPING_TABLE];
2073
                if (symbolMappingDT == null || !ImportSymbolMappingTable(symbolMappingDT))
2074
                    result = false;
2075

    
2076
                DataTable attributeMappingDT = dataSet.Tables[SPPID_ATTRIBUTE_MAPPING_TABLE];
2077
                if (attributeMappingDT == null || !ImportAttributeMappingTable(attributeMappingDT))
2078
                    result = false;
2079
            }
2080
            catch (Exception ex)
2081
            {
2082
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2083
                result = false;
2084
            }
2085

    
2086
            return result;
2087
        }
2088

    
2089
        private static DataTable GetTable(string tableName)
2090
        {
2091
            DataTable dt = new DataTable();
2092
            Project_Info projectInfo = Project_Info.GetInstance();
2093
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2094
            {
2095
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
2096
                {
2097
                    try
2098
                    {
2099
                        connection.Open();
2100
                        using (SQLiteCommand cmd = connection.CreateCommand())
2101
                        {
2102
                            cmd.CommandText = string.Format("SELECT * FROM {0}", tableName);
2103
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
2104
                                dt.Load(dr);
2105
                        }
2106
                        connection.Close();
2107
                    }
2108
                    catch (Exception ex)
2109
                    {
2110
                        dt = null;
2111
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2112
                    }
2113
                    finally
2114
                    {
2115
                        connection.Dispose();
2116
                    }
2117
                }
2118
            }
2119
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2120
            {
2121
                using (SqlConnection connection = GetSqlConnection())
2122
                {
2123
                    try
2124
                    {
2125
                        if (connection != null && connection.State == ConnectionState.Open)
2126
                        {
2127
                            using (SqlCommand cmd = connection.CreateCommand())
2128
                            {
2129
                                cmd.CommandText = string.Format("SELECT * FROM {0}", tableName);
2130
                                using (SqlDataReader dr = cmd.ExecuteReader())
2131
                                    dt.Load(dr);
2132
                            }
2133
                            connection.Close();
2134
                        }
2135
                    }
2136
                    catch (Exception ex)
2137
                    {
2138
                        dt = null;
2139
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2140
                    }
2141
                    finally
2142
                    {
2143
                        if (connection != null)
2144
                            connection.Dispose();
2145
                    }
2146
                }
2147
            }
2148

    
2149
            return dt;
2150
        }
2151

    
2152
        private static bool ImportSymbolMappingTable(DataTable dt)
2153
        {
2154
            bool result = false;
2155

    
2156
            Project_Info projectInfo = Project_Info.GetInstance();
2157
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2158
            {
2159
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
2160
                {
2161
                    try
2162
                    {
2163
                        connection.Open();
2164
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2165
                        {
2166
                            try
2167
                            {
2168
                                using (SQLiteCommand cmd = connection.CreateCommand())
2169
                                {
2170
                                    foreach (DataRow item in dt.Rows)
2171
                                    {
2172
                                        string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2173
                                        string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"];
2174
                                        string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"];
2175
                                        bool LEADERLINE = Convert.ToBoolean(item["LEADERLINE"]);
2176

    
2177
                                        cmd.Parameters.Clear();
2178
                                        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);
2179
                                        cmd.Parameters.AddWithValue("@UID", UID);
2180
                                        cmd.Parameters.AddWithValue("@NAME", NAME);
2181
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH);
2182
                                        cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE);
2183
                                        cmd.ExecuteNonQuery();
2184
                                    }
2185
                                }
2186
                                transaction.Commit();
2187
                                connection.Close();
2188
                                result = true;
2189
                            }
2190
                            catch (Exception ex)
2191
                            {
2192
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2193
                                transaction.Rollback();
2194
                            }
2195
                            finally
2196
                            {
2197
                                transaction.Dispose();
2198
                            }
2199
                        }
2200
                    }
2201
                    catch (Exception ex)
2202
                    {
2203
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2204
                    }
2205
                    finally
2206
                    {
2207
                        connection.Dispose();
2208
                    }
2209
                }
2210
            }
2211
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2212
            {
2213
                using (SqlConnection connection = GetSqlConnection())
2214
                {
2215
                    try
2216
                    {
2217
                        if (connection != null && connection.State == ConnectionState.Open)
2218
                        {
2219
                            using (SqlCommand cmd = connection.CreateCommand())
2220
                            {
2221
                                foreach (DataRow item in dt.Rows)
2222
                                {
2223
                                    string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2224
                                    string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"];
2225
                                    string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"];
2226
                                    bool LEADERLINE = DBNull.Value.Equals(item["LEADERLINE"]) ? false : Convert.ToBoolean(item["LEADERLINE"]);
2227

    
2228
                                    cmd.Parameters.Clear();
2229
                                    cmd.CommandText = string.Format(@"
2230
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2231
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
2232
                                    ELSE
2233
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, UID);
2234
                                    cmd.Parameters.AddWithValue("@UID", UID);
2235
                                    if (string.IsNullOrEmpty(NAME))
2236
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
2237
                                    else
2238
                                        cmd.Parameters.AddWithValue("@NAME", NAME);
2239
                                    if (string.IsNullOrEmpty(SPPID_SYMBOL_PATH))
2240
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
2241
                                    else
2242
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH);
2243
                                    cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE);
2244
                                    cmd.ExecuteNonQuery();
2245
                                }
2246
                            }
2247
                            connection.Close();
2248
                            result = true;
2249
                        }
2250
                    }
2251
                    catch (Exception ex)
2252
                    {
2253
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2254
                    }
2255
                    finally
2256
                    {
2257
                        if (connection != null)
2258
                            connection.Dispose();
2259
                    }
2260
                }
2261
            }
2262

    
2263
            return result;
2264
        }
2265

    
2266
        private static bool ImportAttributeMappingTable(DataTable dt)
2267
        {
2268
            bool result = false;
2269

    
2270
            Project_Info projectInfo = Project_Info.GetInstance();
2271
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2272
            {
2273
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
2274
                {
2275
                    try
2276
                    {
2277
                        connection.Open();
2278
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2279
                        {
2280
                            try
2281
                            {
2282
                                using (SQLiteCommand cmd = connection.CreateCommand())
2283
                                {
2284
                                    foreach (DataRow item in dt.Rows)
2285
                                    {
2286
                                        string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2287
                                        string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"];
2288

    
2289
                                        cmd.Parameters.Clear();
2290
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
2291
                                        cmd.Parameters.AddWithValue("@UID", UID);
2292
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE);
2293
                                        cmd.ExecuteNonQuery();
2294
                                    }
2295
                                }
2296
                                transaction.Commit();
2297
                                connection.Close();
2298
                                result = true;
2299
                            }
2300
                            catch (Exception ex)
2301
                            {
2302
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2303
                                transaction.Rollback();
2304
                            }
2305
                            finally
2306
                            {
2307
                                transaction.Dispose();
2308
                            }
2309
                        }
2310
                    }
2311
                    catch (Exception ex)
2312
                    {
2313
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2314
                    }
2315
                    finally
2316
                    {
2317
                        connection.Dispose();
2318
                    }
2319
                }
2320
            }
2321
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2322
            {
2323
                using (SqlConnection connection = GetSqlConnection())
2324
                {
2325
                    try
2326
                    {
2327
                        if (connection != null && connection.State == ConnectionState.Open)
2328
                        {
2329
                            using (SqlCommand cmd = connection.CreateCommand())
2330
                            {
2331
                                foreach (DataRow item in dt.Rows)
2332
                                {
2333
                                    string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2334
                                    string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"];
2335

    
2336
                                    cmd.Parameters.Clear();
2337
                                    cmd.CommandText = string.Format(@"
2338
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2339
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE WHERE UID = @UID
2340
                                    ELSE
2341
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE, UID);
2342

    
2343
                                    cmd.Parameters.AddWithValue("@UID", UID);
2344
                                    if (string.IsNullOrEmpty(SPPID_ATTRIBUTE))
2345
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
2346
                                    else
2347
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE);
2348
                                    cmd.ExecuteNonQuery();
2349
                                }
2350
                            }
2351
                            connection.Close();
2352
                            result = true;
2353
                        }
2354
                    }
2355
                    catch (Exception ex)
2356
                    {
2357
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2358
                    }
2359
                    finally
2360
                    {
2361
                        if (connection != null)
2362
                            connection.Dispose();
2363
                    }
2364
                }
2365
            }
2366

    
2367

    
2368

    
2369
            return result;
2370
        }
2371
    }
2372
}
클립보드 이미지 추가 (최대 크기: 500 MB)