프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / POSCO_Report / DB.cs @ 762819ff

이력 | 보기 | 이력해설 | 다운로드 (10.3 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
11
namespace POSCO_Report
12
{
13
	
14
15
	public static class DB
16
	{
17
		const string HMB_From_To = "HMB_From_To";
18
		const string HMB_LIST = "HMB_LIST";
19
		const string HMB_VALUE = "HMB_VALUE";
20
		const string Stream_No = "Stream_No";
21
		const string Components = "Components";
22
		const string Attributes = "Attributes";
23
		const string Symbol = "Symbol";
24
		const string SymbolType = "SymbolType";
25
		const string SymbolAttribute = "SymbolAttribute";
26
27
		public static bool CheckConnection()
28
		{
29
			ProjectInformation projectInformation = ProjectInformation.GetInstance();
30
			bool result = false;
31
			try
32
			{
33
				if (projectInformation.DBType.Equals(DBType.SQLite))
34
				{
35
					using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true))
36
					{
37
						connection.Open();
38
						if (connection.State.Equals(ConnectionState.Open))
39
						{
40
                            using (SQLiteCommand cmd = connection.CreateCommand())
41
                            {
42
                                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
43
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
44
                                using (DataTable dt = new DataTable())
45
                                {
46
                                    dt.Load(dr);
47
48
                                    if (dt.Select(string.Format("NAME = '{0}'", HMB_From_To)).Length.Equals(1) &&
49
										dt.Select(string.Format("NAME = '{0}'", HMB_LIST)).Length.Equals(1) &&
50
										dt.Select(string.Format("NAME = '{0}'", HMB_VALUE)).Length.Equals(1) &&
51
										dt.Select(string.Format("NAME = '{0}'", Stream_No)).Length.Equals(1) &&
52
										dt.Select(string.Format("NAME = '{0}'", Components)).Length.Equals(1) &&
53
										dt.Select(string.Format("NAME = '{0}'", Attributes)).Length.Equals(1))
54
                                    {
55
										result = true;
56
									}
57
                                }
58
                            }
59
                            
60
						}
61
						connection.Close();
62
					}
63
				}
64
				else if (projectInformation.DBType.Equals(DBType.MSSQL))
65
				{
66
					throw new Exception("mssql");
67
				}
68
			}
69
			catch (Exception ex)
70
			{
71
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
72
			}
73
			
74
			return result;
75
		}
76
		public static DataTable GetSymbolTable()
77
		{
78
			DataTable dt = new DataTable();
79
			ProjectInformation projectInformation = ProjectInformation.GetInstance();
80
			try
81
			{
82
				if (projectInformation.DBType.Equals(DBType.SQLite))
83
				{
84
					using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true))
85
					{
86
						connection.Open();
87
						if (connection.State.Equals(ConnectionState.Open))
88
						{
89
							using (SQLiteCommand cmd = connection.CreateCommand())
90
							{
91
								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", Symbol, SymbolType);
92
								using (SQLiteDataReader dr = cmd.ExecuteReader())
93
									dt.Load(dr);
94
							}
95
96
						}
97
						connection.Close();
98
					}
99
				}
100
				else if (projectInformation.DBType.Equals(DBType.MSSQL))
101
				{
102
					throw new Exception("mssql");
103
				}
104
			}
105
			catch (Exception ex)
106
			{
107
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
108
			}
109
			return dt;
110
		}
111
		public static DataTable GetSymbolAttributeTable()
112
		{
113
			DataTable dt = new DataTable();
114
			ProjectInformation projectInformation = ProjectInformation.GetInstance();
115
			try
116
			{
117
				if (projectInformation.DBType.Equals(DBType.SQLite))
118
				{
119
					using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true))
120
					{
121
						connection.Open();
122
						if (connection.State.Equals(ConnectionState.Open))
123
						{
124
							using (SQLiteCommand cmd = connection.CreateCommand())
125
							{
126
								cmd.CommandText = string.Format("SELECT DISTINCT Attribute FROM {0}", SymbolAttribute);
127
								using (SQLiteDataReader dr = cmd.ExecuteReader())
128
									dt.Load(dr);
129
							}
130
131
						}
132
						connection.Close();
133
					}
134
				}
