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