프로젝트

일반

사용자정보

통계
| 개정판:

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

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

1 b18dc619 gaqhf
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Threading.Tasks;
6 fab4f207 gaqhf
using System.Globalization;
7
using System.Data.SQLite;
8
using System.Data;
9 8847ea67 gaqhf
using System.Data.SqlClient;
10 2425d530 gaqhf
using Converter.SPPID;
11
using System.IO;
12 224535bb gaqhf
using Newtonsoft.Json;
13 82ab5276 gaqhf
using Converter.SPPID.Model;
14 b18dc619 gaqhf
15 171142c5 gaqhf
namespace Converter.BaseModel
16 b18dc619 gaqhf
{
17 171142c5 gaqhf
    public class Project_DB
18 b18dc619 gaqhf
    {
19 bca86986 gaqhf
        const string SPPID_DB_INFO_TABLE = "T_SPPID_CONNECTION_INFO";
20
        const string SPPID_SYMBOL_MAPPING_TABLE = "T_SPPID_SYMBOL_MAPPING";
21
        const string SPPID_ATTRIBUTE_MAPPING_TABLE = "T_SPPID_ATTRIBUTE_MAPPING";
22 7cbb1038 gaqhf
        const string SPPID_SETTING_TABLE = "T_SPPID_SETTING_TABLE";
23 b8bd98aa gaqhf
        const string SPPID_LABEL_INFO_TABLE = "T_SPPID_LABEL_INFO";
24 e8536f2b gaqhf
        const string SPPID_DRAWING_INFO = "T_SPPID_DRAWING_INFO";
25 154d8f43 gaqhf
        const string SPPID_OPC_INFO = "T_SPPID_OPC_INFO";
26 306a0af9 gaqhf
        const string ID2_DRAWINGATTRIBUTE_TABLE = "T_ID2_DRAWINGATTRIBUTE";
27 c9264d17 gaqhf
        const string ID2_NOMINALDIAMETER_TABLE = "NominalDiameter";
28 171142c5 gaqhf
29 bca86986 gaqhf
        const string LineProperties_TABLE = "LineProperties";
30
        const string LineTypes_TABLE = "LineTypes";
31
        const string SymbolType_TABLE = "SymbolType";
32
        const string SymbolAttribute_TABLE = "SymbolAttribute";
33
        const string Symbol_TABLE = "Symbol";
34 154d8f43 gaqhf
        const string OPCRelations_TABLE = "OPCRelations";
35 306a0af9 gaqhf
        const string DrawingAttribute_Table = "TitleBlockProperties";
36 171142c5 gaqhf
37 8847ea67 gaqhf
        private static SqlConnection GetSqlConnection()
38 fab4f207 gaqhf
        {
39
            Project_Info projectInfo = Project_Info.GetInstance();
40 8847ea67 gaqhf
            SqlConnection connection = null;
41 fab4f207 gaqhf
            try
42
            {
43 8847ea67 gaqhf
                connection = new SqlConnection(string.Format(CultureInfo.CurrentCulture,
44
                    @"Data Source = {0},{1}; Initial Catalog = {2}; User ID = {3}; Password = {4};",
45
                    projectInfo.ServerIP,
46
                    projectInfo.Port,
47
                    System.IO.Path.GetFileName(projectInfo.DefaultPath),
48
                    projectInfo.DBUser,
49
                    projectInfo.DBPassword));
50
51 fab4f207 gaqhf
                connection.Open();
52
            }
53
            catch (Exception ex)
54
            {
55 2425d530 gaqhf
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
56 da84b14b gaqhf
                if (connection != null)
57
                    connection.Dispose();
58 8847ea67 gaqhf
                connection = null;
59 fab4f207 gaqhf
            }
60
61 8847ea67 gaqhf
            return connection;
62 fab4f207 gaqhf
        }
63
64 8847ea67 gaqhf
        public static bool ConnTestAndCreateTable()
65 fab4f207 gaqhf
        {
66 8847ea67 gaqhf
            bool result = false;
67 fab4f207 gaqhf
            Project_Info projectInfo = Project_Info.GetInstance();
68 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
69 fab4f207 gaqhf
            {
70 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath), true))
71 fab4f207 gaqhf
                {
72 8847ea67 gaqhf
                    try
73 fab4f207 gaqhf
                    {
74 8847ea67 gaqhf
                        connection.Open();
75
                        if (connection.State == ConnectionState.Open)
76
                        {
77
                            using (SQLiteCommand cmd = connection.CreateCommand())
78
                            {
79
                                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
80
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
81
                                using (DataTable dt = new DataTable())
82
                                {
83
                                    dt.Load(dr);
84
85
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
86
                                    {
87
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT)", SPPID_DB_INFO_TABLE);
88
                                        cmd.ExecuteNonQuery();
89
                                    }
90
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
91
                                    {
92
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString TEXT, SettingType TEXT)", SPPID_SETTING_TABLE);
93
                                        cmd.ExecuteNonQuery();
94
                                    }
95
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
96
                                    {
97
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, SPPID_SYMBOL_PATH TEXT, LEADERLINE BOOLEAN)", SPPID_SYMBOL_MAPPING_TABLE);
98
                                        cmd.ExecuteNonQuery();
99
                                    }
100
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
101
                                    {
102
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, SPPID_ATTRIBUTE TEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
103
                                        cmd.ExecuteNonQuery();
104
                                    }
105
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
106
                                    {
107
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BOOLEAN)", SPPID_LABEL_INFO_TABLE);
108
                                        cmd.ExecuteNonQuery();
109
                                    }
110
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
111
                                    {
112
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
113
                                        cmd.ExecuteNonQuery();
114
                                    }
115
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
116
                                    {
117
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID TEXT PRIMARY KEY, SPPID_OPC_MODELITEM_ID TEXT, ID2_DRAWING_UID TEXT, ATTRIBUTES TEXT, PAIRED BOOL)", SPPID_OPC_INFO);
118
                                        cmd.ExecuteNonQuery();
119
                                    }
120 306a0af9 gaqhf
                                    if (dt.Select(string.Format("NAME = '{0}'", ID2_DRAWINGATTRIBUTE_TABLE)).Length == 0)
121
                                    {
122
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT)", ID2_DRAWINGATTRIBUTE_TABLE);
123
                                        cmd.ExecuteNonQuery();
124
125
                                        List<string> datas = new List<string>() { "Unit", "Rev No", "Drawing No" };
126
                                        foreach (var data in datas)
127
                                        {
128
                                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES('{1}', '{1}')", ID2_DRAWINGATTRIBUTE_TABLE, data);
129
                                            cmd.ExecuteNonQuery();
130
                                        }
131
                                    }
132 8847ea67 gaqhf
                                }
133
134
                                #region Check Column 업데이트시 예비용
135 93c417a9 gaqhf
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_ATTRIBUTE_MAPPING_TABLE);
136
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
137
                                using (DataTable dt = new DataTable())
138
                                {
139
                                    dt.Load(dr);
140
                                    if (!dt.Columns.Contains("ISTEXT"))
141
                                    {
142
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN ISTEXT BOOLEAN", SPPID_ATTRIBUTE_MAPPING_TABLE);
143
                                        cmd.ExecuteNonQuery();
144
                                    }
145
                                }
146
147 8847ea67 gaqhf
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
148
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
149
                                using (DataTable dt = new DataTable())
150
                                {
151
                                    dt.Load(dr);
152
                                    if (!dt.Columns.Contains("LEADERLINE"))
153
                                    {
154
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN LEADERLINE BOOLEAN", SPPID_SYMBOL_MAPPING_TABLE);
155
                                        cmd.ExecuteNonQuery();
156
                                    }
157
                                }
158
159
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
160
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
161
                                using (DataTable dt = new DataTable())
162
                                {
163
                                    dt.Load(dr);
164
                                    if (!dt.Columns.Contains("SettingType"))
165
                                    {
166
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD COLUMN SettingType Text", SPPID_SETTING_TABLE);
167
                                        cmd.ExecuteNonQuery();
168
                                    }
169
                                }
170 fab4f207 gaqhf
171 8847ea67 gaqhf
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
172
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
173
                                using (DataTable dt = new DataTable())
174
                                {
175
                                    dt.Load(dr);
176
                                    if (dt.Columns.Contains("DOCUMENT"))
177
                                    {
178
                                        cmd.CommandText = string.Format("DROP TABLE {0}", SPPID_DRAWING_INFO);
179
                                        cmd.ExecuteNonQuery();
180
181
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID Text PRIMARY KEY, PATH TEXT, DRAWINGNUMBER TEXT, DRAWINGNAME TEXT)", SPPID_DRAWING_INFO);
182
                                        cmd.ExecuteNonQuery();
183
                                    }
184
                                }
185
                                #endregion
186
                            }
187
                            result = true;
188
                        }
189
                        connection.Close();
190
                    }
191
                    catch (Exception ex)
192
                    {
193 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
194 8847ea67 gaqhf
                    }
195
                    finally
196
                    {
197
                        connection.Dispose();
198 fab4f207 gaqhf
                    }
199
                }
200
            }
201 8847ea67 gaqhf
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
202 fab4f207 gaqhf
            {
203 8847ea67 gaqhf
                using (SqlConnection connection = GetSqlConnection())
204 fab4f207 gaqhf
                {
205 8847ea67 gaqhf
                    try
206 fab4f207 gaqhf
                    {
207 8847ea67 gaqhf
                        if (connection != null && connection.State == ConnectionState.Open)
208
                        {
209
                            using (SqlCommand cmd = connection.CreateCommand())
210
                            {
211
                                cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES";
212
                                using (SqlDataReader dr = cmd.ExecuteReader())
213
                                using (DataTable dt = new DataTable())
214
                                {
215
                                    dt.Load(dr);
216
217
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DB_INFO_TABLE)).Length == 0)
218
                                    {
219 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX))", SPPID_DB_INFO_TABLE);
220 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
221
                                    }
222
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SETTING_TABLE)).Length == 0)
223
                                    {
224 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (JsonString varchar(MAX), SettingType varchar(MAX))", SPPID_SETTING_TABLE);
225 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
226
                                    }
227
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_SYMBOL_MAPPING_TABLE)).Length == 0)
228
                                    {
229 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(MAX), SPPID_SYMBOL_PATH varchar(MAX), LEADERLINE BIT)", SPPID_SYMBOL_MAPPING_TABLE);
230 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
231
                                    }
232
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_ATTRIBUTE_MAPPING_TABLE)).Length == 0)
233
                                    {
234 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, SPPID_ATTRIBUTE varchar(MAX))", SPPID_ATTRIBUTE_MAPPING_TABLE);
235 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
236
                                    }
237
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_LABEL_INFO_TABLE)).Length == 0)
238
                                    {
239
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, LOCATION INT DEFAULT 0, LEADERLINE BIT)", SPPID_LABEL_INFO_TABLE);
240
                                        cmd.ExecuteNonQuery();
241
                                    }
242
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_DRAWING_INFO)).Length == 0)
243
                                    {
244 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_DRAWING_UID varchar(255) PRIMARY KEY, PATH varchar(MAX), DRAWINGNUMBER varchar(MAX), DRAWINGNAME varchar(MAX))", SPPID_DRAWING_INFO);
245 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
246
                                    }
247
                                    if (dt.Select(string.Format("NAME = '{0}'", SPPID_OPC_INFO)).Length == 0)
248
                                    {
249 d2a7bef1 gaqhf
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (ID2_OPC_UID varchar(255) PRIMARY KEY, SPPID_OPC_MODELITEM_ID varchar(MAX), ID2_DRAWING_UID varchar(MAX), ATTRIBUTES varchar(MAX), PAIRED BIT)", SPPID_OPC_INFO);
250 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
251
                                    }
