프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / DB / SPPID_DB.cs @ 04133d37

이력 | 보기 | 이력해설 | 다운로드 (23 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
            try
306
            {
307
                if (dbInfo.DBType == "ORACLE")
308
                {
309
                    string connString = string.Format(oracleConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
310
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
311

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

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

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

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

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