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