252 306a0af9 gaqhf
                                    if (dt.Select(string.Format("NAME = '{0}'", ID2_DRAWINGATTRIBUTE_TABLE)).Length == 0)
253
                                    {
254
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(255))", ID2_DRAWINGATTRIBUTE_TABLE);
255
                                        cmd.ExecuteNonQuery();
256
257
                                        List<string> datas = new List<string>() { "Unit", "Rev No", "Drawing No" };
258
                                        foreach (var data in datas)
259
                                        {
260
                                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES('{1}', '{1}')", ID2_DRAWINGATTRIBUTE_TABLE, data);
261
                                            cmd.ExecuteNonQuery();
262
                                        }
263
                                    }
264 8847ea67 gaqhf
                                }
265
266
                                #region Check Column 업데이트시 예비용
267 923501a9 gaqhf
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_ATTRIBUTE_MAPPING_TABLE);
268
                                using (SqlDataReader dr = cmd.ExecuteReader())
269
                                using (DataTable dt = new DataTable())
270
                                {
271
                                    dt.Load(dr);
272
                                    if (!dt.Columns.Contains("ISTEXT"))
273
                                    {
274
                                        cmd.CommandText = string.Format("ALTER TABLE {0} ADD ISTEXT BIT", SPPID_ATTRIBUTE_MAPPING_TABLE);
275
                                        cmd.ExecuteNonQuery();
276
                                    }
277
                                }
278 8847ea67 gaqhf
                                #endregion
279
                            }
280
                            result = true;
281
                        }
282 fab4f207 gaqhf
                    }
283 8847ea67 gaqhf
                    catch (Exception ex)
284
                    {
285 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
286 8847ea67 gaqhf
                    }
287
                    finally
288
                    {
289
                        if (connection != null)
290
                            connection.Dispose();
291
                    }
292 fab4f207 gaqhf
                }
293
            }
294
295 8847ea67 gaqhf
            return result;
296 fab4f207 gaqhf
        }
297
298 8847ea67 gaqhf
        public static bool SaveSPPID_DB_INFO(string jsonString)
299 e00e891d gaqhf
        {
300
            Project_Info projectInfo = Project_Info.GetInstance();
301 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
302 e00e891d gaqhf
            {
303 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
304 e00e891d gaqhf
                {
305
306 8847ea67 gaqhf
                    try
307
                    {
308
                        connection.Open();
309
                        using (SQLiteCommand cmd = connection.CreateCommand())
310 7cbb1038 gaqhf
                        {
311 8847ea67 gaqhf
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
312
                            cmd.ExecuteNonQuery();
313
314
                            cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
315
                            cmd.Parameters.AddWithValue("@jsonString", jsonString);
316 7cbb1038 gaqhf
                            cmd.ExecuteNonQuery();
317
                        }
318 8847ea67 gaqhf
                        connection.Close();
319
                    }
320
                    catch (Exception ex)
321
                    {
322 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
323 8847ea67 gaqhf
                        return false;
324
                    }
325
                    finally
326
                    {
327
                        connection.Dispose();
328 e00e891d gaqhf
                    }
329
                }
330 8847ea67 gaqhf
            }
331
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
332
            {
333
                using (SqlConnection connection = GetSqlConnection())
334 e00e891d gaqhf
                {
335 8847ea67 gaqhf
                    try
336
                    {
337
                        if (connection != null && connection.State == ConnectionState.Open)
338
                        {
339 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
340
                            {
341
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_DB_INFO_TABLE);
342
                                cmd.ExecuteNonQuery();
343 8847ea67 gaqhf
344 d2a7bef1 gaqhf
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString)", SPPID_DB_INFO_TABLE);
345
                                cmd.Parameters.AddWithValue("@jsonString", jsonString);
346
                                cmd.ExecuteNonQuery();
347
                            }
348
                            connection.Close();
349
                        }
350
                        else
351
                        {
352
                            return false;
353 8847ea67 gaqhf
                        }
354
                    }
355
                    catch (Exception ex)
356
                    {
357 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
358 d2a7bef1 gaqhf
                        return false;
359 8847ea67 gaqhf
                    }
360
                    finally
361
                    {
362
                        if (connection != null)
363
                            connection.Dispose();
364
                    }
365 e00e891d gaqhf
                }
366
            }
367
368 8847ea67 gaqhf
369 e00e891d gaqhf
            return true;
370
        }
371
372 8847ea67 gaqhf
        public static DataTable SelectSPPID_DB_INFO()
373 e00e891d gaqhf
        {
374
            DataTable dt = new DataTable();
375
            Project_Info projectInfo = Project_Info.GetInstance();
376 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
377 e00e891d gaqhf
            {
378 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
379 e00e891d gaqhf
                {
380 8847ea67 gaqhf
                    try
381 e00e891d gaqhf
                    {
382 8847ea67 gaqhf
                        connection.Open();
383
                        using (SQLiteCommand cmd = connection.CreateCommand())
384
                        {
385
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
386
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
387
                                dt.Load(dr);
388
                        }
389
                        connection.Close();
390 e00e891d gaqhf
                    }
391 8847ea67 gaqhf
                    catch (Exception ex)
392
                    {
393 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
394 8847ea67 gaqhf
                    }
395
                    finally
396
                    {
397
                        connection.Dispose();
398
                    }
399 e00e891d gaqhf
                }
400 8847ea67 gaqhf
            }
401
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
402
            {
403
                using (SqlConnection connection = GetSqlConnection())
404 e00e891d gaqhf
                {
405 8847ea67 gaqhf
                    try
406
                    {
407
                        if (connection != null && connection.State == ConnectionState.Open)
408
                        {
409 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
410
                            {
411
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DB_INFO_TABLE);
412
                                using (SqlDataReader dr = cmd.ExecuteReader())
413
                                    dt.Load(dr);
414
                            }
415
                            connection.Close();
416 8847ea67 gaqhf
                        }
417
                    }
418
                    catch (Exception ex)
419
                    {
420 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
421 8847ea67 gaqhf
                    }
422
                    finally
423
                    {
424
                        if (connection != null)
425
                            connection.Dispose();
426
                    }
427 e00e891d gaqhf
                }
428
            }
429
430
            return dt;
431
        }
432
433 8847ea67 gaqhf
        public static bool SaveETCSetting(Dictionary<string,string> dicSetting)
434 fab4f207 gaqhf
        {
435 8847ea67 gaqhf
            Project_Info projectInfo = Project_Info.GetInstance();
436
            if (projectInfo.DBType == ID2DB_Type.SQLite)
437 fab4f207 gaqhf
            {
438 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
439 fab4f207 gaqhf
                {
440
441 8847ea67 gaqhf
                    try
442 e00e891d gaqhf
                    {
443 8847ea67 gaqhf
                        connection.Open();
444
                        using (SQLiteCommand cmd = connection.CreateCommand())
445
                        {
446
                            cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
447
                            cmd.ExecuteNonQuery();
448
449
                            foreach (var item in dicSetting)
450
                            {
451
                                cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
452 f4485671 gaqhf
                                cmd.Parameters.Clear();
453 8847ea67 gaqhf
                                cmd.Parameters.AddWithValue("@jsonString", item.Value);
454
                                cmd.Parameters.AddWithValue("@SettingType", item.Key);
455
                                cmd.ExecuteNonQuery();
456
                            }
457
                        }
458
                        connection.Close();
459 e00e891d gaqhf
                    }
460 8847ea67 gaqhf
                    catch (Exception ex)
461 bca86986 gaqhf
                    {
462 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
463 8847ea67 gaqhf
                        return false;
464 bca86986 gaqhf
                    }
465 8847ea67 gaqhf
                    finally
466 bca86986 gaqhf
                    {
467 8847ea67 gaqhf
                        connection.Dispose();
468 bca86986 gaqhf
                    }
469 8847ea67 gaqhf
                }
470
            }
471
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
472
            {
473
                using (SqlConnection connection = GetSqlConnection())
474
                {
475
                    try
476 1a3a74a8 gaqhf
                    {
477 8847ea67 gaqhf
                        if (connection != null && connection.State == ConnectionState.Open)
478
                        {
479 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
480
                            {
481
                                cmd.CommandText = string.Format("DELETE FROM {0}", SPPID_SETTING_TABLE);
482
                                cmd.ExecuteNonQuery();
483 8847ea67 gaqhf
484 d2a7bef1 gaqhf
                                foreach (var item in dicSetting)
485
                                {
486
                                    cmd.CommandText = string.Format("INSERT INTO {0} VALUES (@jsonString, @SettingType)", SPPID_SETTING_TABLE);
487 f4485671 gaqhf
                                    cmd.Parameters.Clear();
488 d2a7bef1 gaqhf
                                    cmd.Parameters.AddWithValue("@jsonString", item.Value);
489
                                    cmd.Parameters.AddWithValue("@SettingType", item.Key);
490
                                    cmd.ExecuteNonQuery();
491
                                }
492
                            }
493
                            connection.Close();
494 8847ea67 gaqhf
                        }
495 d2a7bef1 gaqhf
                        else
496
                            return false;
497 1a3a74a8 gaqhf
                    }
498 8847ea67 gaqhf
                    catch (Exception ex)
499 e8536f2b gaqhf
                    {
500 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
501 d2a7bef1 gaqhf
                        return false;
502 154d8f43 gaqhf
                    }
503 8847ea67 gaqhf
                    finally
504 154d8f43 gaqhf
                    {
505 8847ea67 gaqhf
                        if (connection != null)
506
                            connection.Dispose();
507 e8536f2b gaqhf
                    }
508 fab4f207 gaqhf
                }
509 8847ea67 gaqhf
            }
510 cf924377 gaqhf
511 8847ea67 gaqhf
            return true;
512
        }
513
514
        public static DataTable SelectSetting()
515
        {
516
            DataTable dt = new DataTable();
517
            Project_Info projectInfo = Project_Info.GetInstance();
518
            if (projectInfo.DBType == ID2DB_Type.SQLite)
519
            {
520 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
521 cf924377 gaqhf
                {
522 8847ea67 gaqhf
                    try
523 cf924377 gaqhf
                    {
524 8847ea67 gaqhf
                        connection.Open();
525
                        using (SQLiteCommand cmd = connection.CreateCommand())
526
                        {
527
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
528
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
529
                                dt.Load(dr);
530
                        }
531
                        connection.Close();
532 cf924377 gaqhf
                    }
533 8847ea67 gaqhf
                    catch (Exception ex)
534
                    {
535 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
536 8847ea67 gaqhf
                    }
537
                    finally
538 7cbb1038 gaqhf
                    {
539 8847ea67 gaqhf
                        connection.Dispose();
540 7cbb1038 gaqhf
                    }
541
                }
542 8847ea67 gaqhf
            }
543
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
544
            {
545
                using (SqlConnection connection = GetSqlConnection())
546 224535bb gaqhf
                {
547 8847ea67 gaqhf
                    try
548
                    {
549
                        if (connection != null && connection.State == ConnectionState.Open)
550
                        {
551 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
552
                            {
553
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_SETTING_TABLE);
554
                                using (SqlDataReader dr = cmd.ExecuteReader())
555
                                    dt.Load(dr);
556
                            }
557
                            connection.Close();
558 8847ea67 gaqhf
                        }
559
                    }
560
                    catch (Exception ex)
561 224535bb gaqhf
                    {
562 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
563 8847ea67 gaqhf
                    }
564
                    finally
565
                    {
566
                        if (connection != null)
567
                            connection.Dispose();
568 224535bb gaqhf
                    }
569
                }
570 fab4f207 gaqhf
            }
571 8847ea67 gaqhf
572
            return dt;
