hytos / DTI_PID / POSCO_Report / DB.cs @ b2e32d43
이력 | 보기 | 이력해설 | 다운로드 (11.9 KB)
1 | 9c451472 | gaqhf | using System; |
---|---|---|---|
2 | using System.Collections.Generic; |
||
3 | using System.Linq; |
||
4 | using System.Text; |
||
5 | using System.Threading.Tasks; |
||
6 | using System.Data.SQLite; |
||
7 | using System.Globalization; |
||
8 | using System.Data; |
||
9 | using System.Windows.Forms; |
||
10 | 61004277 | esham21 | using System.IO; |
11 | 9c451472 | gaqhf | |
12 | namespace POSCO_Report |
||
13 | { |
||
14 | |||
15 | |||
16 | public static class DB |
||
17 | { |
||
18 | const string HMB_From_To = "HMB_From_To"; |
||
19 | const string HMB_LIST = "HMB_LIST"; |
||
20 | const string HMB_VALUE = "HMB_VALUE"; |
||
21 | const string Stream_No = "Stream_No"; |
||
22 | const string Components = "Components"; |
||
23 | const string Attributes = "Attributes"; |
||
24 | const string Symbol = "Symbol"; |
||
25 | const string SymbolType = "SymbolType"; |
||
26 | const string SymbolAttribute = "SymbolAttribute"; |
||
27 | |||
28 | public static bool CheckConnection() |
||
29 | { |
||
30 | ProjectInformation projectInformation = ProjectInformation.GetInstance(); |
||
31 | bool result = false; |
||
32 | try |
||
33 | { |
||
34 | if (projectInformation.DBType.Equals(DBType.SQLite)) |
||
35 | { |
||
36 | using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true)) |
||
37 | { |
||
38 | connection.Open(); |
||
39 | if (connection.State.Equals(ConnectionState.Open)) |
||
40 | { |
||
41 | using (SQLiteCommand cmd = connection.CreateCommand()) |
||
42 | { |
||
43 | cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'"; |
||
44 | using (SQLiteDataReader dr = cmd.ExecuteReader()) |
||
45 | using (DataTable dt = new DataTable()) |
||
46 | { |
||
47 | dt.Load(dr); |
||
48 | |||
49 | if (dt.Select(string.Format("NAME = '{0}'", HMB_From_To)).Length.Equals(1) && |
||
50 | dt.Select(string.Format("NAME = '{0}'", HMB_LIST)).Length.Equals(1) && |
||
51 | dt.Select(string.Format("NAME = '{0}'", HMB_VALUE)).Length.Equals(1) && |
||
52 | dt.Select(string.Format("NAME = '{0}'", Stream_No)).Length.Equals(1) && |
||
53 | dt.Select(string.Format("NAME = '{0}'", Components)).Length.Equals(1) && |
||
54 | dt.Select(string.Format("NAME = '{0}'", Attributes)).Length.Equals(1)) |
||
55 | { |
||
56 | result = true; |
||
57 | } |
||
58 | } |
||
59 | } |
||
60 | |||
61 | } |
||
62 | connection.Close(); |
||
63 | } |
||
64 | } |
||
65 | else if (projectInformation.DBType.Equals(DBType.MSSQL)) |
||
66 | { |
||
67 | throw new Exception("mssql"); |
||
68 | } |
||
69 | } |
||
70 | catch (Exception ex) |
||
71 | { |
||
72 | MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
73 | } |
||
74 | |||
75 | return result; |
||
76 | } |
||
77 | public static DataTable GetSymbolTable() |
||
78 | { |
||
79 | DataTable dt = new DataTable(); |
||
80 | ProjectInformation projectInformation = ProjectInformation.GetInstance(); |
||
81 | try |
||
82 | { |
||
83 | if (projectInformation.DBType.Equals(DBType.SQLite)) |
||
84 | { |
||
85 | using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true)) |
||
86 | { |
||
87 | connection.Open(); |
||
88 | if (connection.State.Equals(ConnectionState.Open)) |
||
89 | { |
||
90 | using (SQLiteCommand cmd = connection.CreateCommand()) |
||
91 | { |
||
92 | 61004277 | esham21 | cmd.CommandText = string.Format("SELECT s.UID, s.Name, st.Type, s.BaseSymbol FROM {0} s LEFT JOIN {1} st on s.SymbolType_UID = st.UID WHERE s.BaseSymbol IS NOT NULL", Symbol, SymbolType); |
93 | 9c451472 | gaqhf | using (SQLiteDataReader dr = cmd.ExecuteReader()) |
94 | dt.Load(dr); |
||
95 | } |
||
96 | |||
97 | } |
||
98 | connection.Close(); |
||
99 | } |
||
100 | } |
||
101 | else if (projectInformation.DBType.Equals(DBType.MSSQL)) |
||
102 | { |
||
103 | throw new Exception("mssql"); |
||
104 | } |
||
105 | } |
||
106 | catch (Exception ex) |
||
107 | { |
||
108 | MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
109 | } |
||
110 | 61004277 | esham21 | |
111 | dt.DefaultView.Sort = "Name"; |
||
112 | dt = dt.DefaultView.ToTable(); |
||
113 | |||
114 | return dt; |
||
115 | 9c451472 | gaqhf | } |
116 | public static DataTable GetSymbolAttributeTable() |
||
117 | { |
||
118 | DataTable dt = new DataTable(); |
||
119 | ProjectInformation projectInformation = ProjectInformation.GetInstance(); |
||
120 | try |
||
121 | { |
||
122 | if (projectInformation.DBType.Equals(DBType.SQLite)) |
||
123 | { |
||
124 | using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true)) |
||
125 | { |
||
126 | connection.Open(); |
||
127 | if (connection.State.Equals(ConnectionState.Open)) |
||
128 | { |
||
129 | using (SQLiteCommand cmd = connection.CreateCommand()) |
||
130 | { |
||
131 | cmd.CommandText = string.Format("SELECT DISTINCT Attribute FROM {0}", SymbolAttribute); |
||
132 | using (SQLiteDataReader dr = cmd.ExecuteReader()) |
||
133 | dt.Load(dr); |
||
134 | } |
||
135 | } |
||
136 | connection.Close(); |
||
137 | } |
||
138 | } |
||
139 | else if (projectInformation.DBType.Equals(DBType.MSSQL)) |
||
140 | { |
||
141 | throw new Exception("mssql"); |
||
142 | } |
||
143 | } |
||
144 | catch (Exception ex) |
||
145 | { |
||
146 | MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
147 | } |
||
148 | 61004277 | esham21 | |
149 | dt.DefaultView.Sort = "Attribute"; |
||
150 | dt = dt.DefaultView.ToTable(); |
||
151 | |||
152 | return dt; |
||
153 | 9c451472 | gaqhf | } |
154 | public static DataTable GetHMBCaseDataTable() |
||
155 | { |
||
156 | DataTable dt = new DataTable(); |
||
157 | ProjectInformation projectInformation = ProjectInformation.GetInstance(); |
||
158 | try |
||
159 | { |
||
160 | if (projectInformation.DBType.Equals(DBType.SQLite)) |
||
161 | { |
||
162 | using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true)) |
||
163 | { |
||
164 | connection.Open(); |
||
165 | if (connection.State.Equals(ConnectionState.Open)) |
||
166 | { |
||
167 | using (SQLiteCommand cmd = connection.CreateCommand()) |
||
168 | { |
||
169 | cmd.CommandText = string.Format(@" |
||
170 | SELECT hv.UID, hv.Value, hv.[Case], hv.HMB_LIST_UID, hl.Name, hl.Unit, hl.Type, st.Stream_No |
||
171 | FROM {0} hv |
||
172 | LEFT JOIN {1} hl |
||
173 | ON hv.HMB_LIST_UID = hl.UID |
||
174 | LEFT JOIN {2} st |
||
175 | ON hv.Stream_No_UID = st.UID", HMB_VALUE, HMB_LIST, Stream_No); |
||
176 | using (SQLiteDataReader dr = cmd.ExecuteReader()) |
||
177 | dt.Load(dr); |
||
178 | } |
||
179 | |||
180 | } |
||
181 | connection.Close(); |
||
182 | } |
||
183 | } |
||
184 | else if (projectInformation.DBType.Equals(DBType.MSSQL)) |
||
185 | { |
||
186 | throw new Exception("mssql"); |
||
187 | } |
||
188 | } |
||
189 | catch (Exception ex) |
||
190 | { |
||
191 | MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
192 | } |
||
193 | return dt; |
||
194 | } |
||
195 | public static DataTable GetHMBListDataTable() |
||
196 | { |
||
197 | DataTable dt = new DataTable(); |
||
198 | ProjectInformation projectInformation = ProjectInformation.GetInstance(); |
||
199 | try |
||
200 | { |
||
201 | if (projectInformation.DBType.Equals(DBType.SQLite)) |
||
202 | { |
||
203 | using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true)) |
||
204 | { |
||
205 | connection.Open(); |
||
206 | if (connection.State.Equals(ConnectionState.Open)) |
||
207 | { |
||
208 | using (SQLiteCommand cmd = connection.CreateCommand()) |
||
209 | { |
||
210 | cmd.CommandText = string.Format(@" |
||
211 | SELECT * |
||
212 | FROM {0}", HMB_LIST); |
||
213 | using (SQLiteDataReader dr = cmd.ExecuteReader()) |
||
214 | dt.Load(dr); |
||
215 | } |
||
216 | |||
217 | } |
||
218 | connection.Close(); |
||
219 | } |
||
220 | } |
||
221 | else if (projectInformation.DBType.Equals(DBType.MSSQL)) |
||
222 | { |
||
223 | throw new Exception("mssql"); |
||
224 | } |
||
225 | } |
||
226 | catch (Exception ex) |
||
227 | { |
||
228 | MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
229 | } |
||
230 | return dt; |
||
231 | } |
||
232 | |||
233 | public static DataTable GetSymbolStreamData(string streamNoAttrName) |
||
234 | { |
||
235 | DataTable dt = new DataTable(); |
||
236 | ProjectInformation projectInformation = ProjectInformation.GetInstance(); |
||
237 | try |
||
238 | { |
||
239 | if (projectInformation.DBType.Equals(DBType.SQLite)) |
||
240 | { |
||
241 | using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true)) |
||
242 | { |
||
243 | connection.Open(); |
||
244 | if (connection.State.Equals(ConnectionState.Open)) |
||
245 | { |
||
246 | using (SQLiteCommand cmd = connection.CreateCommand()) |
||
247 | { |
||
248 | cmd.CommandText = string.Format(@" |
||
249 | SELECT c.UID, c.SYMBOL_UID, a.[VALUE], sa.ATTRIBUTE |
||
250 | FROM COMPONENTS c, Attributes a, SymbolAttribute sa |
||
251 | WHERE c.UID = a.COMPONENTS_UID AND a.SymbolAttribute_UID = sa.UID AND sa.Attribute = '{0}'", streamNoAttrName); |
||
252 | using (SQLiteDataReader dr = cmd.ExecuteReader()) |
||
253 | dt.Load(dr); |
||
254 | } |
||
255 | |||
256 | } |
||
257 | connection.Close(); |
||
258 | } |
||
259 | } |
||
260 | else if (projectInformation.DBType.Equals(DBType.MSSQL)) |
||
261 | { |
||
262 | throw new Exception("mssql"); |
||
263 | } |
||
264 | } |
||
265 | catch (Exception ex) |
||
266 | { |
||
267 | MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
268 | } |
||
269 | return dt; |
||
270 | } |
||
271 | |||
272 | public static DataTable GetSymbolItemData(string itemAttrName) |
||
273 | { |
||
274 | DataTable dt = new DataTable(); |
||
275 | ProjectInformation projectInformation = ProjectInformation.GetInstance(); |
||
276 | try |
||
277 | { |
||
278 | if (projectInformation.DBType.Equals(DBType.SQLite)) |
||
279 | { |
||
280 | using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true)) |
||
281 | { |
||
282 | connection.Open(); |
||
283 | if (connection.State.Equals(ConnectionState.Open)) |
||
284 | { |
||
285 | using (SQLiteCommand cmd = connection.CreateCommand()) |
||
286 | { |
||
287 | cmd.CommandText = string.Format(@" |
||
288 | SELECT c.UID, c.SYMBOL_UID, a.[VALUE], sa.ATTRIBUTE |
||
289 | FROM COMPONENTS c, Attributes a, SymbolAttribute sa |
||
290 | WHERE c.UID = a.COMPONENTS_UID AND a.SymbolAttribute_UID = sa.UID AND sa.Attribute = '{0}'", itemAttrName); |
||
291 | using (SQLiteDataReader dr = cmd.ExecuteReader()) |
||
292 | dt.Load(dr); |
||
293 | } |
||
294 | |||
295 | } |
||
296 | connection.Close(); |
||
297 | } |
||
298 | } |
||
299 | else if (projectInformation.DBType.Equals(DBType.MSSQL)) |
||
300 | { |
||
301 | throw new Exception("mssql"); |
||
302 | } |
||
303 | } |
||
304 | catch (Exception ex) |
||
305 | { |
||
306 | MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
307 | } |
||
308 | return dt; |
||
309 | } |
||
310 | |||
311 | public static string GetAttributeValue(string componentUID, string attrName) |
||
312 | { |
||
313 | string result = string.Empty; |
||
314 | |||
315 | ProjectInformation projectInformation = ProjectInformation.GetInstance(); |
||
316 | try |
||
317 | { |
||
318 | if (projectInformation.DBType.Equals(DBType.SQLite)) |
||
319 | { |
||
320 | using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true)) |
||
321 | { |
||
322 | connection.Open(); |
||
323 | if (connection.State.Equals(ConnectionState.Open)) |
||
324 | { |
||
325 | using (SQLiteCommand cmd = connection.CreateCommand()) |
||
326 | { |
||
327 | DataTable dt = new DataTable(); |
||
328 | cmd.CommandText = string.Format(@" |
||
329 | SELECT c.UID, c.SYMBOL_UID, a.[VALUE], sa.ATTRIBUTE |
||
330 | FROM COMPONENTS c, Attributes a, SymbolAttribute sa |
||
331 | WHERE c.UID = a.COMPONENTS_UID AND a.SymbolAttribute_UID = sa.UID AND sa.Attribute = '{0}' AND c.UID = '{1}'", attrName, componentUID); |
||
332 | using (SQLiteDataReader dr = cmd.ExecuteReader()) |
||
333 | dt.Load(dr); |
||
334 | |||
335 | if (dt.Rows.Count.Equals(1)) |
||
336 | result = dt.Rows[0]["VALUE"].ToString(); |
||
337 | } |
||
338 | |||
339 | } |
||
340 | connection.Close(); |
||
341 | } |
||
342 | } |
||
343 | else if (projectInformation.DBType.Equals(DBType.MSSQL)) |
||
344 | { |
||
345 | throw new Exception("mssql"); |
||
346 | } |
||
347 | } |
||
348 | catch (Exception ex) |
||
349 | { |
||
350 | MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
351 | } |
||
352 | |||
353 | return result; |
||
354 | } |
||
355 | |||
356 | e6782243 | gaqhf | public static DataTable GetProject() |
357 | { |
||
358 | DataTable dt = new DataTable(); |
||
359 | ProjectInformation projectInformation = ProjectInformation.GetInstance(); |
||
360 | try |
||
361 | { |
||
362 | using (SQLiteConnection connection = new SQLiteConnection(@"Data Source = C:\ProgramData\Digital PID\Project.db", true)) |
||
363 | { |
||
364 | connection.Open(); |
||
365 | if (connection.State.Equals(ConnectionState.Open)) |
||
366 | { |
||
367 | using (SQLiteCommand cmd = connection.CreateCommand()) |
||
368 | { |
||
369 | cmd.CommandText = "SELECT p.[Id], p.Name, p.Path, s.DBTypes_UID, s.Host, s.[User], s.[Password], s.FilePath FROM DBSettings s, Projects p WHERE s.Projects_UID = p.[Id]"; |
||
370 | using (SQLiteDataReader dr = cmd.ExecuteReader()) |
||
371 | dt.Load(dr); |
||
372 | } |
||
373 | |||
374 | } |
||
375 | connection.Close(); |
||
376 | } |
||
377 | } |
||
378 | catch (Exception ex) |
||
379 | { |
||
380 | MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error); |
||
381 | } |
||
382 | 61004277 | esham21 | |
383 | foreach (DataRow row in dt.Rows) |
||
384 | { |
||
385 | string projectDir = row["FilePath"].ToString(); |
||
386 | |||
387 | FileInfo fileInfo = new FileInfo(projectDir); |
||
388 | if (fileInfo.Exists == false) |
||
389 | { |
||
390 | row.Delete(); |
||
391 | } |
||
392 | } |
||
393 | dt.AcceptChanges(); |
||
394 | dt.DefaultView.Sort = "Name"; |
||
395 | dt = dt.DefaultView.ToTable(); |
||
396 | |||
397 | return dt; |
||
398 | e6782243 | gaqhf | } |
399 | |||
400 | 9c451472 | gaqhf | |
401 | } |
||
402 | } |