프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / APIDConverter / DB / Project_DB.cs @ 534ca9c5

이력 | 보기 | 이력해설 | 다운로드 (14.5 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.Globalization;
7
using System.Data.SQLite;
8
using System.Data;
9
using System.Data.SqlClient;
10
using System.IO;
11
using Newtonsoft.Json;
12
using AVEVA.PID.CustomizationUtility;
13
using AVEVA.PID.CustomizationUtility.Model;
14

    
15
namespace AVEVA.PID.CustomizationUtility.DB
16
{
17
    public class Project_DB
18
    {
19
        const string SPPID_DB_INFO_TABLE = "T_SPPID_CONNECTION_INFO";
20
        const string SPPID_SYMBOL_MAPPING_TABLE = "T_SPPID_SYMBOL_MAPPING";
21
        const string SPPID_ATTRIBUTE_MAPPING_TABLE = "T_SPPID_ATTRIBUTE_MAPPING";
22
        const string SPPID_SETTING_TABLE = "T_SPPID_SETTING_TABLE";
23
        const string SPPID_LABEL_INFO_TABLE = "T_SPPID_LABEL_INFO";
24
        const string SPPID_DRAWING_INFO = "T_SPPID_DRAWING_INFO";
25
        const string SPPID_OPC_INFO = "T_SPPID_OPC_INFO";
26

    
27
        const string LineProperties_TABLE = "LineProperties";
28
        const string LineTypes_TABLE = "LineTypes";
29
        const string SymbolType_TABLE = "SymbolType";
30
        const string SymbolAttribute_TABLE = "SymbolAttribute";
31
        const string Symbol_TABLE = "Symbol";
32
        const string OPCRelations_TABLE = "OPCRelations";
33

    
34
        private static SqlConnection GetSqlConnection()
35
        {
36
            Project_Info projectInfo = Project_Info.GetInstance();
37
            SqlConnection connection = null;
38
            try
39
            {
40
                connection = new SqlConnection(string.Format(CultureInfo.CurrentCulture,
41
                    @"Data Source = {0},{1}; Initial CataLog = {2}; User ID = {3}; Password = {4};",
42
                    projectInfo.ServerIP,
43
                    projectInfo.Port,
44
                    System.IO.Path.GetFileName(projectInfo.DefaultPath),
45
                    projectInfo.DBUser,
46
                    projectInfo.DBPassword));
47

    
48
                connection.Open();
49
            }
50
            catch (Exception ex)
51
            {
52
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
53
                if (connection != null)
54
                    connection.Dispose();
55
                connection = null;
56
            }
57

    
58
            return connection;
59
        }
60

    
61
        public static bool ConnTestAndCreateTable()
62
        {
63
            bool result = false;
64
            Project_Info projectInfo = Project_Info.GetInstance();
65
            if (projectInfo.DBType == ID2DB_Type.SQLite)
66
            {
67
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath)))
68
                {
69
                    try
70
                    {
71
                        connection.Open();
72
                        if (connection.State == ConnectionState.Open)
73
                        {
74
                            using (SQLiteCommand cmd = connection.CreateCommand())
75
                            {
76
                                
77
                            }
78
                            result = true;
79
                        }
80
                        connection.Close();
81
                    }
82
                    catch (Exception ex)
83
                    {
84
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
85
                    }
86
                    finally
87
                    {
88
                        connection.Dispose();
89
                    }
90
                }
91
            }
92
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
93
            {
94
                using (SqlConnection connection = GetSqlConnection())
95
                {
96
                    try
97
                    {
98
                        if (connection != null && connection.State == ConnectionState.Open)
99
                        {
100
                            using (SqlCommand cmd = connection.CreateCommand())
101
                            {
102
                                
103
                            }
104
                            result = true;
105
                        }
106
                    }
107
                    catch (Exception ex)
108
                    {
109
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
110
                    }
111
                    finally
112
                    {
113
                        if (connection != null)
114
                            connection.Dispose();
115
                    }
116
                }
117
            }
118

    
119
            return result;
120
        }
121

    
122
        public static DataTable SelectID2SymbolTable()
123
        {
124
            DataTable dt = new DataTable();
125
            Project_Info projectInfo = Project_Info.GetInstance();
126
            if (projectInfo.DBType == ID2DB_Type.SQLite)
127
            {
128
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
129
                {
130
                    try
131
                    {
132
                        connection.Open();
133
                        using (SQLiteCommand cmd = connection.CreateCommand())
134
                        {
135
                            cmd.CommandText = @"SELECT * FROM Symbol";
136
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
137
                                dt.Load(dr);
138
                        }
139
                        connection.Close();
140
                    }
141
                    catch (Exception ex)
142
                    {
143
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
144
                    }
145
                    finally
146
                    {
147
                        connection.Dispose();
148
                    }
149
                }
150
            }
151
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
152
            {
153
                using (SqlConnection connection = GetSqlConnection())
154
                {
155
                    try
156
                    {
157
                        if (connection != null && connection.State == ConnectionState.Open)
158
                        {
159
                            using (SqlCommand cmd = connection.CreateCommand())
160
                            {
161
                                cmd.CommandText = @"SELECT * FROM Symbol";
162
                                using (SqlDataReader dr = cmd.ExecuteReader())
163
                                    dt.Load(dr);
164
                            }
165
                            connection.Close();
166
                        }
167
                    }
168
                    catch (Exception ex)
169
                    {
170
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
171
                    }
172
                    finally
173
                    {
174
                        if (connection != null)
175
                            connection.Dispose();
176
                    }
177
                }
178
            }
179

    
180
            return dt;
181
        }