573 fab4f207 gaqhf
        }
574 bca86986 gaqhf
575
        public static DataTable SelectProjectSymbol()
576
        {
577
            DataTable dt = new DataTable();
578
            Project_Info projectInfo = Project_Info.GetInstance();
579 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
580 bca86986 gaqhf
            {
581 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
582 bca86986 gaqhf
                {
583 8847ea67 gaqhf
                    try
584 bca86986 gaqhf
                    {
585 8847ea67 gaqhf
                        connection.Open();
586
                        using (SQLiteCommand cmd = connection.CreateCommand())
587
                        {
588
                            cmd.CommandText = string.Format(@"
589 cf924377 gaqhf
                            SELECT s.UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
590 bca86986 gaqhf
                                LEFT OUTER JOIN {2} as sp 
591
                                    ON s.UID = SP.UID 
592
                            WHERE s.SymbolType_UID = st.UID 
593
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
594 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
595
                                dt.Load(dr);
596 310aeb31 gaqhf
597 8847ea67 gaqhf
                            DataTable dtClone = dt.Clone();
598
                            dtClone.Columns["UID"].DataType = typeof(string);
599
                            foreach (DataRow row in dt.Rows)
600
                            {
601
                                dtClone.ImportRow(row);
602
                            }
603
                            dt.Dispose();
604
                            dt = dtClone;
605 310aeb31 gaqhf
                        }
606 8847ea67 gaqhf
                        connection.Close();
607 bca86986 gaqhf
                    }
608 8847ea67 gaqhf
                    catch (Exception ex)
609
                    {
610 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
611 8847ea67 gaqhf
                    }
612
                    finally
613
                    {
614
                        connection.Dispose();
615
                    }
616 bca86986 gaqhf
                }
617 8847ea67 gaqhf
            }
618
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
619
            {
620
                using (SqlConnection connection = GetSqlConnection())
621 bca86986 gaqhf
                {
622 8847ea67 gaqhf
                    try
623
                    {
624
                        if (connection != null && connection.State == ConnectionState.Open)
625
                        {
626 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
627
                            {
628
                                cmd.CommandText = string.Format(@"
629 a2c803a0 gaqhf
                            SELECT CONVERT(VARCHAR(255), s.UID) AS UID, s.Name, st.Type, sp.SPPID_SYMBOL_PATH, sp.LEADERLINE FROM {1} as st, {0} as s 
630 d2a7bef1 gaqhf
                                LEFT OUTER JOIN {2} as sp 
631 a2c803a0 gaqhf
                                    ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID)
632 d2a7bef1 gaqhf
                            WHERE s.SymbolType_UID = st.UID 
633
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
634
                                using (SqlDataReader dr = cmd.ExecuteReader())
635
                                    dt.Load(dr);
636
                            }
637
                            connection.Close();
638 8847ea67 gaqhf
                        }
639
                    }
640
                    catch (Exception ex)
641
                    {
642 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
643 8847ea67 gaqhf
                    }
644
                    finally
645
                    {
646
                        if (connection != null)
647
                            connection.Dispose();
648
                    }
649 bca86986 gaqhf
                }
650
            }
651
652
            return dt;
653
        }
654
655 4b4dbca9 gaqhf
        public static DataTable SelectProjectChildSymbol()
656
        {
657
            DataTable result = new DataTable();
658
            result.Columns.Add("UID");
659
            result.Columns.Add("Name");
660
            result.Columns.Add("Type");
661
            result.Columns.Add("SPPID_SYMBOL_PATH");
662
            
663
            Project_Info projectInfo = Project_Info.GetInstance();
664 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
665 4b4dbca9 gaqhf
            {
666 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
667 8847ea67 gaqhf
                using (DataTable dt = new DataTable())
668 4b4dbca9 gaqhf
                {
669 8847ea67 gaqhf
                    try
670 4b4dbca9 gaqhf
                    {
671 8847ea67 gaqhf
                        connection.Open();
672
                        using (SQLiteCommand cmd = connection.CreateCommand())
673
                        {
674
                            cmd.CommandText = string.Format(@"
675 4b4dbca9 gaqhf
                            SELECT AdditionalSymbol FROM Symbol");
676 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
677
                                dt.Load(dr);
678
                            List<string> childList = new List<string>();
679
                            foreach (DataRow row in dt.Rows)
680 4b4dbca9 gaqhf
                            {
681 8847ea67 gaqhf
                                if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
682 4b4dbca9 gaqhf
                                {
683 8847ea67 gaqhf
                                    string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
684
                                    foreach (var childString in array)
685
                                    {
686
                                        childList.Add(childString.Split(new char[] { ',' })[2]);
687
                                    }
688 4b4dbca9 gaqhf
                                }
689
690 8847ea67 gaqhf
                            }
691 4b4dbca9 gaqhf
692 8847ea67 gaqhf
                            dt.Clear();
693
                            cmd.Reset();
694
                            cmd.CommandText = string.Format(@"
695 4b4dbca9 gaqhf
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
696 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
697
                                dt.Load(dr);
698 4b4dbca9 gaqhf
699 8847ea67 gaqhf
                            childList = childList.Distinct().ToList();
700
                            foreach (var child in childList)
701
                            {
702
                                string mappingPath = string.Empty;
703
                                DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
704
                                if (rows.Length == 1)
705
                                    mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
706
707
                                DataRow newRow = result.NewRow();
708
                                newRow["UID"] = child;
709
                                newRow["Name"] = child;
710
                                newRow["Type"] = "Child Symbol";
711
                                newRow["SPPID_SYMBOL_PATH"] = mappingPath;
712
                                result.Rows.Add(newRow);
713
                            }
714 4b4dbca9 gaqhf
                        }
715 8847ea67 gaqhf
                        connection.Close();
716 4b4dbca9 gaqhf
                    }
717 8847ea67 gaqhf
                    catch (Exception ex)
718
                    {
719 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
720 8847ea67 gaqhf
                    }
721
                    finally
722
                    {
723
                        connection.Dispose();
724
                    }
725 4b4dbca9 gaqhf
                }
726 8847ea67 gaqhf
            }
727
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
728
            {
729
                using (SqlConnection connection = GetSqlConnection())
730 d2a7bef1 gaqhf
                using (DataTable dt = new DataTable())
731 4b4dbca9 gaqhf
                {
732 8847ea67 gaqhf
                    try
733
                    {
734
                        if (connection != null && connection.State == ConnectionState.Open)
735
                        {
736 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
737
                            {
738
                                cmd.CommandText = string.Format(@"
739
                            SELECT AdditionalSymbol FROM Symbol");
740
                                using (SqlDataReader dr = cmd.ExecuteReader())
741
                                    dt.Load(dr);
742
                                List<string> childList = new List<string>();
743
                                foreach (DataRow row in dt.Rows)
744
                                {
745
                                    if (row["AdditionalSymbol"] != null && !DBNull.Value.Equals(row["AdditionalSymbol"]) && !string.IsNullOrEmpty((string)row["AdditionalSymbol"]))
746
                                    {
747
                                        string[] array = row["AdditionalSymbol"].ToString().Split(new char[] { '/' });
748
                                        foreach (var childString in array)
749
                                        {
750
                                            childList.Add(childString.Split(new char[] { ',' })[2]);
751
                                        }
752
                                    }
753
754
                                }
755
756
                                dt.Clear();
757
                                cmd.CommandText = string.Format(@"
758
                            SELECT * FROM {0}", SPPID_SYMBOL_MAPPING_TABLE);
759
                                using (SqlDataReader dr = cmd.ExecuteReader())
760
                                    dt.Load(dr);
761 8847ea67 gaqhf
762 d2a7bef1 gaqhf
                                childList = childList.Distinct().ToList();
763
                                foreach (var child in childList)
764
                                {
765
                                    string mappingPath = string.Empty;
766
                                    DataRow[] rows = dt.Select(string.Format("UID = '{0}'", child));
767
                                    if (rows.Length == 1)
768
                                        mappingPath = !DBNull.Value.Equals(rows[0]["SPPID_SYMBOL_PATH"]) ? (string)rows[0]["SPPID_SYMBOL_PATH"] : null;
769
770
                                    DataRow newRow = result.NewRow();
771
                                    newRow["UID"] = child;
772
                                    newRow["Name"] = child;
773
                                    newRow["Type"] = "Child Symbol";
774
                                    newRow["SPPID_SYMBOL_PATH"] = mappingPath;
775
                                    result.Rows.Add(newRow);
776
                                }
777
                            }
778
                            connection.Close();
779 8847ea67 gaqhf
                        }
780
                    }
781
                    catch (Exception ex)
782
                    {
783 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
784 8847ea67 gaqhf
                    }
785
                    finally
786
                    {
787
                        if (connection != null)
788
                            connection.Dispose();
789
                    }
790 4b4dbca9 gaqhf
                }
791
            }
792
            return result;
793
        }
794
795 bca86986 gaqhf
        public static DataTable SelectProjectLine()
796
        {
797
            DataTable dt = new DataTable();
798
            Project_Info projectInfo = Project_Info.GetInstance();
799 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
800 bca86986 gaqhf
            {
801 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
802 bca86986 gaqhf
                {
803 8847ea67 gaqhf
                    try
804 bca86986 gaqhf
                    {
805 8847ea67 gaqhf
                        connection.Open();
806
                        using (SQLiteCommand cmd = connection.CreateCommand())
807
                        {
808
                            cmd.CommandText = string.Format(@"
809 bca86986 gaqhf
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
810
                                LEFT OUTER JOIN {1} as sp 
811
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
812 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
813
                                dt.Load(dr);
814
                        }
815
                        connection.Close();
816 bca86986 gaqhf
                    }
817 8847ea67 gaqhf
                    catch (Exception ex)
818
                    {
819 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
820 8847ea67 gaqhf
                    }
821
                    finally
822
                    {
823
                        connection.Dispose();
824
                    }
825 bca86986 gaqhf
                }
826 8847ea67 gaqhf
            }
827
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
828
            {
829
                using (SqlConnection connection = GetSqlConnection())
830 bca86986 gaqhf
                {
831 8847ea67 gaqhf
                    try
832
                    {
833
                        if (connection != null && connection.State == ConnectionState.Open)
834
                        {
835 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
836
                            {
837
                                cmd.CommandText = string.Format(@"
838
                            SELECT l.UID, l.Name, sp.SPPID_SYMBOL_PATH FROM {0} as l 
839
                                LEFT OUTER JOIN {1} as sp 
840
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, SPPID_SYMBOL_MAPPING_TABLE);
841
                                using (SqlDataReader dr = cmd.ExecuteReader())
842
                                    dt.Load(dr);
843
                            }
844
                            connection.Close();
845 8847ea67 gaqhf
                        }
846
                    }
847
                    catch (Exception ex)
848
                    {
849 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
850 8847ea67 gaqhf
                    }
851
                    finally
852
                    {
853
                        if (connection != null)
854
                            connection.Dispose();
855
                    }
856 bca86986 gaqhf
                }
857
            }
858
859
            return dt;
860
        }
861
862
        public static DataTable SelectProjectLineProperties()
863
        {
864
            DataTable dt = new DataTable();
865
            Project_Info projectInfo = Project_Info.GetInstance();
866 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
867 bca86986 gaqhf
            {
868 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
869 bca86986 gaqhf
                {
870 8847ea67 gaqhf
                    try
871 bca86986 gaqhf
                    {
872 8847ea67 gaqhf
                        connection.Open();
873
                        using (SQLiteCommand cmd = connection.CreateCommand())
874
                        {
875
                            cmd.CommandText = string.Format(@"
876 bca86986 gaqhf
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
877
                            FROM {0} as lp 
878
                                 LEFT OUTER JOIN {1} as sp 
879
                                      ON lp.UID = sp.UID
880
                                 LEFT OUTER JOIN {2} as spa 
881
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
882 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
883
                                dt.Load(dr);
884
                        }
885
                        connection.Close();
886 bca86986 gaqhf
                    }
887 8847ea67 gaqhf
                    catch (Exception ex)
888
                    {
889 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
890 8847ea67 gaqhf
                    }
891
                    finally
892
                    {
893
                        connection.Dispose();
894
                    }
895 bca86986 gaqhf
                }
896 8847ea67 gaqhf
            }
897
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
898
            {
899
                using (SqlConnection connection = GetSqlConnection())
900 bca86986 gaqhf
                {
901 8847ea67 gaqhf
                    try
902
                    {
903
                        if (connection != null && connection.State == ConnectionState.Open)
904
                        {
905 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
906
                            {
907
                                cmd.CommandText = string.Format(@"
908
                            SELECT lp.UID, lp.DisplayName, sp.SPPID_SYMBOL_PATH, spa.SPPID_ATTRIBUTE
909
                            FROM {0} as lp 
910
                                 LEFT OUTER JOIN {1} as sp 
911
                                      ON lp.UID = sp.UID
912
                                 LEFT OUTER JOIN {2} as spa 
913
                                      ON lp.UID = spa.UID;", LineProperties_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
914
                                using (SqlDataReader dr = cmd.ExecuteReader())
915
                                    dt.Load(dr);
916
                            }
917
                            connection.Close();
918 8847ea67 gaqhf
                        }
919
                    }
920
                    catch (Exception ex)
921
                    {
922 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
923 8847ea67 gaqhf
                    }
924
                    finally
925
                    {
926
                        if (connection != null)
927
                            connection.Dispose();
928
                    }
929 bca86986 gaqhf
                }
930
            }
931
932
            return dt;
933
        }
934 c9264d17 gaqhf
        public static DataTable SelectProjectNominalDiameter()
935
        {
936
            DataTable dt = new DataTable();
937
            Project_Info projectInfo = Project_Info.GetInstance();
938
            if (projectInfo.DBType == ID2DB_Type.SQLite)
939
            {
940 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
941 c9264d17 gaqhf
                {
942
                    try
943
                    {
944
                        connection.Open();
945
                        using (SQLiteCommand cmd = connection.CreateCommand())
946
                        {
947
                            cmd.CommandText = string.Format(@"SELECT * FROM {0};", ID2_NOMINALDIAMETER_TABLE);
948
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
949
                                dt.Load(dr);
950
                        }
951
                        connection.Close();
952
                    }
953
                    catch (Exception ex)
954
                    {
955
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
956
                    }
957
                    finally
958
                    {
959
                        connection.Dispose();
960
                    }
961
                }
962
            }
963
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
964
            {
965
                using (SqlConnection connection = GetSqlConnection())
966
                {
967
                    try
968
                    {
969
                        if (connection != null && connection.State == ConnectionState.Open)
970
                        {
971
                            using (SqlCommand cmd = connection.CreateCommand())
972
                            {
973
                                cmd.CommandText = string.Format(@"SELECT * FROM {0};", ID2_NOMINALDIAMETER_TABLE);
974
                                using (SqlDataReader dr = cmd.ExecuteReader())
975
                                    dt.Load(dr);
976
                            }
977
                            connection.Close();
978
                        }
979
                    }
980
                    catch (Exception ex)
981
                    {
982
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
983
                    }
984
                    finally
985
                    {
986
                        if (connection != null)
987
                            connection.Dispose();
988
                    }
989
                }
990
            }
991 bca86986 gaqhf
992 c9264d17 gaqhf
            return dt;
993
        }
994 1efc25a3 gaqhf
        public static DataTable SelectProjectAttribute()
995 bca86986 gaqhf
        {
996
            DataTable dt = new DataTable();
997
            Project_Info projectInfo = Project_Info.GetInstance();
998 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
999 bca86986 gaqhf
            {
1000 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1001 bca86986 gaqhf
                {
1002 8847ea67 gaqhf
                    try
1003 bca86986 gaqhf
                    {
1004 8847ea67 gaqhf
                        connection.Open();
1005
                        using (SQLiteCommand cmd = connection.CreateCommand())
1006
                        {
1007
                            cmd.CommandText = string.Format(@"
1008 923501a9 gaqhf
                            SELECT sa.UID, sa.DisplayAttribute, st.TYPE, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spa.ISTEXT, spl.LOCATION, spl.LEADERLINE, sa.Property
1009 bca86986 gaqhf
                            FROM {1} as sa, {0} as st 
1010
                                 LEFT OUTER JOIN {2} as sp 
1011
                                      ON sa.UID = SP.UID 
1012
                                LEFT OUTER JOIN {3} as spa 
1013
                                     ON sa.UID = spa.UID
1014 1a3a74a8 gaqhf
                                LEFT OUTER JOIN {4} as spl 
1015
                                     ON sa.UID = spl.UID
1016 401efcff gaqhf
                            WHERE sa.SymbolType_UID = st.UID AND (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE, SPPID_LABEL_INFO_TABLE);
1017 8847ea67 gaqhf
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1018
                                dt.Load(dr);
1019
                        }
1020
                        connection.Close();
1021 bca86986 gaqhf
                    }
1022 8847ea67 gaqhf
                    catch (Exception ex)
1023
                    {
1024 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1025 8847ea67 gaqhf
                    }
1026
                    finally
1027
                    {
1028
                        connection.Dispose();
1029
                    }
1030
                }
1031
            }
1032
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1033
            {
1034
                using (SqlConnection connection = GetSqlConnection())
1035 bca86986 gaqhf
                {
1036 8847ea67 gaqhf
                    try
1037
                    {
1038
                        if (connection != null && connection.State == ConnectionState.Open)
1039
                        {
1040 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1041
                            {
1042
                                cmd.CommandText = string.Format(@"
1043 923501a9 gaqhf
                            SELECT sa.UID, sa.DisplayAttribute, st.Type, spa.SPPID_ATTRIBUTE, sp.SPPID_SYMBOL_PATH, spa.ISTEXT, spl.LOCATION, spl.LEADERLINE, sa.Property
1044 d2a7bef1 gaqhf
                            FROM {1} as sa
1045
                                 LEFT OUTER JOIN {2} as sp 
1046
                                      ON sa.UID = SP.UID 
1047
                                LEFT OUTER JOIN {3} as spa 
1048
                                     ON sa.UID = spa.UID
1049
                                LEFT OUTER JOIN {4} as spl 
1050
                                     ON sa.UID = spl.UID
1051
                                LEFT OUTER JOIN {0} as st 
1052
                                     ON sa.SymbolType_UID = st.UID 
1053
                            WHERE (sa.Property != 2 OR sa.Property IS NULL);", SymbolType_TABLE, SymbolAttribute_TABLE, SPPID_SYMBOL_MAPPING_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE, SPPID_LABEL_INFO_TABLE);
1054
                                using (SqlDataReader dr = cmd.ExecuteReader())
1055
                                    dt.Load(dr);
1056
                            }
1057
                            connection.Close();
1058 8847ea67 gaqhf
                        }
1059
                    }
1060
                    catch (Exception ex)
1061
                    {
1062 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1063 8847ea67 gaqhf
                    }
1064
                    finally
1065
                    {
1066
                        if (connection != null)
1067
                            connection.Dispose();
1068
                    }
1069 bca86986 gaqhf
                }
1070
            }
1071
1072 8847ea67 gaqhf
1073 bca86986 gaqhf
            return dt;
1074
        }
1075
1076 306a0af9 gaqhf
        public static DataTable SelectDrawingProjectAttribute()
1077
        {
1078
            DataTable dt = new DataTable();
1079
            Project_Info projectInfo = Project_Info.GetInstance();
1080
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1081
            {
1082 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1083 306a0af9 gaqhf
                {
1084
                    try
1085
                    {
1086
                        connection.Open();
1087
                        using (SQLiteCommand cmd = connection.CreateCommand())
1088
                        {
1089
                            cmd.CommandText = string.Format(@"
1090
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1091
                            FROM {0} as da 
1092
                                 LEFT OUTER JOIN {1} as sam 
1093
                                      ON da.UID = sam.UID;
1094
                            ", DrawingAttribute_Table, SPPID_ATTRIBUTE_MAPPING_TABLE);
1095
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1096
                                dt.Load(dr);
1097
                        }
1098
                        connection.Close();
1099
                    }
1100
                    catch (Exception ex)
1101
                    {
1102
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1103
                    }
1104
                    finally
1105
                    {
1106
                        connection.Dispose();
1107
                    }
1108
                }
1109
            }
1110
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1111
            {
1112
                using (SqlConnection connection = GetSqlConnection())
1113
                {
1114
                    try
1115
                    {
1116
                        if (connection != null && connection.State == ConnectionState.Open)
1117
                        {
1118
                            using (SqlCommand cmd = connection.CreateCommand())
1119
                            {
1120
                                cmd.CommandText = string.Format(@"
1121
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1122
                            FROM {0} as da
1123
                                 LEFT OUTER JOIN {1} as sam 
1124
                                      ON da.UID = sam.UID;
1125
                            ", DrawingAttribute_Table, SPPID_ATTRIBUTE_MAPPING_TABLE);
1126
                                using (SqlDataReader dr = cmd.ExecuteReader())
1127
                                    dt.Load(dr);
1128
                            }
1129
                            connection.Close();
1130
                        }
1131
                    }
1132
                    catch (Exception ex)
1133
                    {
1134
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1135
                    }
1136
                    finally
1137
                    {
1138
                        if (connection != null)
1139
                            connection.Dispose();
1140
                    }
1141
                }
1142
            }
1143
1144
            dt.Merge(SelectDrawingProjectAttribute2());
1145
1146
            return dt;
1147
        }
1148 923501a9 gaqhf
1149 306a0af9 gaqhf
        private static DataTable SelectDrawingProjectAttribute2()
1150
        {
1151
            DataTable dt = new DataTable();
1152
            Project_Info projectInfo = Project_Info.GetInstance();
1153
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1154
            {
1155 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1156 306a0af9 gaqhf
                {
1157
                    try
1158
                    {
1159
                        connection.Open();
1160
                        using (SQLiteCommand cmd = connection.CreateCommand())
1161
                        {
1162
                            cmd.CommandText = string.Format(@"
1163
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1164
                            FROM {0} as da 
1165
                                 LEFT OUTER JOIN {1} as sam 
1166
                                      ON da.UID = sam.UID;
1167
                            ", ID2_DRAWINGATTRIBUTE_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
1168
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1169
                                dt.Load(dr);
1170
                        }
1171
                        connection.Close();
1172
                    }
1173
                    catch (Exception ex)
1174
                    {
1175
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1176
                    }
1177
                    finally
1178
                    {
1179
                        connection.Dispose();
1180
                    }
1181
                }
1182
            }
1183
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1184
            {
1185
                using (SqlConnection connection = GetSqlConnection())
1186
                {
1187
                    try
1188
                    {
1189
                        if (connection != null && connection.State == ConnectionState.Open)
1190
                        {
1191
                            using (SqlCommand cmd = connection.CreateCommand())
1192
                            {
1193
                                cmd.CommandText = string.Format(@"
1194
                            SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE 
1195
                            FROM {0} as da
1196
                                 LEFT OUTER JOIN {1} as sam 
1197
                                      ON da.UID = sam.UID;
1198
                            ", ID2_DRAWINGATTRIBUTE_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE);
1199
                                using (SqlDataReader dr = cmd.ExecuteReader())
1200
                                    dt.Load(dr);
1201
                            }
1202
                            connection.Close();
1203
                        }
1204
                    }
1205
                    catch (Exception ex)
1206
                    {
1207
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1208
                    }
1209
                    finally
1210
                    {
1211
                        if (connection != null)
1212
                            connection.Dispose();
1213
                    }
1214
                }
1215
            }
1216
1217
            return dt;
1218
        }
1219
1220 4d2571ab gaqhf
        public static DataTable SelectID2SymbolTable()
1221
        {
1222
            DataTable dt = new DataTable();
1223
            Project_Info projectInfo = Project_Info.GetInstance();
1224 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1225 4d2571ab gaqhf
            {
1226 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1227 4d2571ab gaqhf
                {
1228 8847ea67 gaqhf
                    try
1229 4d2571ab gaqhf
                    {
1230 8847ea67 gaqhf
                        connection.Open();
1231
                        using (SQLiteCommand cmd = connection.CreateCommand())
1232
                        {
1233
                            cmd.CommandText = @"SELECT * FROM Symbol";
1234
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1235
                                dt.Load(dr);
1236
                        }
1237
                        connection.Close();
1238 4d2571ab gaqhf
                    }
1239 8847ea67 gaqhf
                    catch (Exception ex)
1240
                    {
1241 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1242 8847ea67 gaqhf
                    }
1243
                    finally
1244
                    {
1245
                        connection.Dispose();
1246
                    }
1247 4d2571ab gaqhf
                }
1248 8847ea67 gaqhf
            }
1249
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1250
            {
1251
                using (SqlConnection connection = GetSqlConnection())
1252 4d2571ab gaqhf
                {
1253 8847ea67 gaqhf
                    try
1254
                    {
1255
                        if (connection != null && connection.State == ConnectionState.Open)
1256
                        {
1257 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1258
                            {
1259
                                cmd.CommandText = @"SELECT * FROM Symbol";
1260
                                using (SqlDataReader dr = cmd.ExecuteReader())
1261
                                    dt.Load(dr);
1262
                            }
1263
                            connection.Close();
1264 8847ea67 gaqhf
                        }
1265
                    }
1266
                    catch (Exception ex)
1267
                    {
1268 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1269 8847ea67 gaqhf
                    }
1270
                    finally
1271
                    {
1272
                        if (connection != null)
1273
                            connection.Dispose();
1274
                    }
1275 4d2571ab gaqhf
                }
1276
            }
1277
1278
            return dt;
1279
        }
1280
1281 154d8f43 gaqhf
        public static DataTable SelectOPCRelations()
1282
        {
1283
            DataTable dt = new DataTable();
1284
            Project_Info projectInfo = Project_Info.GetInstance();
1285 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1286 154d8f43 gaqhf
            {
1287 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1288 154d8f43 gaqhf
                {
1289 8847ea67 gaqhf
                    try
1290 154d8f43 gaqhf
                    {
1291 8847ea67 gaqhf
                        connection.Open();
1292
                        using (SQLiteCommand cmd = connection.CreateCommand())
1293
                        {
1294
                            cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1295
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1296
                                dt.Load(dr);
1297
                        }
1298
                        connection.Close();
1299 154d8f43 gaqhf
                    }
1300 8847ea67 gaqhf
                    catch (Exception ex)
1301
                    {
1302 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1303 8847ea67 gaqhf
                    }
1304
                    finally
1305
                    {
1306
                        connection.Dispose();
1307
                    }
1308 154d8f43 gaqhf
                }
1309 8847ea67 gaqhf
            }
1310
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1311
            {
1312
                using (SqlConnection connection = GetSqlConnection())
1313 154d8f43 gaqhf
                {
1314 8847ea67 gaqhf
                    try
1315
                    {
1316
                        if (connection != null && connection.State == ConnectionState.Open)
1317
                        {
1318 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1319
                            {
1320
                                cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE);
1321
                                using (SqlDataReader dr = cmd.ExecuteReader())
1322
                                    dt.Load(dr);
1323
                            }
1324
                            connection.Close();
1325 8847ea67 gaqhf
                        }
1326
                    }
1327
                    catch (Exception ex)
1328
                    {
1329 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1330 8847ea67 gaqhf
                    }
1331
                    finally
1332
                    {
1333
                        if (connection != null)
1334
                            connection.Dispose();
1335
                    }
1336 154d8f43 gaqhf
                }
1337
            }
1338
1339
            return dt;
1340
        }
1341
1342
        public static DataTable SelectDrawings()
1343
        {
1344
            DataTable dt = new DataTable();
1345
            Project_Info projectInfo = Project_Info.GetInstance();
1346 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1347 154d8f43 gaqhf
            {
1348 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1349 154d8f43 gaqhf
                {
1350 8847ea67 gaqhf
                    try
1351 154d8f43 gaqhf
                    {
1352 8847ea67 gaqhf
                        connection.Open();
1353
                        using (SQLiteCommand cmd = connection.CreateCommand())
1354
                        {
1355
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1356
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1357
                                dt.Load(dr);
1358
                        }
1359
                        connection.Close();
1360 154d8f43 gaqhf
                    }
1361 8847ea67 gaqhf
                    catch (Exception ex)
1362
                    {
1363 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1364 8847ea67 gaqhf
                    }
1365
                    finally
1366
                    {
1367
                        connection.Dispose();
1368
                    }
1369 154d8f43 gaqhf
                }
1370 8847ea67 gaqhf
            }
1371
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1372
            {
1373
                using (SqlConnection connection = GetSqlConnection())
1374 154d8f43 gaqhf
                {
1375 8847ea67 gaqhf
                    try
1376
                    {
1377
                        if (connection != null && connection.State == ConnectionState.Open)
1378
                        {
1379 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1380
                            {
1381
                                cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
1382
                                using (SqlDataReader dr = cmd.ExecuteReader())
1383
                                    dt.Load(dr);
1384
                            }
1385
                            connection.Close();
1386 8847ea67 gaqhf
                        }
1387
                    }
1388
                    catch (Exception ex)
1389
                    {
1390 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1391 8847ea67 gaqhf
                    }
1392
                    finally
1393
                    {
1394
                        if (connection != null)
1395
                            connection.Dispose();
1396
                    }
1397 154d8f43 gaqhf
                }
1398
            }
1399
1400
            return dt;
1401
        }
1402
1403
        public static DataTable SelectOPCInfo()
1404
        {
1405
            DataTable dt = new DataTable();
1406
            Project_Info projectInfo = Project_Info.GetInstance();
1407 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1408 154d8f43 gaqhf
            {
1409 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1410 154d8f43 gaqhf
                {
1411 8847ea67 gaqhf
                    try
1412 154d8f43 gaqhf
                    {
1413 8847ea67 gaqhf
                        connection.Open();
1414
                        using (SQLiteCommand cmd = connection.CreateCommand())
1415
                        {
1416
                            cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO);
1417
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1418
                                dt.Load(dr);
1419
                        }
1420
                        connection.Close();
1421 154d8f43 gaqhf
                    }
1422 8847ea67 gaqhf
                    catch (Exception ex)
1423
                    {
1424 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1425 8847ea67 gaqhf
                    }
1426
                    finally
1427
                    {
1428
                        connection.Dispose();
1429
                    }
1430 154d8f43 gaqhf
                }
1431 8847ea67 gaqhf
            }
1432
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1433
            {
1434
                using (SqlConnection connection = GetSqlConnection())
1435 154d8f43 gaqhf
                {
1436 8847ea67 gaqhf
                    try
1437
                    {
1438
                        if (connection != null && connection.State == ConnectionState.Open)
1439
                        {
1440 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1441
                            {
1442 0cab8feb gaqhf
                                cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = 0", SPPID_OPC_INFO);
1443 d2a7bef1 gaqhf
                                using (SqlDataReader dr = cmd.ExecuteReader())
1444
                                    dt.Load(dr);
1445
                            }
1446
                            connection.Close();
1447 8847ea67 gaqhf
                        }
1448
                    }
1449
                    catch (Exception ex)
1450
                    {
1451 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1452 8847ea67 gaqhf
                    }
1453
                    finally
1454
                    {
1455
                        if (connection != null)
1456
                            connection.Dispose();
1457
                    }
1458 154d8f43 gaqhf
                }
1459
            }
1460
1461
            return dt;
1462
        }
1463 7e680366 gaqhf
1464
        public static DataTable SelectSymbolType()
1465
        {
1466
            DataTable dt = new DataTable();
1467
            Project_Info projectInfo = Project_Info.GetInstance();
1468 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1469 7e680366 gaqhf
            {
1470 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1471 7e680366 gaqhf
                {
1472 8847ea67 gaqhf
                    try
1473 7e680366 gaqhf
                    {
1474 8847ea67 gaqhf
                        connection.Open();
1475
                        using (SQLiteCommand cmd = connection.CreateCommand())
1476
                        {
1477
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1478
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1479
                                dt.Load(dr);
1480
                        }
1481
                        connection.Close();
1482 7e680366 gaqhf
                    }
1483 8847ea67 gaqhf
                    catch (Exception ex)
1484
                    {
1485 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1486 8847ea67 gaqhf
                    }
1487
                    finally
1488
                    {
1489
                        connection.Dispose();
1490
                    }
1491 7e680366 gaqhf
                }
1492 8847ea67 gaqhf
            }
1493
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1494
            {
1495
                using (SqlConnection connection = GetSqlConnection())
1496 7e680366 gaqhf
                {
1497 8847ea67 gaqhf
                    try
1498
                    {
1499
                        if (connection != null && connection.State == ConnectionState.Open)
1500
                        {
1501 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1502
                            {
1503
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
1504
                                using (SqlDataReader dr = cmd.ExecuteReader())
1505
                                    dt.Load(dr);
1506
                            }
1507
                            connection.Close();
1508 8847ea67 gaqhf
                        }
1509
                    }
1510
                    catch (Exception ex)
1511
                    {
1512 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1513 8847ea67 gaqhf
                    }
1514
                    finally
1515
                    {
1516
                        if (connection != null)
1517
                            connection.Dispose();
1518
                    }
1519 7e680366 gaqhf
                }
1520
            }
1521
1522
            return dt;
1523
        }
1524
1525 4fb0f8d5 gaqhf
        public static DataTable SelectDrawingInfo()
1526
        {
1527 154d8f43 gaqhf
            DataTable dt = new DataTable();
1528 4fb0f8d5 gaqhf
            Project_Info projectInfo = Project_Info.GetInstance();
1529 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1530 4fb0f8d5 gaqhf
            {
1531 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1532 4fb0f8d5 gaqhf
                {
1533 8847ea67 gaqhf
                    try
1534 4fb0f8d5 gaqhf
                    {
1535 8847ea67 gaqhf
                        connection.Open();
1536
                        using (SQLiteCommand cmd = connection.CreateCommand())
1537
                        {
1538
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1539
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
1540
                                dt.Load(dr);
1541
                        }
1542
                        connection.Close();
1543 4fb0f8d5 gaqhf
                    }
1544 8847ea67 gaqhf
                    catch (Exception ex)
1545
                    {
1546 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1547 8847ea67 gaqhf
                    }
1548
                    finally
1549
                    {
1550
                        connection.Dispose();
1551
                    }
1552 4fb0f8d5 gaqhf
                }
1553 8847ea67 gaqhf
            }
1554
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1555
            {
1556
                using (SqlConnection connection = GetSqlConnection())
1557 4fb0f8d5 gaqhf
                {
1558 8847ea67 gaqhf
                    try
1559
                    {
1560
                        if (connection != null && connection.State == ConnectionState.Open)
1561
                        {
1562 d2a7bef1 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1563
                            {
1564
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO);
1565
                                using (SqlDataReader dr = cmd.ExecuteReader())
1566
                                    dt.Load(dr);
1567
                            }
1568
                            connection.Close();
1569 8847ea67 gaqhf
                        }
1570
                    }
1571
                    catch (Exception ex)
1572
                    {
1573 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1574 8847ea67 gaqhf
                    }
1575
                    finally
1576
                    {
1577
                        if (connection != null)
1578
                            connection.Dispose();
1579
                    }
1580 4fb0f8d5 gaqhf
                }
1581
            }
1582
1583
            return dt;
1584
        }
1585
1586 cf924377 gaqhf
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas)
1587 bca86986 gaqhf
        {
1588
            Project_Info projectInfo = Project_Info.GetInstance();
1589 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1590 bca86986 gaqhf
            {
1591 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1592 bca86986 gaqhf
                {
1593 8847ea67 gaqhf
                    try
1594 bca86986 gaqhf
                    {
1595 8847ea67 gaqhf
                        connection.Open();
1596
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1597 1421f1d6 gaqhf
                        {
1598 8847ea67 gaqhf
                            try
1599 1421f1d6 gaqhf
                            {
1600 8847ea67 gaqhf
                                using (SQLiteCommand cmd = connection.CreateCommand())
1601 1421f1d6 gaqhf
                                {
1602 8847ea67 gaqhf
                                    foreach (var item in datas)
1603
                                    {
1604
                                        cmd.Parameters.Clear();
1605
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE);
1606
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1607
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1608
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1609
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1610
                                        cmd.ExecuteNonQuery();
1611
                                    }
1612 1421f1d6 gaqhf
                                }
1613 8847ea67 gaqhf
                                transaction.Commit();
1614
                                connection.Close();
1615
                            }
1616
                            catch (Exception ex)
1617
                            {
1618 2425d530 gaqhf
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1619 8847ea67 gaqhf
                                transaction.Rollback();
1620 a2c803a0 gaqhf
                                return false;
1621 8847ea67 gaqhf
                            }
1622
                            finally
1623
                            {
1624
                                transaction.Dispose();
1625 1421f1d6 gaqhf
                            }
1626 bca86986 gaqhf
                        }
1627
                    }
1628 8847ea67 gaqhf
                    catch (Exception ex)
1629
                    {
1630 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1631 8847ea67 gaqhf
                        return false;
1632
                    }
1633
                    finally
1634
                    {
1635
                        connection.Dispose();
1636
                    }
1637 bca86986 gaqhf
                }
1638 8847ea67 gaqhf
            }
1639
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1640
            {
1641
                using (SqlConnection connection = GetSqlConnection())
1642 bca86986 gaqhf
                {
1643 8847ea67 gaqhf
                    try
1644
                    {
1645
                        if (connection != null && connection.State == ConnectionState.Open)
1646
                        {
1647 a2c803a0 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1648
                            {
1649
                                foreach (var item in datas)
1650
                                {
1651
                                    cmd.Parameters.Clear();
1652
                                    cmd.CommandText = string.Format(@"
1653
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1654
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
1655
                                    ELSE
1656
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, item.Item1);
1657
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1658 f4485671 gaqhf
                                    if (string.IsNullOrEmpty(item.Item2))
1659
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
1660
                                    else
1661
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
1662 a2c803a0 gaqhf
                                    if (string.IsNullOrEmpty(item.Item3))
1663
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
1664
                                    else
1665
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3);
1666
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4);
1667
                                    cmd.ExecuteNonQuery();
1668
                                }
1669
                            }
1670
                            connection.Close();
1671 8847ea67 gaqhf
                        }
1672
                    }
1673
                    catch (Exception ex)
1674
                    {
1675 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1676 a2c803a0 gaqhf
                        return false;
1677 8847ea67 gaqhf
                    }
1678
                    finally
1679
                    {
1680
                        if (connection != null)
1681
                            connection.Dispose();
1682
                    }
1683 bca86986 gaqhf
                }
1684
            }
1685
1686
            return true;
1687
        }
1688
1689 923501a9 gaqhf
        public static bool InsertAttributeMapping(List<Tuple<string, string, bool>> datas)
1690 bca86986 gaqhf
        {
1691
            Project_Info projectInfo = Project_Info.GetInstance();
1692 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1693 bca86986 gaqhf
            {
1694 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1695 bca86986 gaqhf
                {
1696 8847ea67 gaqhf
                    try
1697 bca86986 gaqhf
                    {
1698 8847ea67 gaqhf
                        connection.Open();
1699
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1700 1421f1d6 gaqhf
                        {
1701 8847ea67 gaqhf
                            try
1702 1421f1d6 gaqhf
                            {
1703 8847ea67 gaqhf
                                using (SQLiteCommand cmd = connection.CreateCommand())
1704 1421f1d6 gaqhf
                                {
1705 8847ea67 gaqhf
                                    foreach (var item in datas)
1706
                                    {
1707
                                        cmd.Parameters.Clear();
1708 923501a9 gaqhf
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE, ISTEXT) VALUES (@UID, @SPPID_ATTRIBUTE, @ISTEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE);
1709 8847ea67 gaqhf
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1710
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1711 923501a9 gaqhf
                                        cmd.Parameters.AddWithValue("@ISTEXT", item.Item3);
1712 8847ea67 gaqhf
                                        cmd.ExecuteNonQuery();
1713
                                    }
1714 1421f1d6 gaqhf
                                }
1715 8847ea67 gaqhf
                                transaction.Commit();
1716
                                connection.Close();
1717
                            }
1718
                            catch (Exception ex)
1719
                            {
1720 2425d530 gaqhf
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1721 8847ea67 gaqhf
                                transaction.Rollback();
1722
                            }
1723
                            finally
1724
                            {
1725
                                transaction.Dispose();
1726 1421f1d6 gaqhf
                            }
1727 bca86986 gaqhf
                        }
1728
                    }
1729 8847ea67 gaqhf
                    catch (Exception ex)
1730
                    {
1731 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1732 8847ea67 gaqhf
                        return false;
1733
                    }
1734
                    finally
1735
                    {
1736
                        connection.Dispose();
1737
                    }
1738 bca86986 gaqhf
                }
1739 8847ea67 gaqhf
            }
1740
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1741
            {
1742
                using (SqlConnection connection = GetSqlConnection())
1743 bca86986 gaqhf
                {
1744 8847ea67 gaqhf
                    try
1745
                    {
1746
                        if (connection != null && connection.State == ConnectionState.Open)
1747
                        {
1748 f4485671 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1749 a2c803a0 gaqhf
                            {
1750 f4485671 gaqhf
                                foreach (var item in datas)
1751 a2c803a0 gaqhf
                                {
1752 f4485671 gaqhf
                                    cmd.Parameters.Clear();
1753
                                    cmd.CommandText = string.Format(@"
1754
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1755 923501a9 gaqhf
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE, ISTEXT = @ISTEXT WHERE UID = @UID
1756 f4485671 gaqhf
                                    ELSE
1757 923501a9 gaqhf
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE, ISTEXT) VALUES (@UID, @SPPID_ATTRIBUTE, @ISTEXT)", SPPID_ATTRIBUTE_MAPPING_TABLE, item.Item1);
1758 f4485671 gaqhf
1759
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1760
                                    if (string.IsNullOrEmpty(item.Item2))
1761
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
1762
                                    else
1763 a2c803a0 gaqhf
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2);
1764 923501a9 gaqhf
                                    cmd.Parameters.AddWithValue("@ISTEXT", item.Item3);
1765 f4485671 gaqhf
                                    cmd.ExecuteNonQuery();
1766 a2c803a0 gaqhf
                                }
1767
                            }
1768 f4485671 gaqhf
                            connection.Close();
1769 8847ea67 gaqhf
                        }
1770
                    }
1771
                    catch (Exception ex)
1772
                    {
1773 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1774 a2c803a0 gaqhf
                        return false;
1775 8847ea67 gaqhf
                    }
1776
                    finally
1777
                    {
1778
                        if (connection != null)
1779
                            connection.Dispose();
1780
                    }
1781 bca86986 gaqhf
                }
1782
            }
1783
            return true;
1784
        }
1785 1a3a74a8 gaqhf
1786 cf924377 gaqhf
        public static bool InsertLabelInfoMapping(List<Tuple<string, int, bool>> datas)
1787 1a3a74a8 gaqhf
        {
1788
            Project_Info projectInfo = Project_Info.GetInstance();
1789 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1790 1a3a74a8 gaqhf
            {
1791 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1792 1a3a74a8 gaqhf
                {
1793 8847ea67 gaqhf
                    try
1794 1a3a74a8 gaqhf
                    {
1795 8847ea67 gaqhf
                        connection.Open();
1796
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1797 1a3a74a8 gaqhf
                        {
1798 8847ea67 gaqhf
                            try
1799 1a3a74a8 gaqhf
                            {
1800 8847ea67 gaqhf
                                using (SQLiteCommand cmd = connection.CreateCommand())
1801 1a3a74a8 gaqhf
                                {
1802 8847ea67 gaqhf
                                    foreach (var item in datas)
1803
                                    {
1804
                                        cmd.Parameters.Clear();
1805
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE);
1806
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
1807
                                        cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
1808
                                        cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
1809
                                        cmd.ExecuteNonQuery();
1810
                                    }
1811 1a3a74a8 gaqhf
                                }
1812 8847ea67 gaqhf
                                transaction.Commit();
1813
                                connection.Close();
1814
                            }
1815
                            catch (Exception ex)
1816
                            {
1817 2425d530 gaqhf
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1818 8847ea67 gaqhf
                                transaction.Rollback();
1819 a2c803a0 gaqhf
                                return false;
1820 8847ea67 gaqhf
                            }
1821
                            finally
1822
                            {
1823
                                transaction.Dispose();
1824 1a3a74a8 gaqhf
                            }
1825
                        }
1826
                    }
1827 8847ea67 gaqhf
                    catch (Exception ex)
1828
                    {
1829 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1830 8847ea67 gaqhf
                        return false;
1831
                    }
1832
                    finally
1833
                    {
1834
                        connection.Dispose();
1835
                    }
1836 1a3a74a8 gaqhf
                }
1837 8847ea67 gaqhf
            }
1838
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1839
            {
1840
                using (SqlConnection connection = GetSqlConnection())
1841 1a3a74a8 gaqhf
                {
1842 8847ea67 gaqhf
                    try
1843
                    {
1844
                        if (connection != null && connection.State == ConnectionState.Open)
1845
                        {
1846 a2c803a0 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1847
                            {
1848
                                foreach (var item in datas)
1849
                                {
1850
                                    cmd.Parameters.Clear();
1851 f4485671 gaqhf
                                    cmd.CommandText = string.Format(@"
1852
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
1853
                                        UPDATE {0} SET LOCATION = @LOCATION, LEADERLINE = @LEADERLINE WHERE UID = @UID
1854
                                    ELSE
1855
                                        INSERT INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE, item.Item1);
1856
1857 a2c803a0 gaqhf
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
1858
                                    cmd.Parameters.AddWithValue("@LOCATION", item.Item2);
1859
                                    cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3);
1860
                                    cmd.ExecuteNonQuery();
1861
                                }
1862
                            }
1863
                            connection.Close();
1864 8847ea67 gaqhf
                        }
1865
                    }
1866
                    catch (Exception ex)
1867
                    {
1868 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1869 a2c803a0 gaqhf
                        return false;
1870 8847ea67 gaqhf
                    }
1871
                    finally
1872
                    {
1873
                        if (connection != null)
1874
                            connection.Dispose();
1875
                    }
1876 1a3a74a8 gaqhf
                }
1877
            }
1878
            return true;
1879
        }
1880 69b7387a gaqhf
1881 154d8f43 gaqhf
        public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document)
1882 69b7387a gaqhf
        {
1883
            Project_Info projectInfo = Project_Info.GetInstance();
1884 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
1885 69b7387a gaqhf
            {
1886 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
1887 69b7387a gaqhf
                {
1888 8847ea67 gaqhf
                    try
1889 69b7387a gaqhf
                    {
1890 8847ea67 gaqhf
                        connection.Open();
1891
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
1892 69b7387a gaqhf
                        {
1893 8847ea67 gaqhf
                            try
1894 154d8f43 gaqhf
                            {
1895
                                using (SQLiteCommand cmd = connection.CreateCommand())
1896
                                {
1897 8847ea67 gaqhf
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO);
1898 154d8f43 gaqhf
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1899 8847ea67 gaqhf
                                    cmd.Parameters.AddWithValue("@PATH", path);
1900
                                    cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1901
                                    cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1902 154d8f43 gaqhf
                                    cmd.ExecuteNonQuery();
1903
                                }
1904
1905 8847ea67 gaqhf
                                List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1906
                                foreach (var item in OPCs)
1907
                                {
1908
                                    using (SQLiteCommand cmd = connection.CreateCommand())
1909
                                    {
1910
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, ATTRIBUTES, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @ATTRIBUTES, @PAIRED)", SPPID_OPC_INFO);
1911
                                        cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1912
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1913
                                        cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1914
                                        cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1915
                                        cmd.Parameters.AddWithValue("@PAIRED", false);
1916
                                        cmd.ExecuteNonQuery();
1917
                                    }
1918
                                }
1919
1920
                                transaction.Commit();
1921
                                connection.Close();
1922
                            }
1923
                            catch (Exception ex)
1924
                            {
1925 2425d530 gaqhf
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1926 8847ea67 gaqhf
                                transaction.Rollback();
1927 a2c803a0 gaqhf
                                return false;
1928 8847ea67 gaqhf
                            }
1929
                            finally
1930
                            {
1931
                                transaction.Dispose();
1932
                            }
1933 154d8f43 gaqhf
                        }
1934
                    }
1935 8847ea67 gaqhf
                    catch (Exception ex)
1936
                    {
1937 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1938 8847ea67 gaqhf
                        return false;
1939
                    }
1940
                    finally
1941
                    {
1942
                        connection.Dispose();
1943
                    }
1944 154d8f43 gaqhf
                }
1945 8847ea67 gaqhf
            }
1946
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
1947
            {
1948
                using (SqlConnection connection = GetSqlConnection())
1949 154d8f43 gaqhf
                {
1950 8847ea67 gaqhf
                    try
1951
                    {
1952
                        if (connection != null && connection.State == ConnectionState.Open)
1953
                        {
1954 a2c803a0 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
1955
                            {
1956 2425d530 gaqhf
                                cmd.Parameters.Clear();
1957
                                cmd.CommandText = string.Format(@"
1958 fb2d9638 gaqhf
                                    IF EXISTS (SELECT * FROM {0} WHERE ID2_DRAWING_UID = '{1}')
1959 2425d530 gaqhf
                                        UPDATE {0} SET PATH = @PATH, DRAWINGNUMBER = @DRAWINGNUMBER, DRAWINGNAME = @DRAWINGNAME WHERE ID2_DRAWING_UID = @ID2_DRAWING_UID
1960
                                    ELSE
1961
                                        INSERT INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO, document.UID);
1962
1963 a2c803a0 gaqhf
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1964
                                cmd.Parameters.AddWithValue("@PATH", path);
1965
                                cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber);
1966
                                cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName);
1967
                                cmd.ExecuteNonQuery();
1968
                            }
1969 8847ea67 gaqhf
1970 a2c803a0 gaqhf
                            List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's");
1971
                            foreach (var item in OPCs)
1972
                            {
1973
                                using (SqlCommand cmd = connection.CreateCommand())
1974
                                {
1975 2425d530 gaqhf
                                    cmd.Parameters.Clear();
1976
                                    cmd.CommandText = string.Format(@"
1977 c5bc0767 gaqhf
                                    IF EXISTS (SELECT * FROM {0} WHERE ID2_OPC_UID = '{1}')
1978 2425d530 gaqhf
                                        UPDATE {0} SET SPPID_OPC_MODELITEM_ID = @SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID = @ID2_DRAWING_UID, ATTRIBUTES = @ATTRIBUTES, PAIRED = @PAIRED WHERE ID2_OPC_UID = @ID2_OPC_UID
1979
                                    ELSE
1980
                                        INSERT INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, ATTRIBUTES, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @ATTRIBUTES, @PAIRED)", SPPID_OPC_INFO, item.UID);
1981
1982 a2c803a0 gaqhf
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID);
1983 2425d530 gaqhf
                                    if (string.IsNullOrEmpty(item.SPPID.ModelItemID))
1984
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value);
1985
                                    else
1986
                                        cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID);
1987 a2c803a0 gaqhf
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID);
1988
                                    cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes));