135
				else if (projectInformation.DBType.Equals(DBType.MSSQL))
136
				{
137
					throw new Exception("mssql");
138
				}
139
			}
140
			catch (Exception ex)
141
			{
142
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
143
			}
144
			return dt;
145
		}
146
		public static DataTable GetHMBCaseDataTable()
147
		{
148
			DataTable dt = new DataTable();
149
			ProjectInformation projectInformation = ProjectInformation.GetInstance();
150
			try
151
			{
152
				if (projectInformation.DBType.Equals(DBType.SQLite))
153
				{
154
					using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true))
155
					{
156
						connection.Open();
157
						if (connection.State.Equals(ConnectionState.Open))
158
						{
159
							using (SQLiteCommand cmd = connection.CreateCommand())
160
							{
161
								cmd.CommandText = string.Format(@"
162
								SELECT hv.UID, hv.Value, hv.[Case], hv.HMB_LIST_UID, hl.Name, hl.Unit, hl.Type, st.Stream_No
163
								FROM {0} hv 
164
								LEFT JOIN {1} hl 
165
								ON hv.HMB_LIST_UID = hl.UID
166
								LEFT JOIN {2} st
167
								ON hv.Stream_No_UID = st.UID", HMB_VALUE, HMB_LIST, Stream_No);
168
								using (SQLiteDataReader dr = cmd.ExecuteReader())
169
									dt.Load(dr);
170
							}
171
172
						}
173
						connection.Close();
174
					}
175
				}
176
				else if (projectInformation.DBType.Equals(DBType.MSSQL))
177
				{
178
					throw new Exception("mssql");
179
				}
180
			}
181
			catch (Exception ex)
182
			{
183
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
184
			}
185
			return dt;
186
		}
187
		public static DataTable GetHMBListDataTable()
188
		{
189
			DataTable dt = new DataTable();
190
			ProjectInformation projectInformation = ProjectInformation.GetInstance();
191
			try
192
			{
193
				if (projectInformation.DBType.Equals(DBType.SQLite))
194
				{
195
					using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true))
196
					{
197
						connection.Open();
198
						if (connection.State.Equals(ConnectionState.Open))
199
						{
200
							using (SQLiteCommand cmd = connection.CreateCommand())
201
							{
202
								cmd.CommandText = string.Format(@"
203
								SELECT *
204
								FROM {0}", HMB_LIST);
205
								using (SQLiteDataReader dr = cmd.ExecuteReader())
206
									dt.Load(dr);
207
							}
208
209
						}
210
						connection.Close();
211
					}
212
				}
213
				else if (projectInformation.DBType.Equals(DBType.MSSQL))
214
				{
215
					throw new Exception("mssql");
216
				}
217
			}
218
			catch (Exception ex)
219
			{
220
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
221
			}
222
			return dt;
223
		}
224
225
		public static DataTable GetSymbolStreamData(string streamNoAttrName)
226
		{
227
			DataTable dt = new DataTable();
228
			ProjectInformation projectInformation = ProjectInformation.GetInstance();
229
			try
230
			{
231
				if (projectInformation.DBType.Equals(DBType.SQLite))
232
				{
233
					using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true))
234
					{
235
						connection.Open();
236
						if (connection.State.Equals(ConnectionState.Open))
237
						{
238
							using (SQLiteCommand cmd = connection.CreateCommand())
239
							{
240
								cmd.CommandText = string.Format(@"
241
								SELECT c.UID, c.SYMBOL_UID, a.[VALUE], sa.ATTRIBUTE 
242
								FROM COMPONENTS c, Attributes a, SymbolAttribute sa
243
								WHERE c.UID = a.COMPONENTS_UID AND a.SymbolAttribute_UID = sa.UID AND sa.Attribute = '{0}'", streamNoAttrName);
244
								using (SQLiteDataReader dr = cmd.ExecuteReader())
245
									dt.Load(dr);
246
							}
247
248
						}
249
						connection.Close();
250
					}
251
				}
252
				else if (projectInformation.DBType.Equals(DBType.MSSQL))
253
				{
254
					throw new Exception("mssql");
255
				}
256
			}