182
        public static DataTable SelectSymbolType()
183
        {
184
            DataTable dt = new DataTable();
185
            Project_Info projectInfo = Project_Info.GetInstance();
186
            if (projectInfo.DBType == ID2DB_Type.SQLite)
187
            {
188
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
189
                {
190
                    try
191
                    {
192
                        connection.Open();
193
                        using (SQLiteCommand cmd = connection.CreateCommand())
194
                        {
195
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
196
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
197
                                dt.Load(dr);
198
                        }
199
                        connection.Close();
200
                    }
201
                    catch (Exception ex)
202
                    {
203
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
204
                    }
205
                    finally
206
                    {
207
                        connection.Dispose();
208
                    }
209
                }
210
            }
211
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
212
            {
213
                using (SqlConnection connection = GetSqlConnection())
214
                {
215
                    try
216
                    {
217
                        if (connection != null && connection.State == ConnectionState.Open)
218
                        {
219
                            using (SqlCommand cmd = connection.CreateCommand())
220
                            {
221
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
222
                                using (SqlDataReader dr = cmd.ExecuteReader())
223
                                    dt.Load(dr);
224
                            }
225
                            connection.Close();
226
                        }
227
                    }
228
                    catch (Exception ex)
229
                    {
230
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
231
                    }
232
                    finally
233
                    {
234
                        if (connection != null)
235
                            connection.Dispose();
236
                    }
237
                }
238
            }
239

    
240
            return dt;
241
        }
242
        public static DataTable SelectDrawings()
243
        {
244
            DataTable dt = new DataTable();
245
            Project_Info projectInfo = Project_Info.GetInstance();
246
            if (projectInfo.DBType == ID2DB_Type.SQLite)
247
            {
248
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
249
                {
250
                    try
251
                    {
252
                        connection.Open();
253
                        using (SQLiteCommand cmd = connection.CreateCommand())
254
                        {
255
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
256
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
257
                                dt.Load(dr);
258
                        }
259
                        connection.Close();
260
                    }
261
                    catch (Exception ex)
262
                    {
263
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
264
                    }
265
                    finally
266
                    {
267
                        connection.Dispose();
268
                    }
269
                }
270
            }
271
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
272
            {
273
                using (SqlConnection connection = GetSqlConnection())
274
                {
275
                    try
276
                    {
277
                        if (connection != null && connection.State == ConnectionState.Open)
278
                        {
279
                            using (SqlCommand cmd = connection.CreateCommand())
280
                            {
281
                                cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
282
                                using (SqlDataReader dr = cmd.ExecuteReader())
283
                                    dt.Load(dr);
284
                            }
285
                            connection.Close();
286
                        }
287
                    }
288
                    catch (Exception ex)
289
                    {
290
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
291
                    }
292
                    finally
293
                    {
294
                        if (connection != null)
295
                            connection.Dispose();
296
                    }
297
                }
298
            }
299

    
300
            return dt;
301
        }
302

    
303

    
304
        #region AVEVA
305
        public static string GetAvevaConnectionString()
306
        {
307
            string strConn = string.Empty;
308
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
309
            {
310
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);
311
            }
312
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
313
            {
314
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
315
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
316
            }
317

    
318
            return strConn;
319
        }
320

    
321
        public static string GetAvevaConnectionString_Reports()
322
        {
323
            string strConn = string.Empty;
324
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
325
            {
326
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
327
            }
328
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
329
            {
330
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
331
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
332
            }
333

    
334
            return strConn;
335
        }
336

    
337
        public static string GetAvevaConnectionString_Admin()
338
        {
339
            string strConn = string.Empty;
340
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
341
            {
342
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName);
343
            }
344
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
345
            {
346
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
347
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString);
348
            }
349

    
350
            return strConn;
351
        }
352

    
353
        public static DataTable SelectSymbolTable()
354
        {
355
            DataTable dt = new DataTable();
356

    
357
            using (SqlConnection connection = new SqlConnection())
358
            {
359
                connection.ConnectionString = GetAvevaConnectionString_Admin();
360
                connection.Open();
361
                using (SqlCommand cmd = connection.CreateCommand())
362
                {
363
                    cmd.CommandText = string.Format("SELECT * FROM {0}", "SYMBOLS");
364
                    using (SqlDataReader dr = cmd.ExecuteReader())
365
                        dt.Load(dr);
366
                }
367
                connection.Close();
368
            }
369

    
370
            return dt;
371
        }
372

    
373
        #endregion
374

    
375
    }
376
}
클립보드 이미지 추가 (최대 크기: 500 MB)