1989
                                    cmd.Parameters.AddWithValue("@PAIRED", false);
1990
                                    cmd.ExecuteNonQuery();
1991
                                }
1992
                            }
1993
                            connection.Close();
1994 8847ea67 gaqhf
                        }
1995
                    }
1996
                    catch (Exception ex)
1997
                    {
1998 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
1999 a2c803a0 gaqhf
                        return false;
2000 8847ea67 gaqhf
                    }
2001
                    finally
2002
                    {
2003
                        if (connection != null)
2004
                            connection.Dispose();
2005
                    }
2006 154d8f43 gaqhf
                }
2007
            }
2008
            return true;
2009
        }
2010
2011 82ab5276 gaqhf
        public static bool InsertLineNumberInfo(string path, string drawingNumber, string drawingName, SPPID_Document document)
2012 da1aeb27 gaqhf
        {
2013 82ab5276 gaqhf
            Project_Info projectInfo = Project_Info.GetInstance();
2014
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2015
            {
2016 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2017 82ab5276 gaqhf
                {
2018
                    try
2019
                    {
2020
                        connection.Open();
2021
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2022
                        {
2023
                            try
2024
                            {
2025
                                using (SQLiteCommand cmd = connection.CreateCommand())
2026
                                {
2027
                                    foreach (var lineNumber in document.LINENUMBERS)
2028
                                    {
2029
                                        foreach (var attribute in lineNumber.ATTRIBUTES)
2030
                                        {
2031
                                            LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2032
                                            if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2033
                                            {
2034
                                                if (mapping.SPPIDATTRIBUTENAME == "PlantGroup.Name")
2035
                                                {
2036
                                                    cmd.Parameters.Clear();
2037
2038
2039
2040
                                                    cmd.ExecuteNonQuery();
2041
                                                }
2042
                                            }
2043
                                        }
2044
                                    }
2045
                                }
2046
2047
                                transaction.Commit();
2048
                                connection.Close();
2049
                            }
2050
                            catch (Exception ex)
2051
                            {
2052
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2053
                                transaction.Rollback();
2054
                                return false;
2055
                            }
2056
                            finally
2057
                            {
2058
                                transaction.Dispose();
2059
                            }
2060
                        }
2061
                    }
2062
                    catch (Exception ex)
2063
                    {
2064
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2065
                        return false;
2066
                    }
2067
                    finally
2068
                    {
2069
                        connection.Dispose();
2070
                    }
2071
                }
2072
            }
2073
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2074
            {
2075
                using (SqlConnection connection = GetSqlConnection())
2076
                {
2077
                    try
2078
                    {
2079
                        if (connection != null && connection.State == ConnectionState.Open)
2080
                        {
2081
                            using (SqlCommand cmd = connection.CreateCommand())
2082
                            {
2083
                                foreach (var lineNumber in document.LINENUMBERS)
2084
                                {
2085
                                    foreach (var attribute in lineNumber.ATTRIBUTES)
2086
                                    {
2087
                                        LineNumberMapping mapping = document.LineNumberMappings.Find(x => x.UID == attribute.UID);
2088
                                        if (mapping != null && !string.IsNullOrEmpty(attribute.VALUE) && attribute.VALUE != "None")
2089
                                        {
2090
                                            if (mapping.SPPIDATTRIBUTENAME == "PlantGroup.Name")
2091
                                            {
2092
                                                cmd.Parameters.Clear();
2093
2094
                                                cmd.ExecuteNonQuery();
2095
                                            }
2096
                                        }
2097
                                    }
2098
                                }
2099
                            }
2100
                            connection.Close();
2101
                        }
2102
                    }
2103
                    catch (Exception ex)
2104
                    {
2105
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2106
                        return false;
2107
                    }
2108
                    finally
2109
                    {
2110
                        if (connection != null)
2111
                            connection.Dispose();
2112
                    }
2113
                }
2114
            }
2115
            return true;
2116 da1aeb27 gaqhf
        }
