프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / SPPIDConverter / DB / SPPID_DB.cs @ fdcf00d4

이력 | 보기 | 이력해설 | 다운로드 (14.3 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

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

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

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

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

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

    
51

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

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

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

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

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

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

    
150
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT PATH FROM {0}.T_ROOTITEM WHERE NAME = '{1}'", dbInfo.Site, dbInfo.Plant);
151
                                using (OracleDataReader reader = cmd.ExecuteReader())
152
                                {
153
                                    while (reader.Read())
154
                                    {
155
                                        dbInfo.PlantPath = reader["PATH"].ToString();
156
                                    }
157
                                }
158
                            }
159
                        }
160
                    }
161
                }
162
                else
163
                {
164

    
165
                }
166
            }
167
            catch (Exception ex)
168
            {
169
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
170
            }
171

    
172
            return bResult;
173
        }
174
        public static List<string> GetPlantList(SPPID_DBInfo dbInfo)
175
        {
176
            List<string> plantList = new List<string>();
177
            try
178
            {
179
                if (dbInfo.DBType == "ORACLE")
180
                {
181
                    string connString = string.Format(oracleConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
182
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
183

    
184
                    using (OracleConnection conn = new OracleConnection(connString))
185
                    {
186
                        conn.Open();
187
                        if (conn.State == System.Data.ConnectionState.Open)
188
                        {
189
                            using (OracleCommand cmd = new OracleCommand())
190
                            {
191
                                cmd.Connection = conn;
192
                                cmd.CommandText = string.Format(CultureInfo.CurrentCulture, "SELECT NAME, PATH FROM {0}.T_ROOTITEM", dbInfo.Site);
193

    
194
                                using (OracleDataReader reader = cmd.ExecuteReader())
195
                                {
196
                                    while (reader.Read())
197
                                    {
198
                                        plantList.Add(reader["NAME"].ToString());
199
                                    }
200
                                }
201
                            }
202
                        }
203
                    }
204
                }
205
                else
206
                {
207

    
208
                }
209
            }
210
            catch (Exception ex)
211
            {
212
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
213
            }
214

    
215
            return plantList;
216
        }
217
        
218
        public static DataTable GetSPPIDAttribute()
219
        {
220
            List<string> attributes = new List<string>();
221
            SPPID_DBInfo dbInfo = SPPID_DBInfo.GetInstance();
222
            DataTable dt = null;
223
            try
224
            {
225
                if (dbInfo.DBType == "ORACLE")
226
                {
227
                    string connString = string.Format(oracleConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
228
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
229

    
230
                    using (OracleConnection conn = new OracleConnection(connString))
231
                    {
232
                        conn.Open();
233
                        if (conn.State == System.Data.ConnectionState.Open)
234
                        {
235
                            string sQuery = string.Format(CultureInfo.CurrentCulture,
236
                                @"SELECT ITEM.description ItemType ,ATTR.displayname, ATTR.name FROM {0}.itemattributions ATTR 
237
                                LEFT OUTER JOIN {0}.item ITEM 
238
                                    ON ITEM.ID = ATTR.ITEMID 
239
                                ORDER BY ATTR.displayname ASC", dbInfo.PlantPIDDic);
240
                            //string sQuery = string.Format(CultureInfo.CurrentCulture,
241
                            //    @"SELECT DISTINCT(ATTR.displayname), ATTR.name FROM {0}.itemattributions ATTR
242
                            //    ORDER BY ATTR.displayname ASC", dbInfo.PlantPIDDic);
243
                            using (OracleCommand cmd = new OracleCommand(sQuery, conn))
244
                            using (OracleDataAdapter adapter = new OracleDataAdapter())
245
                            {
246
                                adapter.SelectCommand = cmd;
247
                                dt = new DataTable();
248
                                adapter.Fill(dt);
249
                            }
250
                        }
251
                    }
252
                }
253
                else
254
                {
255

    
256
                }
257
            }
258
            catch (Exception ex)
259
            {
260
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
261
            }
262

    
263
            return dt;
264
        }
265
        public static DataTable GetFluidSystemInfo(string operFluidCode)
266
        {
267
            List<string> attributes = new List<string>();
268
            SPPID_DBInfo dbInfo = SPPID_DBInfo.GetInstance();
269
            DataTable dt = null;
270
            try
271
            {
272
                if (dbInfo.DBType == "ORACLE")
273
                {
274
                    string connString = string.Format(oracleConnString, dbInfo.ServerIP, dbInfo.Port, dbInfo.Service);
275
                    connString = "Data Source=" + connString + ";User Id=" + dbInfo.DBUser + ";Password=" + dbInfo.DBPassword;
276

    
277
                    using (OracleConnection conn = new OracleConnection(connString))
278
                    {
279
                        conn.Open();
280
                        if (conn.State == System.Data.ConnectionState.Open)
281
                        {
282
                            string sQuery = string.Format(CultureInfo.CurrentCulture,
283
                                @"SELECT * FROM {0}.CODELISTS OUTATTR 
284
                                WHERE OUTATTR.codelist_index =  
285
                                    (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);
286
                            using (OracleCommand cmd = new OracleCommand(sQuery, conn))
287
                            using (OracleDataAdapter adapter = new OracleDataAdapter())
288
                            {
289
                                adapter.SelectCommand = cmd;
290
                                dt = new DataTable();
291
                                adapter.Fill(dt);
292
                            }
293
                        }
294
                    }
295
                }
296
                else
297
                {
298

    
299
                }
300
            }
301
            catch (Exception ex)
302
            {
303
                System.Windows.Forms.MessageBox.Show(ex.Message + "\r\n" + ex.StackTrace);
304
            }
305

    
306
            return dt;
307
        }
308
    }
309
}
클립보드 이미지 추가 (최대 크기: 500 MB)