257
			catch (Exception ex)
258
			{
259
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
260
			}
261
			return dt;
262
		}
263
264
		public static DataTable GetSymbolItemData(string itemAttrName)
265
		{
266
			DataTable dt = new DataTable();
267
			ProjectInformation projectInformation = ProjectInformation.GetInstance();
268
			try
269
			{
270
				if (projectInformation.DBType.Equals(DBType.SQLite))
271
				{
272
					using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true))
273
					{
274
						connection.Open();
275
						if (connection.State.Equals(ConnectionState.Open))
276
						{
277
							using (SQLiteCommand cmd = connection.CreateCommand())
278
							{
279
								cmd.CommandText = string.Format(@"
280
								SELECT c.UID, c.SYMBOL_UID, a.[VALUE], sa.ATTRIBUTE 
281
								FROM COMPONENTS c, Attributes a, SymbolAttribute sa
282
								WHERE c.UID = a.COMPONENTS_UID AND a.SymbolAttribute_UID = sa.UID AND sa.Attribute = '{0}'", itemAttrName);
283
								using (SQLiteDataReader dr = cmd.ExecuteReader())
284
									dt.Load(dr);
285
							}
286
287
						}
288
						connection.Close();
289
					}
290
				}
291
				else if (projectInformation.DBType.Equals(DBType.MSSQL))
292
				{
293
					throw new Exception("mssql");
294
				}
295
			}
296
			catch (Exception ex)
297
			{
298
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
299
			}
300
			return dt;
301
		}
302
303
		public static string GetAttributeValue(string componentUID, string attrName)
304
		{
305
			string result = string.Empty;
306
307
			ProjectInformation projectInformation = ProjectInformation.GetInstance();
308
			try
309
			{
310
				if (projectInformation.DBType.Equals(DBType.SQLite))
311
				{
312
					using (SQLiteConnection connection = new SQLiteConnection(projectInformation.ConnString, true))
313
					{
314
						connection.Open();
315
						if (connection.State.Equals(ConnectionState.Open))
316
						{
317
							using (SQLiteCommand cmd = connection.CreateCommand())
318
							{
319
								DataTable dt = new DataTable();
320
								cmd.CommandText = string.Format(@"
321
								SELECT c.UID, c.SYMBOL_UID, a.[VALUE], sa.ATTRIBUTE 
322
								FROM COMPONENTS c, Attributes a, SymbolAttribute sa
323
								WHERE c.UID = a.COMPONENTS_UID AND a.SymbolAttribute_UID = sa.UID AND sa.Attribute = '{0}' AND c.UID = '{1}'", attrName, componentUID);
324
								using (SQLiteDataReader dr = cmd.ExecuteReader())
325
									dt.Load(dr);
326
327
								if (dt.Rows.Count.Equals(1))
328
									result = dt.Rows[0]["VALUE"].ToString();
329
							}
330
331
						}
332
						connection.Close();
333
					}
334
				}
335
				else if (projectInformation.DBType.Equals(DBType.MSSQL))
336
				{
337
					throw new Exception("mssql");
338
				}
339
			}
340
			catch (Exception ex)
341
			{
342
				MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
343
			}
344
345
			return result;
346
		}
347
348
349
	}
350
}
클립보드 이미지 추가 (최대 크기: 500 MB)