2117 154d8f43 gaqhf
        public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired)
2118
        {
2119
            Project_Info projectInfo = Project_Info.GetInstance();
2120 8847ea67 gaqhf
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2121 154d8f43 gaqhf
            {
2122 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2123 154d8f43 gaqhf
                {
2124 8847ea67 gaqhf
                    try
2125 154d8f43 gaqhf
                    {
2126 8847ea67 gaqhf
                        connection.Open();
2127
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2128 154d8f43 gaqhf
                        {
2129 8847ea67 gaqhf
                            try
2130 154d8f43 gaqhf
                            {
2131 8847ea67 gaqhf
                                using (SQLiteCommand cmd = connection.CreateCommand())
2132
                                {
2133
                                    cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (ID2_OPC_UID, SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID, PAIRED) VALUES (@ID2_OPC_UID, @SPPID_OPC_MODELITEM_ID, @ID2_DRAWING_UID, @PAIRED)", SPPID_OPC_INFO);
2134
                                    cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
2135
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
2136
                                    cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
2137
                                    cmd.Parameters.AddWithValue("@PAIRED", Paired);
2138
                                    cmd.ExecuteNonQuery();
2139
                                }
2140 154d8f43 gaqhf
2141 8847ea67 gaqhf
                                transaction.Commit();
2142
                                connection.Close();
2143
                            }
2144
                            catch (Exception ex)
2145
                            {
2146 2425d530 gaqhf
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2147 8847ea67 gaqhf
                                transaction.Rollback();
2148 a2c803a0 gaqhf
                                return false;
2149 8847ea67 gaqhf
                            }
2150
                            finally
2151
                            {
2152
                                transaction.Dispose();
2153
                            }
2154 69b7387a gaqhf
                        }
2155
                    }
2156 8847ea67 gaqhf
                    catch (Exception ex)
2157
                    {
2158 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2159 8847ea67 gaqhf
                        return false;
2160
                    }
2161
                    finally
2162
                    {
2163
                        connection.Dispose();
2164
                    }
2165 69b7387a gaqhf
                }
2166 8847ea67 gaqhf
            }
2167
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2168
            {
2169
                using (SqlConnection connection = GetSqlConnection())
2170 69b7387a gaqhf
                {
2171 8847ea67 gaqhf
                    try
2172
                    {
2173
                        if (connection != null && connection.State == ConnectionState.Open)
2174
                        {
2175 a2c803a0 gaqhf
                            using (SqlCommand cmd = connection.CreateCommand())
2176
                            {
2177 2425d530 gaqhf
                                cmd.Parameters.Clear();
2178
                                cmd.CommandText = string.Format(@"
2179
                                    UPDATE {0} SET SPPID_OPC_MODELITEM_ID = @SPPID_OPC_MODELITEM_ID, ID2_DRAWING_UID = @ID2_DRAWING_UID, PAIRED = @PAIRED WHERE ID2_OPC_UID = @ID2_OPC_UID", SPPID_OPC_INFO);
2180 a2c803a0 gaqhf
                                cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID);
2181 2425d530 gaqhf
                                if (string.IsNullOrEmpty(ModelItemID))
2182
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value);
2183
                                else
2184
                                    cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID);
2185 a2c803a0 gaqhf
                                cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID);
2186
                                cmd.Parameters.AddWithValue("@PAIRED", Paired);
2187
                                cmd.ExecuteNonQuery();
2188
                            }
2189
                            connection.Close();
2190 8847ea67 gaqhf
                        }
2191
                    }
