hytos / DTI_PID / SPPIDConverter_AutoModeling / DB / DB.cs @ eb5697b1
이력 | 보기 | 이력해설 | 다운로드 (14.6 KB)
1 |
using System; |
---|---|
2 |
using System.Collections.Generic; |
3 |
using System.Linq; |
4 |
using System.Text; |
5 |
using System.Threading.Tasks; |
6 |
using Oracle.ManagedDataAccess.Client; |
7 |
using Oracle.ManagedDataAccess.Types; |
8 |
using SPPID.Model; |
9 |
using System.Data; |
10 |
using System.Globalization; |
11 |
using System.IO; |
12 |
|
13 |
namespace SPPID.DB |
14 |
{ |
15 |
public class DB |
16 |
{ |
17 |
private const string oConnString = "(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST ={0}" + @")(PORT = {1})) ) (CONNECT_DATA = (SERVICE_NAME = {2})))"; |
18 |
|
19 |
public static bool CheckAndSetDBInformation() |
20 |
{ |
21 |
bool bResult = false; |
22 |
DBInformation dbInfo = DBInformation.GetInstance(); |
23 |
try |
24 |
{ |
25 |
if (dbInfo.DBType == "ORACLE") |
26 |
{ |
27 |
string connString = string.Format(oConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service); |
28 |
connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword; |
29 |
|
30 |
using (OracleConnection conn = new OracleConnection(connString)) |
31 |
{ |
32 |
conn.Open(); |
33 |
if (conn.State == System.Data.ConnectionState.Open) |
34 |
{ |
35 |
using (OracleCommand cmd = new OracleCommand()) |
36 |
{ |
37 |
cmd.Connection = conn; |
38 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT NAME, PATH FROM {0}.T_ROOTITEM", dbInfo.Site); |
39 |
|
40 |
using (OracleDataReader reader = cmd.ExecuteReader()) |
41 |
{ |
42 |
while (reader.Read()) |
43 |
{ |
44 |
dbInfo.Plant = reader["NAME"].ToString(); |
45 |
dbInfo.PlantPath = reader["PATH"].ToString(); |
46 |
} |
47 |
|
48 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, @"SELECT SP_SCHEMA_TYPE, USERNAME |
49 |
FROM {0}.T_DB_DATA DB_DATA |
50 |
INNER JOIN {0}.T_ROOTITEM ROOTITEM ON DB_DATA.SP_ROOTITEMID = ROOTITEM.SP_ID |
51 |
WHERE ROOTITEM.NAME = '{1}'", dbInfo.Site, dbInfo.Plant); |
52 |
|
53 |
|
54 |
using (OracleDataAdapter adapter = new OracleDataAdapter()) |
55 |
{ |
56 |
DataTable dt = new DataTable(); |
57 |
adapter.SelectCommand = cmd; |
58 |
adapter.Fill(dt); |
59 |
|
60 |
foreach (DataRow row in dt.Rows) |
61 |
{ |
62 |
string sType = row["SP_SCHEMA_TYPE"].ToString(); |
63 |
switch (sType) |
64 |
{ |
65 |
case "SPPIDDATA_DICTIONARY": |
66 |
dbInfo.PlantPIDDic = row["USERNAME"].ToString(); |
67 |
break; |
68 |
case "DATA_DICTIONARY": |
69 |
dbInfo.PlantDic = row["USERNAME"].ToString(); |
70 |
break; |
71 |
case "SPAPLANT": |
72 |
dbInfo.Plant = row["USERNAME"].ToString(); |
73 |
break; |
74 |
case "SPPID": |
75 |
dbInfo.PlantPID = row["USERNAME"].ToString(); |
76 |
break; |
77 |
default: |
78 |
break; |
79 |
} |
80 |
} |
81 |
bResult = true; |
82 |
} |
83 |
} |
84 |
} |
85 |
} |
86 |
} |
87 |
} |
88 |
else |
89 |
{ |
90 |
|
91 |
} |
92 |
} |
93 |
catch (Exception ex) |
94 |
{ |
95 |
|
96 |
} |
97 |
|
98 |
return bResult; |
99 |
} |
100 |
|
101 |
public static DataTable GetUnitTree() |
102 |
{ |
103 |
DataTable dt = new DataTable(); |
104 |
try |
105 |
{ |
106 |
DBInformation dbInfo = DBInformation.GetInstance(); |
107 |
|
108 |
if (dbInfo.DBType == "ORACLE") |
109 |
{ |
110 |
string connString = string.Format(oConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service); |
111 |
connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword; |
112 |
|
113 |
using (OracleConnection conn = new OracleConnection(connString)) |
114 |
{ |
115 |
conn.Open(); |
116 |
if (conn.State == System.Data.ConnectionState.Open) |
117 |
{ |
118 |
string sQuery = string.Format(CultureInfo.CurrentCulture, |
119 |
@"SELECT LEVEL, SP_ID, NAME, PARENTID |
120 |
FROM {0}.T_PLANTGROUP |
121 |
START WITH PARENTID='-1' |
122 |
CONNECT BY PRIOR SP_ID=PARENTID", dbInfo.Plant); |
123 |
|
124 |
using (OracleCommand cmd = new OracleCommand(sQuery, conn)) |
125 |
using (OracleDataAdapter adapter = new OracleDataAdapter()) |
126 |
{ |
127 |
adapter.SelectCommand = cmd; |
128 |
adapter.Fill(dt); |
129 |
} |
130 |
} |
131 |
} |
132 |
} |
133 |
else |
134 |
{ |
135 |
|
136 |
} |
137 |
} |
138 |
catch (Exception ex) |
139 |
{ |
140 |
|
141 |
} |
142 |
|
143 |
return dt; |
144 |
} |
145 |
|
146 |
public static string GetPlantPID_T_OPTIONSETTING_Value(string name) |
147 |
{ |
148 |
string TemplatePath = string.Empty; |
149 |
DBInformation dbInfo = DBInformation.GetInstance(); |
150 |
try |
151 |
{ |
152 |
if (dbInfo.DBType == "ORACLE") |
153 |
{ |
154 |
string connString = string.Format(oConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service); |
155 |
connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword; |
156 |
|
157 |
using (OracleConnection conn = new OracleConnection(connString)) |
158 |
{ |
159 |
conn.Open(); |
160 |
if (conn.State == System.Data.ConnectionState.Open) |
161 |
{ |
162 |
using (OracleCommand cmd = new OracleCommand()) |
163 |
{ |
164 |
cmd.Connection = conn; |
165 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT VALUE FROM {0}.T_OPTIONSETTING WHERE NAME = '{1}'", dbInfo.PlantPID, name); |
166 |
|
167 |
using (OracleDataReader reader = cmd.ExecuteReader()) |
168 |
{ |
169 |
while (reader.Read()) |
170 |
{ |
171 |
TemplatePath = reader["VALUE"].ToString(); |
172 |
break; |
173 |
} |
174 |
} |
175 |
} |
176 |
} |
177 |
} |
178 |
} |
179 |
else |
180 |
{ |
181 |
|
182 |
} |
183 |
|
184 |
} |
185 |
catch (Exception ex) |
186 |
{ |
187 |
|
188 |
} |
189 |
|
190 |
return TemplatePath; |
191 |
} |
192 |
|
193 |
public static List<string> GetSPPIDAttribute() |
194 |
{ |
195 |
List<string> attributes = new List<string>(); |
196 |
DBInformation dbInfo = DBInformation.GetInstance(); |
197 |
try |
198 |
{ |
199 |
if (dbInfo.DBType == "ORACLE") |
200 |
{ |
201 |
string connString = string.Format(oConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service); |
202 |
connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword; |
203 |
|
204 |
using (OracleConnection conn = new OracleConnection(connString)) |
205 |
{ |
206 |
conn.Open(); |
207 |
if (conn.State == System.Data.ConnectionState.Open) |
208 |
{ |
209 |
using (OracleCommand cmd = new OracleCommand()) |
210 |
{ |
211 |
cmd.Connection = conn; |
212 |
|
213 |
// 정리 필요 |
214 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_EQUIPMENT"); |
215 |
using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) |
216 |
{ |
217 |
DataTable dt = reader.GetSchemaTable(); |
218 |
foreach (DataRow row in dt.Rows) |
219 |
attributes.Add(row["ColumnName"].ToString()); |
220 |
} |
221 |
|
222 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_NOZZLE"); |
223 |
using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) |
224 |
{ |
225 |
DataTable dt = reader.GetSchemaTable(); |
226 |
foreach (DataRow row in dt.Rows) |
227 |
attributes.Add(row["ColumnName"].ToString()); |
228 |
} |
229 |
|
230 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_PIPINGCOMP"); |
231 |
using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) |
232 |
{ |
233 |
DataTable dt = reader.GetSchemaTable(); |
234 |
foreach (DataRow row in dt.Rows) |
235 |
attributes.Add(row["ColumnName"].ToString()); |
236 |
} |
237 |
|
238 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_PIPERUN"); |
239 |
using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) |
240 |
{ |
241 |
|
242 |
DataTable dt = reader.GetSchemaTable(); |
243 |
foreach (DataRow row in dt.Rows) |
244 |
attributes.Add(row["ColumnName"].ToString()); |
245 |
} |
246 |
|
247 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_INSTRUMENT"); |
248 |
using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) |
249 |
{ |
250 |
DataTable dt = reader.GetSchemaTable(); |
251 |
foreach (DataRow row in dt.Rows) |
252 |
attributes.Add(row["ColumnName"].ToString()); |
253 |
} |
254 |
|
255 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_INLINECOMP"); |
256 |
using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) |
257 |
{ |
258 |
DataTable dt = reader.GetSchemaTable(); |
259 |
foreach (DataRow row in dt.Rows) |
260 |
attributes.Add(row["ColumnName"].ToString()); |
261 |
} |
262 |
|
263 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_VESSEL"); |
264 |
using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) |
265 |
{ |
266 |
DataTable dt = reader.GetSchemaTable(); |
267 |
foreach (DataRow row in dt.Rows) |
268 |
attributes.Add(row["ColumnName"].ToString()); |
269 |
} |
270 |
|
271 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_EXCHANGER"); |
272 |
using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) |
273 |
{ |
274 |
DataTable dt = reader.GetSchemaTable(); |
275 |
foreach (DataRow row in dt.Rows) |
276 |
attributes.Add(row["ColumnName"].ToString()); |
277 |
} |
278 |
|
279 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_MECHANICAL"); |
280 |
using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) |
281 |
{ |
282 |
DataTable dt = reader.GetSchemaTable(); |
283 |
foreach (DataRow row in dt.Rows) |
284 |
attributes.Add(row["ColumnName"].ToString()); |
285 |
} |
286 |
|
287 |
cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT * FROM {0}.{1}", dbInfo.PlantPID, "T_EQUIPCOMPONENT"); |
288 |
using (OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.SchemaOnly)) |
289 |
{ |
290 |
DataTable dt = reader.GetSchemaTable(); |
291 |
foreach (DataRow row in dt.Rows) |
292 |
attributes.Add(row["ColumnName"].ToString()); |
293 |
} |
294 |
|
295 |
} |
296 |
} |
297 |
} |
298 |
} |
299 |
else |
300 |
{ |
301 |
|
302 |
} |
303 |
} |
304 |
catch (Exception ex) |
305 |
{ |
306 |
|
307 |
} |
308 |
|
309 |
|
310 |
|
311 |
return attributes.Distinct().ToList(); |
312 |
} |
313 |
} |
314 |
} |