hytos / DTI_PID / SPPIDConverter / DB / Project_DB.cs @ 93c417a9
이력 | 보기 | 이력해설 | 다운로드 (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 (SQLiteDataReader 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 COLUMN 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, 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, 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 |
private static DataTable SelectDrawingProjectAttribute2() |
1088 |
{ |
1089 |
DataTable dt = new DataTable(); |
1090 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1091 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1092 |
{ |
1093 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1094 |
{ |
1095 |
try |
1096 |
{ |
1097 |
connection.Open(); |
1098 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1099 |
{ |
1100 |
cmd.CommandText = string.Format(@" |
1101 |
SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE |
1102 |
FROM {0} as da |
1103 |
LEFT OUTER JOIN {1} as sam |
1104 |
ON da.UID = sam.UID; |
1105 |
", ID2_DRAWINGATTRIBUTE_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE); |
1106 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
1107 |
dt.Load(dr); |
1108 |
} |
1109 |
connection.Close(); |
1110 |
} |
1111 |
catch (Exception ex) |
1112 |
{ |
1113 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1114 |
} |
1115 |
finally |
1116 |
{ |
1117 |
connection.Dispose(); |
1118 |
} |
1119 |
} |
1120 |
} |
1121 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1122 |
{ |
1123 |
using (SqlConnection connection = GetSqlConnection()) |
1124 |
{ |
1125 |
try |
1126 |
{ |
1127 |
if (connection != null && connection.State == ConnectionState.Open) |
1128 |
{ |
1129 |
using (SqlCommand cmd = connection.CreateCommand()) |
1130 |
{ |
1131 |
cmd.CommandText = string.Format(@" |
1132 |
SELECT da.UID, da.NAME, sam.SPPID_ATTRIBUTE |
1133 |
FROM {0} as da |
1134 |
LEFT OUTER JOIN {1} as sam |
1135 |
ON da.UID = sam.UID; |
1136 |
", ID2_DRAWINGATTRIBUTE_TABLE, SPPID_ATTRIBUTE_MAPPING_TABLE); |
1137 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
1138 |
dt.Load(dr); |
1139 |
} |
1140 |
connection.Close(); |
1141 |
} |
1142 |
} |
1143 |
catch (Exception ex) |
1144 |
{ |
1145 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1146 |
} |
1147 |
finally |
1148 |
{ |
1149 |
if (connection != null) |
1150 |
connection.Dispose(); |
1151 |
} |
1152 |
} |
1153 |
} |
1154 |
|
1155 |
return dt; |
1156 |
} |
1157 |
|
1158 |
public static DataTable SelectID2SymbolTable() |
1159 |
{ |
1160 |
DataTable dt = new DataTable(); |
1161 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1162 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1163 |
{ |
1164 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1165 |
{ |
1166 |
try |
1167 |
{ |
1168 |
connection.Open(); |
1169 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1170 |
{ |
1171 |
cmd.CommandText = @"SELECT * FROM Symbol"; |
1172 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
1173 |
dt.Load(dr); |
1174 |
} |
1175 |
connection.Close(); |
1176 |
} |
1177 |
catch (Exception ex) |
1178 |
{ |
1179 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1180 |
} |
1181 |
finally |
1182 |
{ |
1183 |
connection.Dispose(); |
1184 |
} |
1185 |
} |
1186 |
} |
1187 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1188 |
{ |
1189 |
using (SqlConnection connection = GetSqlConnection()) |
1190 |
{ |
1191 |
try |
1192 |
{ |
1193 |
if (connection != null && connection.State == ConnectionState.Open) |
1194 |
{ |
1195 |
using (SqlCommand cmd = connection.CreateCommand()) |
1196 |
{ |
1197 |
cmd.CommandText = @"SELECT * FROM Symbol"; |
1198 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
1199 |
dt.Load(dr); |
1200 |
} |
1201 |
connection.Close(); |
1202 |
} |
1203 |
} |
1204 |
catch (Exception ex) |
1205 |
{ |
1206 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1207 |
} |
1208 |
finally |
1209 |
{ |
1210 |
if (connection != null) |
1211 |
connection.Dispose(); |
1212 |
} |
1213 |
} |
1214 |
} |
1215 |
|
1216 |
return dt; |
1217 |
} |
1218 |
|
1219 |
public static DataTable SelectOPCRelations() |
1220 |
{ |
1221 |
DataTable dt = new DataTable(); |
1222 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1223 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1224 |
{ |
1225 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1226 |
{ |
1227 |
try |
1228 |
{ |
1229 |
connection.Open(); |
1230 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1231 |
{ |
1232 |
cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE); |
1233 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
1234 |
dt.Load(dr); |
1235 |
} |
1236 |
connection.Close(); |
1237 |
} |
1238 |
catch (Exception ex) |
1239 |
{ |
1240 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1241 |
} |
1242 |
finally |
1243 |
{ |
1244 |
connection.Dispose(); |
1245 |
} |
1246 |
} |
1247 |
} |
1248 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1249 |
{ |
1250 |
using (SqlConnection connection = GetSqlConnection()) |
1251 |
{ |
1252 |
try |
1253 |
{ |
1254 |
if (connection != null && connection.State == ConnectionState.Open) |
1255 |
{ |
1256 |
using (SqlCommand cmd = connection.CreateCommand()) |
1257 |
{ |
1258 |
cmd.CommandText = string.Format("SELECT * FROM {0}", OPCRelations_TABLE); |
1259 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
1260 |
dt.Load(dr); |
1261 |
} |
1262 |
connection.Close(); |
1263 |
} |
1264 |
} |
1265 |
catch (Exception ex) |
1266 |
{ |
1267 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1268 |
} |
1269 |
finally |
1270 |
{ |
1271 |
if (connection != null) |
1272 |
connection.Dispose(); |
1273 |
} |
1274 |
} |
1275 |
} |
1276 |
|
1277 |
return dt; |
1278 |
} |
1279 |
|
1280 |
public static DataTable SelectDrawings() |
1281 |
{ |
1282 |
DataTable dt = new DataTable(); |
1283 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1284 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1285 |
{ |
1286 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1287 |
{ |
1288 |
try |
1289 |
{ |
1290 |
connection.Open(); |
1291 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1292 |
{ |
1293 |
cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings"); |
1294 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
1295 |
dt.Load(dr); |
1296 |
} |
1297 |
connection.Close(); |
1298 |
} |
1299 |
catch (Exception ex) |
1300 |
{ |
1301 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1302 |
} |
1303 |
finally |
1304 |
{ |
1305 |
connection.Dispose(); |
1306 |
} |
1307 |
} |
1308 |
} |
1309 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1310 |
{ |
1311 |
using (SqlConnection connection = GetSqlConnection()) |
1312 |
{ |
1313 |
try |
1314 |
{ |
1315 |
if (connection != null && connection.State == ConnectionState.Open) |
1316 |
{ |
1317 |
using (SqlCommand cmd = connection.CreateCommand()) |
1318 |
{ |
1319 |
cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings"); |
1320 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
1321 |
dt.Load(dr); |
1322 |
} |
1323 |
connection.Close(); |
1324 |
} |
1325 |
} |
1326 |
catch (Exception ex) |
1327 |
{ |
1328 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1329 |
} |
1330 |
finally |
1331 |
{ |
1332 |
if (connection != null) |
1333 |
connection.Dispose(); |
1334 |
} |
1335 |
} |
1336 |
} |
1337 |
|
1338 |
return dt; |
1339 |
} |
1340 |
|
1341 |
public static DataTable SelectOPCInfo() |
1342 |
{ |
1343 |
DataTable dt = new DataTable(); |
1344 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1345 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1346 |
{ |
1347 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1348 |
{ |
1349 |
try |
1350 |
{ |
1351 |
connection.Open(); |
1352 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1353 |
{ |
1354 |
cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = False", SPPID_OPC_INFO); |
1355 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
1356 |
dt.Load(dr); |
1357 |
} |
1358 |
connection.Close(); |
1359 |
} |
1360 |
catch (Exception ex) |
1361 |
{ |
1362 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1363 |
} |
1364 |
finally |
1365 |
{ |
1366 |
connection.Dispose(); |
1367 |
} |
1368 |
} |
1369 |
} |
1370 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1371 |
{ |
1372 |
using (SqlConnection connection = GetSqlConnection()) |
1373 |
{ |
1374 |
try |
1375 |
{ |
1376 |
if (connection != null && connection.State == ConnectionState.Open) |
1377 |
{ |
1378 |
using (SqlCommand cmd = connection.CreateCommand()) |
1379 |
{ |
1380 |
cmd.CommandText = string.Format("SELECT * FROM {0} WHERE SPPID_OPC_MODELITEM_ID IS NOT NULL AND PAIRED = 0", SPPID_OPC_INFO); |
1381 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
1382 |
dt.Load(dr); |
1383 |
} |
1384 |
connection.Close(); |
1385 |
} |
1386 |
} |
1387 |
catch (Exception ex) |
1388 |
{ |
1389 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1390 |
} |
1391 |
finally |
1392 |
{ |
1393 |
if (connection != null) |
1394 |
connection.Dispose(); |
1395 |
} |
1396 |
} |
1397 |
} |
1398 |
|
1399 |
return dt; |
1400 |
} |
1401 |
|
1402 |
public static DataTable SelectSymbolType() |
1403 |
{ |
1404 |
DataTable dt = new DataTable(); |
1405 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1406 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1407 |
{ |
1408 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1409 |
{ |
1410 |
try |
1411 |
{ |
1412 |
connection.Open(); |
1413 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1414 |
{ |
1415 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE); |
1416 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
1417 |
dt.Load(dr); |
1418 |
} |
1419 |
connection.Close(); |
1420 |
} |
1421 |
catch (Exception ex) |
1422 |
{ |
1423 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1424 |
} |
1425 |
finally |
1426 |
{ |
1427 |
connection.Dispose(); |
1428 |
} |
1429 |
} |
1430 |
} |
1431 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1432 |
{ |
1433 |
using (SqlConnection connection = GetSqlConnection()) |
1434 |
{ |
1435 |
try |
1436 |
{ |
1437 |
if (connection != null && connection.State == ConnectionState.Open) |
1438 |
{ |
1439 |
using (SqlCommand cmd = connection.CreateCommand()) |
1440 |
{ |
1441 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE); |
1442 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
1443 |
dt.Load(dr); |
1444 |
} |
1445 |
connection.Close(); |
1446 |
} |
1447 |
} |
1448 |
catch (Exception ex) |
1449 |
{ |
1450 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1451 |
} |
1452 |
finally |
1453 |
{ |
1454 |
if (connection != null) |
1455 |
connection.Dispose(); |
1456 |
} |
1457 |
} |
1458 |
} |
1459 |
|
1460 |
return dt; |
1461 |
} |
1462 |
|
1463 |
public static DataTable SelectDrawingInfo() |
1464 |
{ |
1465 |
DataTable dt = new DataTable(); |
1466 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1467 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1468 |
{ |
1469 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1470 |
{ |
1471 |
try |
1472 |
{ |
1473 |
connection.Open(); |
1474 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1475 |
{ |
1476 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO); |
1477 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
1478 |
dt.Load(dr); |
1479 |
} |
1480 |
connection.Close(); |
1481 |
} |
1482 |
catch (Exception ex) |
1483 |
{ |
1484 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1485 |
} |
1486 |
finally |
1487 |
{ |
1488 |
connection.Dispose(); |
1489 |
} |
1490 |
} |
1491 |
} |
1492 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1493 |
{ |
1494 |
using (SqlConnection connection = GetSqlConnection()) |
1495 |
{ |
1496 |
try |
1497 |
{ |
1498 |
if (connection != null && connection.State == ConnectionState.Open) |
1499 |
{ |
1500 |
using (SqlCommand cmd = connection.CreateCommand()) |
1501 |
{ |
1502 |
cmd.CommandText = string.Format("SELECT * FROM {0}", SPPID_DRAWING_INFO); |
1503 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
1504 |
dt.Load(dr); |
1505 |
} |
1506 |
connection.Close(); |
1507 |
} |
1508 |
} |
1509 |
catch (Exception ex) |
1510 |
{ |
1511 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1512 |
} |
1513 |
finally |
1514 |
{ |
1515 |
if (connection != null) |
1516 |
connection.Dispose(); |
1517 |
} |
1518 |
} |
1519 |
} |
1520 |
|
1521 |
return dt; |
1522 |
} |
1523 |
|
1524 |
public static bool InsertSymbolMapping(List<Tuple<string, string, string, bool>> datas) |
1525 |
{ |
1526 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1527 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1528 |
{ |
1529 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1530 |
{ |
1531 |
try |
1532 |
{ |
1533 |
connection.Open(); |
1534 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
1535 |
{ |
1536 |
try |
1537 |
{ |
1538 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1539 |
{ |
1540 |
foreach (var item in datas) |
1541 |
{ |
1542 |
cmd.Parameters.Clear(); |
1543 |
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); |
1544 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
1545 |
cmd.Parameters.AddWithValue("@NAME", item.Item2); |
1546 |
cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3); |
1547 |
cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4); |
1548 |
cmd.ExecuteNonQuery(); |
1549 |
} |
1550 |
} |
1551 |
transaction.Commit(); |
1552 |
connection.Close(); |
1553 |
} |
1554 |
catch (Exception ex) |
1555 |
{ |
1556 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1557 |
transaction.Rollback(); |
1558 |
return false; |
1559 |
} |
1560 |
finally |
1561 |
{ |
1562 |
transaction.Dispose(); |
1563 |
} |
1564 |
} |
1565 |
} |
1566 |
catch (Exception ex) |
1567 |
{ |
1568 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1569 |
return false; |
1570 |
} |
1571 |
finally |
1572 |
{ |
1573 |
connection.Dispose(); |
1574 |
} |
1575 |
} |
1576 |
} |
1577 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1578 |
{ |
1579 |
using (SqlConnection connection = GetSqlConnection()) |
1580 |
{ |
1581 |
try |
1582 |
{ |
1583 |
if (connection != null && connection.State == ConnectionState.Open) |
1584 |
{ |
1585 |
using (SqlCommand cmd = connection.CreateCommand()) |
1586 |
{ |
1587 |
foreach (var item in datas) |
1588 |
{ |
1589 |
cmd.Parameters.Clear(); |
1590 |
cmd.CommandText = string.Format(@" |
1591 |
IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}') |
1592 |
UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID |
1593 |
ELSE |
1594 |
INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, item.Item1); |
1595 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
1596 |
if (string.IsNullOrEmpty(item.Item2)) |
1597 |
cmd.Parameters.AddWithValue("@NAME", DBNull.Value); |
1598 |
else |
1599 |
cmd.Parameters.AddWithValue("@NAME", item.Item2); |
1600 |
if (string.IsNullOrEmpty(item.Item3)) |
1601 |
cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value); |
1602 |
else |
1603 |
cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", item.Item3); |
1604 |
cmd.Parameters.AddWithValue("@LEADERLINE", item.Item4); |
1605 |
cmd.ExecuteNonQuery(); |
1606 |
} |
1607 |
} |
1608 |
connection.Close(); |
1609 |
} |
1610 |
} |
1611 |
catch (Exception ex) |
1612 |
{ |
1613 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1614 |
return false; |
1615 |
} |
1616 |
finally |
1617 |
{ |
1618 |
if (connection != null) |
1619 |
connection.Dispose(); |
1620 |
} |
1621 |
} |
1622 |
} |
1623 |
|
1624 |
return true; |
1625 |
} |
1626 |
|
1627 |
public static bool InsertAttributeMapping(List<Tuple<string, string>> datas) |
1628 |
{ |
1629 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1630 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1631 |
{ |
1632 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1633 |
{ |
1634 |
try |
1635 |
{ |
1636 |
connection.Open(); |
1637 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
1638 |
{ |
1639 |
try |
1640 |
{ |
1641 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1642 |
{ |
1643 |
foreach (var item in datas) |
1644 |
{ |
1645 |
cmd.Parameters.Clear(); |
1646 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE); |
1647 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
1648 |
cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2); |
1649 |
cmd.ExecuteNonQuery(); |
1650 |
} |
1651 |
} |
1652 |
transaction.Commit(); |
1653 |
connection.Close(); |
1654 |
} |
1655 |
catch (Exception ex) |
1656 |
{ |
1657 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1658 |
transaction.Rollback(); |
1659 |
} |
1660 |
finally |
1661 |
{ |
1662 |
transaction.Dispose(); |
1663 |
} |
1664 |
} |
1665 |
} |
1666 |
catch (Exception ex) |
1667 |
{ |
1668 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1669 |
return false; |
1670 |
} |
1671 |
finally |
1672 |
{ |
1673 |
connection.Dispose(); |
1674 |
} |
1675 |
} |
1676 |
} |
1677 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1678 |
{ |
1679 |
using (SqlConnection connection = GetSqlConnection()) |
1680 |
{ |
1681 |
try |
1682 |
{ |
1683 |
if (connection != null && connection.State == ConnectionState.Open) |
1684 |
{ |
1685 |
using (SqlCommand cmd = connection.CreateCommand()) |
1686 |
{ |
1687 |
foreach (var item in datas) |
1688 |
{ |
1689 |
cmd.Parameters.Clear(); |
1690 |
cmd.CommandText = string.Format(@" |
1691 |
IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}') |
1692 |
UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE WHERE UID = @UID |
1693 |
ELSE |
1694 |
INSERT INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE, item.Item1); |
1695 |
|
1696 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
1697 |
if (string.IsNullOrEmpty(item.Item2)) |
1698 |
cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value); |
1699 |
else |
1700 |
cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", item.Item2); |
1701 |
cmd.ExecuteNonQuery(); |
1702 |
} |
1703 |
} |
1704 |
connection.Close(); |
1705 |
} |
1706 |
} |
1707 |
catch (Exception ex) |
1708 |
{ |
1709 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1710 |
return false; |
1711 |
} |
1712 |
finally |
1713 |
{ |
1714 |
if (connection != null) |
1715 |
connection.Dispose(); |
1716 |
} |
1717 |
} |
1718 |
} |
1719 |
return true; |
1720 |
} |
1721 |
|
1722 |
public static bool InsertLabelInfoMapping(List<Tuple<string, int, bool>> datas) |
1723 |
{ |
1724 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1725 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1726 |
{ |
1727 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1728 |
{ |
1729 |
try |
1730 |
{ |
1731 |
connection.Open(); |
1732 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
1733 |
{ |
1734 |
try |
1735 |
{ |
1736 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1737 |
{ |
1738 |
foreach (var item in datas) |
1739 |
{ |
1740 |
cmd.Parameters.Clear(); |
1741 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE); |
1742 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
1743 |
cmd.Parameters.AddWithValue("@LOCATION", item.Item2); |
1744 |
cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3); |
1745 |
cmd.ExecuteNonQuery(); |
1746 |
} |
1747 |
} |
1748 |
transaction.Commit(); |
1749 |
connection.Close(); |
1750 |
} |
1751 |
catch (Exception ex) |
1752 |
{ |
1753 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1754 |
transaction.Rollback(); |
1755 |
return false; |
1756 |
} |
1757 |
finally |
1758 |
{ |
1759 |
transaction.Dispose(); |
1760 |
} |
1761 |
} |
1762 |
} |
1763 |
catch (Exception ex) |
1764 |
{ |
1765 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1766 |
return false; |
1767 |
} |
1768 |
finally |
1769 |
{ |
1770 |
connection.Dispose(); |
1771 |
} |
1772 |
} |
1773 |
} |
1774 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1775 |
{ |
1776 |
using (SqlConnection connection = GetSqlConnection()) |
1777 |
{ |
1778 |
try |
1779 |
{ |
1780 |
if (connection != null && connection.State == ConnectionState.Open) |
1781 |
{ |
1782 |
using (SqlCommand cmd = connection.CreateCommand()) |
1783 |
{ |
1784 |
foreach (var item in datas) |
1785 |
{ |
1786 |
cmd.Parameters.Clear(); |
1787 |
cmd.CommandText = string.Format(@" |
1788 |
IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}') |
1789 |
UPDATE {0} SET LOCATION = @LOCATION, LEADERLINE = @LEADERLINE WHERE UID = @UID |
1790 |
ELSE |
1791 |
INSERT INTO {0} (UID, LOCATION, LEADERLINE) VALUES (@UID, @LOCATION, @LEADERLINE)", SPPID_LABEL_INFO_TABLE, item.Item1); |
1792 |
|
1793 |
cmd.Parameters.AddWithValue("@UID", item.Item1); |
1794 |
cmd.Parameters.AddWithValue("@LOCATION", item.Item2); |
1795 |
cmd.Parameters.AddWithValue("@LEADERLINE", item.Item3); |
1796 |
cmd.ExecuteNonQuery(); |
1797 |
} |
1798 |
} |
1799 |
connection.Close(); |
1800 |
} |
1801 |
} |
1802 |
catch (Exception ex) |
1803 |
{ |
1804 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1805 |
return false; |
1806 |
} |
1807 |
finally |
1808 |
{ |
1809 |
if (connection != null) |
1810 |
connection.Dispose(); |
1811 |
} |
1812 |
} |
1813 |
} |
1814 |
return true; |
1815 |
} |
1816 |
|
1817 |
public static bool InsertDrawingInfoAndOPCInfo(string path, string drawingNumber, string drawingName, SPPID.Model.SPPID_Document document) |
1818 |
{ |
1819 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1820 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1821 |
{ |
1822 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1823 |
{ |
1824 |
try |
1825 |
{ |
1826 |
connection.Open(); |
1827 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
1828 |
{ |
1829 |
try |
1830 |
{ |
1831 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1832 |
{ |
1833 |
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); |
1834 |
cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID); |
1835 |
cmd.Parameters.AddWithValue("@PATH", path); |
1836 |
cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber); |
1837 |
cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName); |
1838 |
cmd.ExecuteNonQuery(); |
1839 |
} |
1840 |
|
1841 |
List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's"); |
1842 |
foreach (var item in OPCs) |
1843 |
{ |
1844 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1845 |
{ |
1846 |
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); |
1847 |
cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID); |
1848 |
cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID); |
1849 |
cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID); |
1850 |
cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes)); |
1851 |
cmd.Parameters.AddWithValue("@PAIRED", false); |
1852 |
cmd.ExecuteNonQuery(); |
1853 |
} |
1854 |
} |
1855 |
|
1856 |
transaction.Commit(); |
1857 |
connection.Close(); |
1858 |
} |
1859 |
catch (Exception ex) |
1860 |
{ |
1861 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1862 |
transaction.Rollback(); |
1863 |
return false; |
1864 |
} |
1865 |
finally |
1866 |
{ |
1867 |
transaction.Dispose(); |
1868 |
} |
1869 |
} |
1870 |
} |
1871 |
catch (Exception ex) |
1872 |
{ |
1873 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1874 |
return false; |
1875 |
} |
1876 |
finally |
1877 |
{ |
1878 |
connection.Dispose(); |
1879 |
} |
1880 |
} |
1881 |
} |
1882 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1883 |
{ |
1884 |
using (SqlConnection connection = GetSqlConnection()) |
1885 |
{ |
1886 |
try |
1887 |
{ |
1888 |
if (connection != null && connection.State == ConnectionState.Open) |
1889 |
{ |
1890 |
using (SqlCommand cmd = connection.CreateCommand()) |
1891 |
{ |
1892 |
cmd.Parameters.Clear(); |
1893 |
cmd.CommandText = string.Format(@" |
1894 |
IF EXISTS (SELECT * FROM {0} WHERE ID2_DRAWING_UID = '{1}') |
1895 |
UPDATE {0} SET PATH = @PATH, DRAWINGNUMBER = @DRAWINGNUMBER, DRAWINGNAME = @DRAWINGNAME WHERE ID2_DRAWING_UID = @ID2_DRAWING_UID |
1896 |
ELSE |
1897 |
INSERT INTO {0} (ID2_DRAWING_UID, PATH, DRAWINGNUMBER, DRAWINGNAME) VALUES (@ID2_DRAWING_UID, @PATH, @DRAWINGNUMBER, @DRAWINGNAME)", SPPID_DRAWING_INFO, document.UID); |
1898 |
|
1899 |
cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID); |
1900 |
cmd.Parameters.AddWithValue("@PATH", path); |
1901 |
cmd.Parameters.AddWithValue("@DRAWINGNUMBER", drawingNumber); |
1902 |
cmd.Parameters.AddWithValue("@DRAWINGNAME", drawingName); |
1903 |
cmd.ExecuteNonQuery(); |
1904 |
} |
1905 |
|
1906 |
List<Symbol> OPCs = document.SYMBOLS.FindAll(x => x.TYPE == "Piping OPC's" || x.TYPE == "Instrument OPC's"); |
1907 |
foreach (var item in OPCs) |
1908 |
{ |
1909 |
using (SqlCommand cmd = connection.CreateCommand()) |
1910 |
{ |
1911 |
cmd.Parameters.Clear(); |
1912 |
cmd.CommandText = string.Format(@" |
1913 |
IF EXISTS (SELECT * FROM {0} WHERE ID2_OPC_UID = '{1}') |
1914 |
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 |
1915 |
ELSE |
1916 |
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); |
1917 |
|
1918 |
cmd.Parameters.AddWithValue("@ID2_OPC_UID", item.UID); |
1919 |
if (string.IsNullOrEmpty(item.SPPID.ModelItemID)) |
1920 |
cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value); |
1921 |
else |
1922 |
cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", item.SPPID.ModelItemID); |
1923 |
cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", document.UID); |
1924 |
cmd.Parameters.AddWithValue("@ATTRIBUTES", JsonConvert.SerializeObject(item.SPPID.Attributes)); |
1925 |
cmd.Parameters.AddWithValue("@PAIRED", false); |
1926 |
cmd.ExecuteNonQuery(); |
1927 |
} |
1928 |
} |
1929 |
connection.Close(); |
1930 |
} |
1931 |
} |
1932 |
catch (Exception ex) |
1933 |
{ |
1934 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1935 |
return false; |
1936 |
} |
1937 |
finally |
1938 |
{ |
1939 |
if (connection != null) |
1940 |
connection.Dispose(); |
1941 |
} |
1942 |
} |
1943 |
} |
1944 |
return true; |
1945 |
} |
1946 |
|
1947 |
public static bool InsertOPCInfo(string UID, string ModelItemID, string drawingUID, bool Paired) |
1948 |
{ |
1949 |
Project_Info projectInfo = Project_Info.GetInstance(); |
1950 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
1951 |
{ |
1952 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
1953 |
{ |
1954 |
try |
1955 |
{ |
1956 |
connection.Open(); |
1957 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
1958 |
{ |
1959 |
try |
1960 |
{ |
1961 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
1962 |
{ |
1963 |
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); |
1964 |
cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID); |
1965 |
cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID); |
1966 |
cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID); |
1967 |
cmd.Parameters.AddWithValue("@PAIRED", Paired); |
1968 |
cmd.ExecuteNonQuery(); |
1969 |
} |
1970 |
|
1971 |
transaction.Commit(); |
1972 |
connection.Close(); |
1973 |
} |
1974 |
catch (Exception ex) |
1975 |
{ |
1976 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1977 |
transaction.Rollback(); |
1978 |
return false; |
1979 |
} |
1980 |
finally |
1981 |
{ |
1982 |
transaction.Dispose(); |
1983 |
} |
1984 |
} |
1985 |
} |
1986 |
catch (Exception ex) |
1987 |
{ |
1988 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
1989 |
return false; |
1990 |
} |
1991 |
finally |
1992 |
{ |
1993 |
connection.Dispose(); |
1994 |
} |
1995 |
} |
1996 |
} |
1997 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
1998 |
{ |
1999 |
using (SqlConnection connection = GetSqlConnection()) |
2000 |
{ |
2001 |
try |
2002 |
{ |
2003 |
if (connection != null && connection.State == ConnectionState.Open) |
2004 |
{ |
2005 |
using (SqlCommand cmd = connection.CreateCommand()) |
2006 |
{ |
2007 |
cmd.Parameters.Clear(); |
2008 |
cmd.CommandText = string.Format(@" |
2009 |
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); |
2010 |
cmd.Parameters.AddWithValue("@ID2_OPC_UID", UID); |
2011 |
if (string.IsNullOrEmpty(ModelItemID)) |
2012 |
cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", DBNull.Value); |
2013 |
else |
2014 |
cmd.Parameters.AddWithValue("@SPPID_OPC_MODELITEM_ID", ModelItemID); |
2015 |
cmd.Parameters.AddWithValue("@ID2_DRAWING_UID", drawingUID); |
2016 |
cmd.Parameters.AddWithValue("@PAIRED", Paired); |
2017 |
cmd.ExecuteNonQuery(); |
2018 |
} |
2019 |
connection.Close(); |
2020 |
} |
2021 |
} |
2022 |
catch (Exception ex) |
2023 |
{ |
2024 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
2025 |
return false; |
2026 |
} |
2027 |
finally |
2028 |
{ |
2029 |
if (connection != null) |
2030 |
connection.Dispose(); |
2031 |
} |
2032 |
} |
2033 |
} |
2034 |
return true; |
2035 |
} |
2036 |
|
2037 |
public static bool ExportMappingData() |
2038 |
{ |
2039 |
bool result = true; |
2040 |
try |
2041 |
{ |
2042 |
DataSet dataSet = new DataSet(); |
2043 |
DataTable symbolMappingDT = GetTable(SPPID_SYMBOL_MAPPING_TABLE); |
2044 |
if (symbolMappingDT != null) |
2045 |
{ |
2046 |
symbolMappingDT.TableName = SPPID_SYMBOL_MAPPING_TABLE; |
2047 |
dataSet.Tables.Add(symbolMappingDT); |
2048 |
} |
2049 |
else |
2050 |
result = false; |
2051 |
|
2052 |
DataTable attributeMappingDT = GetTable(SPPID_ATTRIBUTE_MAPPING_TABLE); |
2053 |
if (attributeMappingDT != null) |
2054 |
{ |
2055 |
attributeMappingDT.TableName = SPPID_ATTRIBUTE_MAPPING_TABLE; |
2056 |
dataSet.Tables.Add(attributeMappingDT); |
2057 |
} |
2058 |
else |
2059 |
result = false; |
2060 |
|
2061 |
if (result) |
2062 |
{ |
2063 |
string text = JsonConvert.SerializeObject(dataSet); |
2064 |
using (StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf", false)) |
2065 |
{ |
2066 |
sw.Write(text); |
2067 |
sw.Close(); |
2068 |
sw.Dispose(); |
2069 |
} |
2070 |
} |
2071 |
} |
2072 |
catch (Exception ex) |
2073 |
{ |
2074 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
2075 |
result = false; |
2076 |
} |
2077 |
|
2078 |
return result; |
2079 |
} |
2080 |
|
2081 |
public static bool ImportMappingData() |
2082 |
{ |
2083 |
bool result = true; |
2084 |
try |
2085 |
{ |
2086 |
string sJson = string.Empty; |
2087 |
using (StreamReader sw = new StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + @"\ExportFile.mapf")) |
2088 |
{ |
2089 |
sJson = sw.ReadToEnd(); |
2090 |
sw.Close(); |
2091 |
sw.Dispose(); |
2092 |
} |
2093 |
|
2094 |
DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(sJson); |
2095 |
DataTable symbolMappingDT = dataSet.Tables[SPPID_SYMBOL_MAPPING_TABLE]; |
2096 |
if (symbolMappingDT == null || !ImportSymbolMappingTable(symbolMappingDT)) |
2097 |
result = false; |
2098 |
|
2099 |
DataTable attributeMappingDT = dataSet.Tables[SPPID_ATTRIBUTE_MAPPING_TABLE]; |
2100 |
if (attributeMappingDT == null || !ImportAttributeMappingTable(attributeMappingDT)) |
2101 |
result = false; |
2102 |
} |
2103 |
catch (Exception ex) |
2104 |
{ |
2105 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
2106 |
result = false; |
2107 |
} |
2108 |
|
2109 |
return result; |
2110 |
} |
2111 |
|
2112 |
private static DataTable GetTable(string tableName) |
2113 |
{ |
2114 |
DataTable dt = new DataTable(); |
2115 |
Project_Info projectInfo = Project_Info.GetInstance(); |
2116 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
2117 |
{ |
2118 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
2119 |
{ |
2120 |
try |
2121 |
{ |
2122 |
connection.Open(); |
2123 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
2124 |
{ |
2125 |
cmd.CommandText = string.Format("SELECT * FROM {0}", tableName); |
2126 |
using (SQLiteDataReader dr = cmd.ExecuteReader()) |
2127 |
dt.Load(dr); |
2128 |
} |
2129 |
connection.Close(); |
2130 |
} |
2131 |
catch (Exception ex) |
2132 |
{ |
2133 |
dt = null; |
2134 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
2135 |
} |
2136 |
finally |
2137 |
{ |
2138 |
connection.Dispose(); |
2139 |
} |
2140 |
} |
2141 |
} |
2142 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
2143 |
{ |
2144 |
using (SqlConnection connection = GetSqlConnection()) |
2145 |
{ |
2146 |
try |
2147 |
{ |
2148 |
if (connection != null && connection.State == ConnectionState.Open) |
2149 |
{ |
2150 |
using (SqlCommand cmd = connection.CreateCommand()) |
2151 |
{ |
2152 |
cmd.CommandText = string.Format("SELECT * FROM {0}", tableName); |
2153 |
using (SqlDataReader dr = cmd.ExecuteReader()) |
2154 |
dt.Load(dr); |
2155 |
} |
2156 |
connection.Close(); |
2157 |
} |
2158 |
} |
2159 |
catch (Exception ex) |
2160 |
{ |
2161 |
dt = null; |
2162 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
2163 |
} |
2164 |
finally |
2165 |
{ |
2166 |
if (connection != null) |
2167 |
connection.Dispose(); |
2168 |
} |
2169 |
} |
2170 |
} |
2171 |
|
2172 |
return dt; |
2173 |
} |
2174 |
|
2175 |
private static bool ImportSymbolMappingTable(DataTable dt) |
2176 |
{ |
2177 |
bool result = false; |
2178 |
|
2179 |
Project_Info projectInfo = Project_Info.GetInstance(); |
2180 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
2181 |
{ |
2182 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
2183 |
{ |
2184 |
try |
2185 |
{ |
2186 |
connection.Open(); |
2187 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
2188 |
{ |
2189 |
try |
2190 |
{ |
2191 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
2192 |
{ |
2193 |
foreach (DataRow item in dt.Rows) |
2194 |
{ |
2195 |
string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"]; |
2196 |
string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"]; |
2197 |
string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"]; |
2198 |
bool LEADERLINE = Convert.ToBoolean(item["LEADERLINE"]); |
2199 |
|
2200 |
cmd.Parameters.Clear(); |
2201 |
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); |
2202 |
cmd.Parameters.AddWithValue("@UID", UID); |
2203 |
cmd.Parameters.AddWithValue("@NAME", NAME); |
2204 |
cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH); |
2205 |
cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE); |
2206 |
cmd.ExecuteNonQuery(); |
2207 |
} |
2208 |
} |
2209 |
transaction.Commit(); |
2210 |
connection.Close(); |
2211 |
result = true; |
2212 |
} |
2213 |
catch (Exception ex) |
2214 |
{ |
2215 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
2216 |
transaction.Rollback(); |
2217 |
} |
2218 |
finally |
2219 |
{ |
2220 |
transaction.Dispose(); |
2221 |
} |
2222 |
} |
2223 |
} |
2224 |
catch (Exception ex) |
2225 |
{ |
2226 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
2227 |
} |
2228 |
finally |
2229 |
{ |
2230 |
connection.Dispose(); |
2231 |
} |
2232 |
} |
2233 |
} |
2234 |
else if (projectInfo.DBType == ID2DB_Type.MSSQL) |
2235 |
{ |
2236 |
using (SqlConnection connection = GetSqlConnection()) |
2237 |
{ |
2238 |
try |
2239 |
{ |
2240 |
if (connection != null && connection.State == ConnectionState.Open) |
2241 |
{ |
2242 |
using (SqlCommand cmd = connection.CreateCommand()) |
2243 |
{ |
2244 |
foreach (DataRow item in dt.Rows) |
2245 |
{ |
2246 |
string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"]; |
2247 |
string NAME = DBNull.Value.Equals(item["NAME"]) ? null : (string)item["NAME"]; |
2248 |
string SPPID_SYMBOL_PATH = DBNull.Value.Equals(item["SPPID_SYMBOL_PATH"]) ? null : (string)item["SPPID_SYMBOL_PATH"]; |
2249 |
bool LEADERLINE = DBNull.Value.Equals(item["LEADERLINE"]) ? false : Convert.ToBoolean(item["LEADERLINE"]); |
2250 |
|
2251 |
cmd.Parameters.Clear(); |
2252 |
cmd.CommandText = string.Format(@" |
2253 |
IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}') |
2254 |
UPDATE {0} SET NAME = @NAME, SPPID_SYMBOL_PATH = @SPPID_SYMBOL_PATH, LEADERLINE = @LEADERLINE WHERE UID = @UID |
2255 |
ELSE |
2256 |
INSERT INTO {0} (UID, NAME, SPPID_SYMBOL_PATH, LEADERLINE) VALUES (@UID, @NAME, @SPPID_SYMBOL_PATH, @LEADERLINE)", SPPID_SYMBOL_MAPPING_TABLE, UID); |
2257 |
cmd.Parameters.AddWithValue("@UID", UID); |
2258 |
if (string.IsNullOrEmpty(NAME)) |
2259 |
cmd.Parameters.AddWithValue("@NAME", DBNull.Value); |
2260 |
else |
2261 |
cmd.Parameters.AddWithValue("@NAME", NAME); |
2262 |
if (string.IsNullOrEmpty(SPPID_SYMBOL_PATH)) |
2263 |
cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", DBNull.Value); |
2264 |
else |
2265 |
cmd.Parameters.AddWithValue("@SPPID_SYMBOL_PATH", SPPID_SYMBOL_PATH); |
2266 |
cmd.Parameters.AddWithValue("@LEADERLINE", LEADERLINE); |
2267 |
cmd.ExecuteNonQuery(); |
2268 |
} |
2269 |
} |
2270 |
connection.Close(); |
2271 |
result = true; |
2272 |
} |
2273 |
} |
2274 |
catch (Exception ex) |
2275 |
{ |
2276 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
2277 |
} |
2278 |
finally |
2279 |
{ |
2280 |
if (connection != null) |
2281 |
connection.Dispose(); |
2282 |
} |
2283 |
} |
2284 |
} |
2285 |
|
2286 |
return result; |
2287 |
} |
2288 |
|
2289 |
private static bool ImportAttributeMappingTable(DataTable dt) |
2290 |
{ |
2291 |
bool result = false; |
2292 |
|
2293 |
Project_Info projectInfo = Project_Info.GetInstance(); |
2294 |
if (projectInfo.DBType == ID2DB_Type.SQLite) |
2295 |
{ |
2296 |
using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath))) |
2297 |
{ |
2298 |
try |
2299 |
{ |
2300 |
connection.Open(); |
2301 |
using (SQLiteTransaction transaction = connection.BeginTransaction()) |
2302 |
{ |
2303 |
try |
2304 |
{ |
2305 |
using (SQLiteCommand cmd = connection.CreateCommand()) |
2306 |
{ |
2307 |
foreach (DataRow item in dt.Rows) |
2308 |
{ |
2309 |
string UID = DBNull.Value.Equals(item["UID"]) ? null : (string)item["UID"]; |
2310 |
string SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"]; |
2311 |
|
2312 |
cmd.Parameters.Clear(); |
2313 |
cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE); |
2314 |
cmd.Parameters.AddWithValue("@UID", UID); |
2315 |
cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE); |
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 SPPID_ATTRIBUTE = DBNull.Value.Equals(item["SPPID_ATTRIBUTE"]) ? null : (string)item["SPPID_ATTRIBUTE"]; |
2358 |
|
2359 |
cmd.Parameters.Clear(); |
2360 |
cmd.CommandText = string.Format(@" |
2361 |
IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}') |
2362 |
UPDATE {0} SET SPPID_ATTRIBUTE = @SPPID_ATTRIBUTE WHERE UID = @UID |
2363 |
ELSE |
2364 |
INSERT INTO {0} (UID, SPPID_ATTRIBUTE) VALUES (@UID, @SPPID_ATTRIBUTE)", SPPID_ATTRIBUTE_MAPPING_TABLE, UID); |
2365 |
|
2366 |
cmd.Parameters.AddWithValue("@UID", UID); |
2367 |
if (string.IsNullOrEmpty(SPPID_ATTRIBUTE)) |
2368 |
cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", DBNull.Value); |
2369 |
else |
2370 |
cmd.Parameters.AddWithValue("@SPPID_ATTRIBUTE", SPPID_ATTRIBUTE); |
2371 |
cmd.ExecuteNonQuery(); |
2372 |
} |
2373 |
} |
2374 |
connection.Close(); |
2375 |
result = true; |
2376 |
} |
2377 |
} |
2378 |
catch (Exception ex) |
2379 |
{ |
2380 |
Log.Write(ex.Message + "\r\n" + ex.StackTrace); |
2381 |
} |
2382 |
finally |
2383 |
{ |
2384 |
if (connection != null) |
2385 |
connection.Dispose(); |
2386 |
} |
2387 |
} |
2388 |
} |
2389 |
|
2390 |
|
2391 |
|
2392 |
return result; |
2393 |
} |
2394 |
} |
2395 |
} |