2192
                    catch (Exception ex)
2193
                    {
2194 2425d530 gaqhf
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2195 a2c803a0 gaqhf
                        return false;
2196 8847ea67 gaqhf
                    }
2197
                    finally
2198
                    {
2199
                        if (connection != null)
2200
                            connection.Dispose();
2201
                    }
2202 69b7387a gaqhf
                }
2203
            }
2204
            return true;
2205
        }
2206 2425d530 gaqhf
2207
        public static bool ExportMappingData()
2208
        {
2209
            bool result = true;
2210
            try
2211
            {
2212
                DataSet dataSet = new DataSet();
2213
                DataTable symbolMappingDT = GetTable(SPPID_SYMBOL_MAPPING_TABLE);
2214
                if (symbolMappingDT != null)
2215
                {
2216
                    symbolMappingDT.TableName = SPPID_SYMBOL_MAPPING_TABLE;
2217
                    dataSet.Tables.Add(symbolMappingDT);
2218
                }
2219
                else
2220
                    result = false;
2221
2222
                DataTable attributeMappingDT = GetTable(SPPID_ATTRIBUTE_MAPPING_TABLE);
2223
                if (attributeMappingDT != null)
2224
                {
2225
                    attributeMappingDT.TableName = SPPID_ATTRIBUTE_MAPPING_TABLE;
2226
                    dataSet.Tables.Add(attributeMappingDT);
2227
                }
2228
                else
2229
                    result = false;
2230
2231
                if (result)
2232
                {
2233
                    string text = JsonConvert.SerializeObject(dataSet);
2234
                    using (StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf", false))
2235
                    {
2236
                        sw.Write(text);
2237
                        sw.Close();
2238
                        sw.Dispose();
2239
                    }
2240
                }
2241
            }
2242
            catch (Exception ex)
2243
            {
2244
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2245
                result = false;
2246
            }
2247
2248
            return result;
2249
        }
2250
2251
        public static bool ImportMappingData()
2252
        {
2253
            bool result = true;
2254
            try
2255
            {
2256
                string sJson = string.Empty;
2257
                using (StreamReader sw = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf"))
2258
                {
2259
                    sJson = sw.ReadToEnd();
2260
                    sw.Close();
2261
                    sw.Dispose();
2262
                }
2263
2264
                DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(sJson);
2265
                DataTable symbolMappingDT = dataSet.Tables[SPPID_SYMBOL_MAPPING_TABLE];
2266
                if (symbolMappingDT == null || !ImportSymbolMappingTable(symbolMappingDT))
2267
                    result = false;
2268
2269
                DataTable attributeMappingDT = dataSet.Tables[SPPID_ATTRIBUTE_MAPPING_TABLE];
2270
                if (attributeMappingDT == null || !ImportAttributeMappingTable(attributeMappingDT))
2271
                    result = false;
2272
            }
2273
            catch (Exception ex)
2274
            {
2275
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2276
                result = false;
2277
            }
2278
2279
            return result;
2280
        }
2281
2282
        private static DataTable GetTable(string tableName)
2283
        {
2284
            DataTable dt = new DataTable();
2285
            Project_Info projectInfo = Project_Info.GetInstance();
2286
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2287
            {
2288 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2289 2425d530 gaqhf
                {
2290
                    try
2291
                    {
2292
                        connection.Open();
2293
                        using (SQLiteCommand cmd = connection.CreateCommand())
2294
                        {
2295
                            cmd.CommandText = string.Format("SELECT * FROM {0}", tableName);
2296
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
2297
                                dt.Load(dr);
2298
                        }
2299
                        connection.Close();
2300
                    }
2301
                    catch (Exception ex)
2302
                    {
2303
                        dt = null;
2304
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2305
                    }
2306
                    finally
2307
                    {
2308
                        connection.Dispose();
2309
                    }
2310
                }
2311
            }
2312
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2313
            {
2314
                using (SqlConnection connection = GetSqlConnection())
2315
                {
2316
                    try
2317
                    {
2318
                        if (connection != null && connection.State == ConnectionState.Open)
2319
                        {
2320
                            using (SqlCommand cmd = connection.CreateCommand())
2321
                            {
2322
                                cmd.CommandText = string.Format("SELECT * FROM {0}", tableName);
2323
                                using (SqlDataReader dr = cmd.ExecuteReader())
2324
                                    dt.Load(dr);
2325
                            }
2326
                            connection.Close();
2327
                        }
2328
                    }
2329
                    catch (Exception ex)
2330
                    {
2331
                        dt = null;
2332
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2333
                    }
2334
                    finally
2335
                    {
2336
                        if (connection != null)
2337
                            connection.Dispose();
2338
                    }
2339
                }
2340
            }
2341
2342
            return dt;
2343
        }
2344
2345
        private static bool ImportSymbolMappingTable(DataTable dt)
2346
        {
2347
            bool result = false;
2348
2349
            Project_Info projectInfo = Project_Info.GetInstance();
2350
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2351
            {
2352 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2353 2425d530 gaqhf
                {
2354
                    try
2355
                    {
2356
                        connection.Open();
2357
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2358
                        {
2359
                            try
2360
                            {
2361
                                using (SQLiteCommand cmd = connection.CreateCommand())
2362
                                {
2363
                                    foreach (DataRow item in dt.Rows)
2364
                                    {
2365
                                        string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2366
                                        string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"];
2367
                                        string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"];
2368
                                        bool LEADERLINE = Convert.ToBoolean(item["LEADERLINE"]);
2369
2370
                                        cmd.Parameters.Clear();
2371
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE);
2372
                                        cmd.Parameters.AddWithValue("@UID", UID);
2373
                                        cmd.Parameters.AddWithValue("@NAME", NAME);
2374
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH);
2375
                                        cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE);
2376
                                        cmd.ExecuteNonQuery();
2377
                                    }
2378
                                }
2379
                                transaction.Commit();
2380
                                connection.Close();
2381
                                result = true;
2382
                            }
2383
                            catch (Exception ex)
2384
                            {
2385
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2386
                                transaction.Rollback();
2387
                            }
2388
                            finally
2389
                            {
2390
                                transaction.Dispose();
2391
                            }
2392
                        }
2393
                    }
2394
                    catch (Exception ex)
2395
                    {
2396
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2397
                    }
2398
                    finally
2399
                    {
2400
                        connection.Dispose();
2401
                    }
2402
                }
2403
            }
