프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / DB / SPPID_DB.cs @ 8e1eaa19

이력 | 보기 | 이력해설 | 다운로드 (23.1 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.Data;
7
using System.Globalization;
8
using System.IO;
9
using Oracle.ManagedDataAccess.Client;
10
using System.Data.SqlClient;
11

    
12
namespace Converter.SPPID.DB
13
{
14
    public class SPPID_DB
15
    {
16
        private const string oracleConnString = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST ={0}" + @")(PORT = {1}))(CONNECT_DATA = (SERVICE_NAME = {2})))";
17
        private const string sqlConnString = "(DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST ={0}" + @")(PORT = {1}))(CONNECT_DATA = (SERVICE_NAME = {2})))";
18

    
19
        public static bool CheckAndSetSPPIDInfo(SPPID_DBInfo dbInfo)
20
        {
21
            bool bResult = false;
22
            try
23
            {
24
                if (dbInfo.DBType == "ORACLE")
25
                {
26
                    string connString = string.Format(oracleConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
27
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
28

    
29
                    using (OracleConnection conn = new OracleConnection(connString))
30
                    {
31
                        conn.Open();
32
                        if (conn.State == System.Data.ConnectionState.Open)
33
                        {
34
                            using (OracleCommand cmd = new OracleCommand())
35
                            {
36
                                cmd.Connection = conn;
37
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT NAME, PATH FROM {0}.T_ROOTITEM", dbInfo.Site);
38

    
39
                                using (OracleDataReader reader = cmd.ExecuteReader())
40
                                {
41
                                    while (reader.Read())
42
                                    {
43
                                        dbInfo.Plant = reader["NAME"].ToString();
44
                                        dbInfo.PlantPath = reader["PATH"].ToString();
45
                                    }
46

    
47
                                    cmd.CommandText = string.Format(CultureInfo.CurrentCulture, @"SELECT SP_SCHEMA_TYPE, USERNAME
48
                                                FROM {0}.T_DB_DATA DB_DATA 
49
                                                INNER JOIN {0}.T_ROOTITEM ROOTITEM ON DB_DATA.SP_ROOTITEMID = ROOTITEM.SP_ID 
50
                                                WHERE ROOTITEM.NAME = '{1}'", dbInfo.Site, dbInfo.Plant);
51

    
52

    
53
                                    using (OracleDataAdapter adapter = new OracleDataAdapter())
54
                                    {
55
                                        DataTable dt = new DataTable();
56
                                        adapter.SelectCommand = cmd;
57
                                        adapter.Fill(dt);
58

    
59
                                        foreach (DataRow row in dt.Rows)
60
                                        {
61
                                            string sType = row["SP_SCHEMA_TYPE"].ToString();
62
                                            switch (sType)
63
                                            {
64
                                                case "SPPIDDATA_DICTIONARY":
65
                                                    dbInfo.PlantPIDDic = row["USERNAME"].ToString();
66
                                                    break;
67
                                                case "DATA_DICTIONARY":
68
                                                    dbInfo.PlantDic = row["USERNAME"].ToString();
69
                                                    break;
70
                                                case "SPAPLANT":
71
                                                    dbInfo.Plant = row["USERNAME"].ToString();
72
                                                    break;
73
                                                case "SPPID":
74
                                                    dbInfo.PlantPID = row["USERNAME"].ToString();
75
                                                    break;
76
                                                default:
77
                                                    break;
78
                                            }
79
                                        }
80
                                        bResult = true;
81
                                    }
82
                                }
83
                            }
84
                        }
85
                    }
86
                }
87
                else if (dbInfo.DBType == "SQLSERVER")
88
                {
89

    
90
                }
91
            }
92
            catch (Exception ex)
93
            {
94
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
95
            }
96

    
97
            return bResult;
98
        }
99
        public static bool SetPlantDBSetting(SPPID_DBInfo dbInfo)
100
        {
101
            bool bResult = false;
102
            try
103
            {
104
                if (dbInfo.DBType == "ORACLE")
105
                {
106
                    string connString = string.Format(oracleConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
107
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
108

    
109
                    using (OracleConnection conn = new OracleConnection(connString))
110
                    {
111
                        conn.Open();
112
                        if (conn.State == System.Data.ConnectionState.Open)
113
                        {
114
                            using (OracleCommand cmd = new OracleCommand())
115
                            {
116
                                cmd.Connection = conn;
117
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, @"SELECT SP_SCHEMA_TYPE, USERNAME
118
                                                FROM {0}.T_DB_DATA DB_DATA 
119
                                                INNER JOIN {0}.T_ROOTITEM ROOTITEM ON DB_DATA.SP_ROOTITEMID = ROOTITEM.SP_ID 
120
                                                WHERE ROOTITEM.NAME = '{1}'", dbInfo.Site, dbInfo.SelectedPlant);
121
                                using (OracleDataAdapter adapter = new OracleDataAdapter())
122
                                {
123
                                    DataTable dt = new DataTable();
124
                                    adapter.SelectCommand = cmd;
125
                                    adapter.Fill(dt);
126

    
127
                                    foreach (DataRow row in dt.Rows)
128
                                    {
129
                                        string sType = row["SP_SCHEMA_TYPE"].ToString();
130
                                        switch (sType)
131
                                        {
132
                                            case "SPPIDDATA_DICTIONARY":
133
                                                dbInfo.PlantPIDDic = row["USERNAME"].ToString();
134
                                                break;
135
                                            case "DATA_DICTIONARY":
136
                                                dbInfo.PlantDic = row["USERNAME"].ToString();
137
                                                break;
138
                                            case "SPAPLANT":
139
                                                dbInfo.Plant = row["USERNAME"].ToString();
140
                                                break;
141
                                            case "SPPID":
142
                                                dbInfo.PlantPID = row["USERNAME"].ToString();
143
                                                break;
144
                                            default:
145
                                                break;
146
                                        }
147
                                        bResult = true;
148
                                    }
149
                                }
150

    
151
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT PATH FROM {0}.T_ROOTITEM WHERE NAME = '{1}'", dbInfo.Site, dbInfo.Plant);
152
                                using (OracleDataReader reader = cmd.ExecuteReader())
153
                                {
154
                                    while (reader.Read())
155
                                    {
156
                                        dbInfo.PlantPath = reader["PATH"].ToString();
157
                                    }
158
                                }
159
                            }
160
                        }
161
                    }
162
                }
163
                else if (dbInfo.DBType == "SQLSERVER")
164
                {
165
                    string connString = string.Format("server = {0}; uid = {1}; pwd = {2}; database = {3};", dbInfo.Service, dbInfo.DBUser, dbInfo.DBPassword, dbInfo.SQLSERVER_DBNAME);
166
                    using (SqlConnection conn = new SqlConnection(connString))
167
                    {
168
                        conn.Open();
169
                        if (conn.State == ConnectionState.Open)
170
                        {
171
                            using (SqlCommand cmd = new SqlCommand())
172
                            {
173
                                cmd.Connection = conn;
174

    
175
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, @"SELECT SP_SCHEMA_TYPE, USERNAME, DATA_BASE
176
                                                FROM {0}.T_DB_DATA DB_DATA 
177
                                                INNER JOIN {0}.T_ROOTITEM ROOTITEM ON DB_DATA.SP_ROOTITEMID = ROOTITEM.SP_ID 
178
                                                WHERE ROOTITEM.NAME = '{1}'", dbInfo.Site, dbInfo.SelectedPlant);
179
                                using (SqlDataAdapter adapter = new SqlDataAdapter())
180
                                {
181
                                    DataTable dt = new DataTable();
182
                                    adapter.SelectCommand = cmd;
183
                                    adapter.Fill(dt);
184
                                    foreach (DataColumn item in dt.Columns)
185
                                        item.ColumnName = item.ColumnName.ToUpper();
186
                                    
187
                                    foreach (DataRow row in dt.Rows)
188
                                    {
189
                                        string sType = row["SP_SCHEMA_TYPE"].ToString();
190
                                        switch (sType)
191
                                        {
192
                                            case "SPPIDDATA_DICTIONARY":
193
                                                dbInfo.PlantPIDDic = row["USERNAME"].ToString();
194
                                                dbInfo.PlantPIDDic_DataBase = row["DATA_BASE"].ToString();
195
                                                break;
196
                                            case "DATA_DICTIONARY":
197
                                                dbInfo.PlantDic = row["USERNAME"].ToString();
198
                                                dbInfo.PlantDic_DataBase = row["DATA_BASE"].ToString();
199
                                                break;
200
                                            case "SPAPLANT":
201
                                                dbInfo.Plant = row["USERNAME"].ToString();
202
                                                dbInfo.Plant_DataBase = row["DATA_BASE"].ToString();
203
                                                break;
204
                                            case "SPPID":
205
                                                dbInfo.PlantPID = row["USERNAME"].ToString();
206
                                                dbInfo.PlantPID_DataBase = row["DATA_BASE"].ToString();
207
                                                break;
208
                                            default:
209
                                                break;
210
                                        }
211
                                        bResult = true;
212
                                    }
213
                                }
214

    
215
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT PATH FROM {0}.T_ROOTITEM WHERE NAME = '{1}'", dbInfo.Site, dbInfo.SelectedPlant);
216
                                using (SqlDataReader reader = cmd.ExecuteReader())
217
                                {
218
                                    while (reader.Read())
219
                                    {
220
                                        dbInfo.PlantPath = reader["PATH"].ToString();
221
                                    }
222
                                }
223
                            }
224
                        }
225
                    }
226
                }
227
            }
228
            catch (Exception ex)
229
            {
230
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
231
            }
232

    
233
            return bResult;
234
        }
235
        public static List<string> GetPlantList(SPPID_DBInfo dbInfo)
236
        {
237
            List<string> plantList = new List<string>();
238
            try
239
            {
240
                if (dbInfo.DBType == "ORACLE")
241
                {
242
                    string connString = string.Format(oracleConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
243
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
244

    
245
                    using (OracleConnection conn = new OracleConnection(connString))
246
                    {
247
                        conn.Open();
248
                        if (conn.State == System.Data.ConnectionState.Open)
249
                        {
250
                            using (OracleCommand cmd = new OracleCommand())
251
                            {
252
                                cmd.Connection = conn;
253
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT NAME, PATH FROM {0}.T_ROOTITEM", dbInfo.Site);
254

    
255
                                using (OracleDataReader reader = cmd.ExecuteReader())
256
                                {
257
                                    while (reader.Read())
258
                                    {
259
                                        plantList.Add(reader["NAME"].ToString());
260
                                    }
261
                                }
262
                            }
263
                        }
264
                    }
265
                }
266
                else if (dbInfo.DBType == "SQLSERVER")
267
                {
268
                    string connString = string.Format("server = {0}; uid = {1}; pwd = {2}; database = {3};", dbInfo.Service, dbInfo.DBUser, dbInfo.DBPassword, dbInfo.SQLSERVER_DBNAME);
269
                    using (SqlConnection conn = new SqlConnection(connString))
270
                    {
271
                        conn.Open();
272
                        if (conn.State == ConnectionState.Open)
273
                        {
274
                            using (SqlCommand cmd = new SqlCommand())
275
                            {
276
                                cmd.Connection = conn;
277
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT NAME, PATH FROM {0}.T_ROOTITEM", dbInfo.Site);
278

    
279
                                using (SqlDataReader reader = cmd.ExecuteReader())
280
                                {
281
                                    while (reader.Read())
282
                                    {
283
                                        plantList.Add(reader["NAME"].ToString());
284
                                    }
285
                                }
286
                            }
287

    
288
                        }
289
                    }
290
                }
291
            }
292
            catch (Exception ex)
293
            {
294
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
295
            }
296

    
297
            return plantList;
298
        }
299
        
300
        public static DataTable GetSPPIDAttribute()
301
        {
302
            List<string> attributes = new List<string>();
303
            SPPID_DBInfo dbInfo = SPPID_DBInfo.GetInstance();
304
            DataTable dt = null;
305
            string connString = string.Empty;
306
            try
307
            {
308
                if (dbInfo.DBType == "ORACLE")
309
                {
310
                    connString = string.Format(oracleConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
311
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
312

    
313
                    using (OracleConnection conn = new OracleConnection(connString))
314
                    {
315
                        conn.Open();
316
                        if (conn.State == System.Data.ConnectionState.Open)
317
                        {
318
                            string sQuery = string.Format(CultureInfo.CurrentCulture,
319
                                @"SELECT ITEM.description ItemType ,ATTR.displayname, ATTR.name FROM {0}.itemattributions ATTR 
320
                                LEFT OUTER JOIN {0}.item ITEM 
321
                                    ON ITEM.ID = ATTR.ITEMID 
322
                                ORDER BY ATTR.displayname ASC", dbInfo.PlantPIDDic);
323
                            //string sQuery = string.Format(CultureInfo.CurrentCulture,
324
                            //    @"SELECT DISTINCT(ATTR.displayname), ATTR.name FROM {0}.itemattributions ATTR
325
                            //    ORDER BY ATTR.displayname ASC", dbInfo.PlantPIDDic);
326
                            using (OracleCommand cmd = new OracleCommand(sQuery, conn))
327
                            using (OracleDataAdapter adapter = new OracleDataAdapter())
328
                            {
329
                                adapter.SelectCommand = cmd;
330
                                dt = new DataTable();
331
                                adapter.Fill(dt);
332
                                foreach (DataColumn item in dt.Columns)
333
                                    item.ColumnName = item.ColumnName.ToUpper();
334
                            }
335
                        }
336
                    }
337
                }
338
                else if (dbInfo.DBType == "SQLSERVER")
339
                {
340
                    connString = string.Format("server = {0}; uid = {1}; pwd = {2}; database = {3};", dbInfo.Service, dbInfo.DBUser, dbInfo.DBPassword, dbInfo.PlantPIDDic_DataBase);
341
                    using (SqlConnection conn = new SqlConnection(connString))
342
                    {
343
                        conn.Open();
344
                        if (conn.State == ConnectionState.Open)
345
                        {
346
                            string sQuery = string.Format(CultureInfo.CurrentCulture,
347
                                @"SELECT ITEM.description ItemType ,ATTR.displayname, ATTR.name FROM {0}.itemattributions ATTR 
348
                                LEFT OUTER JOIN {0}.item ITEM 
349
                                    ON ITEM.ID = ATTR.ITEMID 
350
                                ORDER BY ATTR.displayname ASC", dbInfo.PlantPIDDic);
351
                            //string sQuery = string.Format(CultureInfo.CurrentCulture,
352
                            //    @"SELECT DISTINCT(ATTR.displayname), ATTR.name FROM {0}.itemattributions ATTR
353
                            //    ORDER BY ATTR.displayname ASC", dbInfo.PlantPIDDic);
354
                            using (SqlCommand cmd = new SqlCommand(sQuery, conn))
355
                            using (SqlDataAdapter adapter = new SqlDataAdapter())
356
                            {
357
                                adapter.SelectCommand = cmd;
358
                                dt = new DataTable();
359
                                adapter.Fill(dt);
360
                                foreach (DataColumn item in dt.Columns)
361
                                    item.ColumnName = item.ColumnName.ToUpper();
362
                            }
363
                        }
364
                    }
365
                }
366

    
367
            }
368
            catch (Exception ex)
369
            {
370
                System.Windows.Forms.MessageBox.Show("Connection String : " + connString + "\r\n" + ex.Message + "\r\n" + ex.StackTrace);
371
            }
372

    
373
            return dt;
374
        }
375
        public static DataTable GetFluidSystemInfo(string operFluidCode)
376
        {
377
            List<string> attributes = new List<string>();
378
            SPPID_DBInfo dbInfo = SPPID_DBInfo.GetInstance();
379
            DataTable dt = null;
380
            try
381
            {
382
                if (dbInfo.DBType == "ORACLE")
383
                {
384
                    string connString = string.Format(oracleConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
385
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
386

    
387
                    using (OracleConnection conn = new OracleConnection(connString))
388
                    {
389
                        conn.Open();
390
                        if (conn.State == System.Data.ConnectionState.Open)
391
                        {
392
                            string sQuery = string.Format(CultureInfo.CurrentCulture,
393
                                @"SELECT * FROM {0}.CODELISTS OUTATTR 
394
                                WHERE OUTATTR.codelist_index =  
395
                                    (SELECT attr.codelist_constraint FROM {0}.CODELISTS ATTR WHERE ATTR.CODELIST_TEXT = '{1}' AND ATTR.CODELIST_NUMBER = 19) AND OUTATTR.CODELIST_NUMBER = 20", dbInfo.PlantPIDDic, operFluidCode);
396
                            using (OracleCommand cmd = new OracleCommand(sQuery, conn))
397
                            using (OracleDataAdapter adapter = new OracleDataAdapter())
398
                            {
399
                                adapter.SelectCommand = cmd;
400
                                dt = new DataTable();
401
                                adapter.Fill(dt);
402
                                foreach (DataColumn item in dt.Columns)
403
                                    item.ColumnName = item.ColumnName.ToUpper();
404
                            }
405
                        }
406
                    }
407
                }
408
                else if (dbInfo.DBType == "SQLSERVER")
409
                {
410
                    string connString = string.Format("server = {0}; uid = {1}; pwd = {2}; database = {3};", dbInfo.Service, dbInfo.DBUser, dbInfo.DBPassword, dbInfo.PlantPIDDic_DataBase);
411
                    using (SqlConnection conn = new SqlConnection(connString))
412
                    {
413
                        conn.Open();
414
                        if (conn.State == ConnectionState.Open)
415
                        {
416
                            string sQuery = string.Format(CultureInfo.CurrentCulture,
417
                                @"SELECT * FROM {0}.CODELISTS OUTATTR 
418
                                WHERE OUTATTR.codelist_index =  
419
                                    (SELECT attr.codelist_constraint FROM {0}.CODELISTS ATTR WHERE ATTR.CODELIST_TEXT = '{1}' AND ATTR.CODELIST_NUMBER = 19) AND OUTATTR.CODELIST_NUMBER = 20", dbInfo.PlantPIDDic, operFluidCode);
420
                            using (SqlCommand cmd = new SqlCommand(sQuery, conn))
421
                            using (SqlDataAdapter adapter = new SqlDataAdapter())
422
                            {
423
                                adapter.SelectCommand = cmd;
424
                                dt = new DataTable();
425
                                adapter.Fill(dt);
426
                                foreach (DataColumn item in dt.Columns)
427
                                    item.ColumnName = item.ColumnName.ToUpper();
428
                            }
429
                        }
430
                    }
431
                }
432
            }
433
            catch (Exception ex)
434
            {
435
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
436
            }
437

    
438
            return dt;
439
        }
440
    }
441
}
클립보드 이미지 추가 (최대 크기: 500 MB)