2404
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2405
            {
2406
                using (SqlConnection connection = GetSqlConnection())
2407
                {
2408
                    try
2409
                    {
2410
                        if (connection != null && connection.State == ConnectionState.Open)
2411
                        {
2412
                            using (SqlCommand cmd = connection.CreateCommand())
2413
                            {
2414
                                foreach (DataRow item in dt.Rows)
2415
                                {
2416
                                    string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2417
                                    string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"];
2418
                                    string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"];
2419
                                    bool LEADERLINE = DBNull.Value.Equals(item["LEADERLINE"]) ? false : Convert.ToBoolean(item["LEADERLINE"]);
2420
2421
                                    cmd.Parameters.Clear();
2422
                                    cmd.CommandText = string.Format(@"
2423
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2424
                                        UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID
2425
                                    ELSE
2426
                                        INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, UID);
2427
                                    cmd.Parameters.AddWithValue("@UID", UID);
2428
                                    if (string.IsNullOrEmpty(NAME))
2429
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
2430
                                    else
2431
                                        cmd.Parameters.AddWithValue("@NAME", NAME);
2432
                                    if (string.IsNullOrEmpty(SPPID_SYMBOL_PATH))
2433
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value);
2434
                                    else
2435
                                        cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH);
2436
                                    cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE);
2437
                                    cmd.ExecuteNonQuery();
2438
                                }
2439
                            }
2440
                            connection.Close();
2441
                            result = true;
2442
                        }
2443
                    }
2444
                    catch (Exception ex)
2445
                    {
2446
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2447
                    }
2448
                    finally
2449
                    {
2450
                        if (connection != null)
2451
                            connection.Dispose();
2452
                    }
2453
                }
2454
            }
2455
2456
            return result;
2457
        }
2458
2459
        private static bool ImportAttributeMappingTable(DataTable dt)
2460
        {
2461
            bool result = false;
2462
2463
            Project_Info projectInfo = Project_Info.GetInstance();
2464
            if (projectInfo.DBType == ID2DB_Type.SQLite)
2465
            {
2466 8770bd0a gaqhf
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath), true))
2467 2425d530 gaqhf
                {
2468
                    try
2469
                    {
2470
                        connection.Open();
2471
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
2472
                        {
2473
                            try
2474
                            {
2475
                                using (SQLiteCommand cmd = connection.CreateCommand())
2476
                                {
2477
                                    foreach (DataRow item in dt.Rows)
2478
                                    {
2479
                                        string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2480
                                        string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"];
2481
2482
                                        cmd.Parameters.Clear();
2483
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE);
2484
                                        cmd.Parameters.AddWithValue("@UID", UID);
2485
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE);
2486
                                        cmd.ExecuteNonQuery();
2487
                                    }
2488
                                }
2489
                                transaction.Commit();
2490
                                connection.Close();
2491
                                result = true;
2492
                            }
2493
                            catch (Exception ex)
2494
                            {
2495
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2496
                                transaction.Rollback();
2497
                            }
2498
                            finally
2499
                            {
2500
                                transaction.Dispose();
2501
                            }
2502
                        }
2503
                    }
2504
                    catch (Exception ex)
2505
                    {
2506
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2507
                    }
2508
                    finally
2509
                    {
2510
                        connection.Dispose();
2511
                    }
2512
                }
2513
            }
2514
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
2515
            {
2516
                using (SqlConnection connection = GetSqlConnection())
2517
                {
2518
                    try
2519
                    {
2520
                        if (connection != null && connection.State == ConnectionState.Open)
2521
                        {
2522
                            using (SqlCommand cmd = connection.CreateCommand())
2523
                            {
2524
                                foreach (DataRow item in dt.Rows)
2525
                                {
2526
                                    string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"];
2527
                                    string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"];
2528
2529
                                    cmd.Parameters.Clear();
2530
                                    cmd.CommandText = string.Format(@"
2531
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
2532
                                        UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE WHERE UID = @UID
2533
                                    ELSE
2534
                                        INSERT INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE, UID);
2535
2536
                                    cmd.Parameters.AddWithValue("@UID", UID);
2537
                                    if (string.IsNullOrEmpty(SPPID_ATTRIBUTE))
2538
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value);
2539
                                    else
2540
                                        cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE);
2541
                                    cmd.ExecuteNonQuery();
2542
                                }
2543
                            }
2544
                            connection.Close();
2545
                            result = true;
2546
                        }
2547
                    }
2548
                    catch (Exception ex)
2549
                    {
2550
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
2551
                    }
2552
                    finally
2553
                    {
2554
                        if (connection != null)
2555
                            connection.Dispose();
2556
                    }
2557
                }
2558
            }
2559
2560
2561
2562
            return result;
2563
        }
2564 b18dc619 gaqhf
    }
2565
}
클립보드 이미지 추가 (최대 크기: 500 MB)