프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / APIDConverter / DB / Project_DB.cs @ a03cde1e

이력 | 보기 | 이력해설 | 다운로드 (33.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.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 APID_SYMBOL_MAPPING_TABLE = "T_APID_SYMBOL_MAPPING";
20
        const string APID_OPC_MAPPING_TABLE = "T_APID_OPC_MAPPING";
21

    
22
        const string LineProperties_TABLE = "LineProperties";
23
        const string LineTypes_TABLE = "LineTypes";
24
        const string SymbolType_TABLE = "SymbolType";
25
        const string SymbolAttribute_TABLE = "SymbolAttribute";
26
        const string Symbol_TABLE = "Symbol";
27
        const string OPCRelations_TABLE = "OPCRelations";
28

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

    
44
                connection.Open();
45
            }
46
            catch (Exception ex)
47
            {
48
                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
49
                if (connection != null)
50
                    connection.Dispose();
51
                connection = null;
52
            }
53

    
54
            return connection;
55
        }
56
        public static bool ConnTestAndCreateTable()
57
        {
58
            bool result = false;
59
            Project_Info projectInfo = Project_Info.GetInstance();
60
            if (projectInfo.DBType == ID2DB_Type.SQLite)
61
            {
62
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, @"Data Source = {0}", projectInfo.DBFilePath)))
63
                {
64
                    try
65
                    {
66
                        connection.Open();
67
                        if (connection.State == ConnectionState.Open)
68
                        {
69
                            using (SQLiteCommand cmd = connection.CreateCommand())
70
                            {
71
                                cmd.CommandText = "SELECT NAME FROM sqlite_master WHERE type='table'";
72
                                using (SQLiteDataReader dr = cmd.ExecuteReader())
73
                                using (DataTable dt = new DataTable())
74
                                {
75
                                    dt.Load(dr);
76

    
77
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_SYMBOL_MAPPING_TABLE)).Length == 0)
78
                                    {
79
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, NAME TEXT, APID_SYMBOL TEXT, DATA1 TEXT)", APID_SYMBOL_MAPPING_TABLE);
80
                                        cmd.ExecuteNonQuery();
81
                                    }
82
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_OPC_MAPPING_TABLE)).Length == 0)
83
                                    {
84
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID TEXT PRIMARY KEY, IN_SYMBOL TEXT, OUT_SYMBOL TEXT, Type INT)", APID_OPC_MAPPING_TABLE);
85
                                        cmd.ExecuteNonQuery();
86
                                    }
87
                                }
88
                            }
89
                            result = true;
90
                        }
91
                        connection.Close();
92
                    }
93
                    catch (Exception ex)
94
                    {
95
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
96
                    }
97
                    finally
98
                    {
99
                        connection.Dispose();
100
                    }
101
                }
102
            }
103
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
104
            {
105
                using (SqlConnection connection = GetSqlConnection())
106
                {
107
                    try
108
                    {
109
                        if (connection != null && connection.State == ConnectionState.Open)
110
                        {
111
                            using (SqlCommand cmd = connection.CreateCommand())
112
                            {
113
                                cmd.CommandText = "SELECT TABLE_NAME AS NAME FROM INFORMATION_SCHEMA.TABLES";
114
                                using (SqlDataReader dr = cmd.ExecuteReader())
115
                                using (DataTable dt = new DataTable())
116
                                {
117
                                    dt.Load(dr);
118

    
119
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_SYMBOL_MAPPING_TABLE)).Length == 0)
120
                                    {
121
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, NAME varchar(MAX), APID_SYMBOL varchar(MAX), DATA1 varchar(255))", APID_SYMBOL_MAPPING_TABLE);
122
                                        cmd.ExecuteNonQuery();
123
                                    }
124
                                    if (dt.Select(string.Format("NAME = '{0}'", APID_OPC_MAPPING_TABLE)).Length == 0)
125
                                    {
126
                                        cmd.CommandText = string.Format("CREATE TABLE {0} (UID varchar(255) PRIMARY KEY, IN_SYMBOL varchar(MAX), OUT_SYMBOL varchar(MAX), Type INT)", APID_OPC_MAPPING_TABLE);
127
                                        cmd.ExecuteNonQuery();
128
                                    }
129
                                }
130
                            }
131
                            result = true;
132
                        }
133
                    }
134
                    catch (Exception ex)
135
                    {
136
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
137
                    }
138
                    finally
139
                    {
140
                        if (connection != null)
141
                            connection.Dispose();
142
                    }
143
                }
144
            }
145

    
146
            return result;
147
        }
148
        public static DataTable GetSymbolMappingTable()
149
        {
150
            DataTable dt = new DataTable();
151
            Project_Info projectInfo = Project_Info.GetInstance();
152
            if (projectInfo.DBType == ID2DB_Type.SQLite)
153
            {
154
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
155
                {
156
                    try
157
                    {
158
                        connection.Open();
159
                        using (SQLiteCommand cmd = connection.CreateCommand())
160
                        {
161
                            cmd.CommandText = string.Format(@"
162
                            SELECT s.UID, s.Name, st.Type, sp.APID_SYMBOL, sp.DATA1 FROM {1} as st, {0} as s 
163
                                LEFT OUTER JOIN {2} as sp 
164
                                    ON s.UID = SP.UID 
165
                            WHERE s.SymbolType_UID = st.UID 
166
                            AND st.Type != 'Instrument OPC''s' AND st.Type != 'Piping OPC''s' 
167
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_SYMBOL_MAPPING_TABLE);
168
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
169
                                dt.Load(dr);
170

    
171
                            DataTable dtClone = dt.Clone();
172
                            dtClone.Columns["UID"].DataType = typeof(string);
173
                            foreach (DataRow row in dt.Rows)
174
                            {
175
                                dtClone.ImportRow(row);
176
                            }
177
                            dt.Dispose();
178
                            dt = dtClone;
179
                        }
180
                        connection.Close();
181
                    }
182
                    catch (Exception ex)
183
                    {
184
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
185
                    }
186
                    finally
187
                    {
188
                        connection.Dispose();
189
                    }
190
                }
191
            }
192
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
193
            {
194
                using (SqlConnection connection = GetSqlConnection())
195
                {
196
                    try
197
                    {
198
                        if (connection != null && connection.State == ConnectionState.Open)
199
                        {
200
                            using (SqlCommand cmd = connection.CreateCommand())
201
                            {
202
                                cmd.CommandText = string.Format(@"
203
                            SELECT CONVERT(VARCHAR(255), s.UID) AS UID, s.Name, st.Type, sp.APID_SYMBOL, sp.DATA1 FROM {1} as st, {0} as s 
204
                                LEFT OUTER JOIN {2} as sp 
205
                                    ON CONVERT(VARCHAR(255), s.UID) = CONVERT(VARCHAR(255), SP.UID)
206
                            WHERE s.SymbolType_UID = st.UID 
207
                            AND st.Type != 'Instrument OPC''s' AND st.Type != 'Piping OPC''s' 
208
                            ORDER BY st.TYPE ASC;", Symbol_TABLE, SymbolType_TABLE, APID_SYMBOL_MAPPING_TABLE);
209
                                using (SqlDataReader dr = cmd.ExecuteReader())
210
                                    dt.Load(dr);
211
                            }
212
                            connection.Close();
213
                        }
214
                    }
215
                    catch (Exception ex)
216
                    {
217
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
218
                    }
219
                    finally
220
                    {
221
                        if (connection != null)
222
                            connection.Dispose();
223
                    }
224
                }
225
            }
226

    
227
            return dt;
228
        }
229
        public static DataTable GetLineMappingTable()
230
        {
231
            DataTable dt = new DataTable();
232
            Project_Info projectInfo = Project_Info.GetInstance();
233
            if (projectInfo.DBType == ID2DB_Type.SQLite)
234
            {
235
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
236
                {
237
                    try
238
                    {
239
                        connection.Open();
240
                        using (SQLiteCommand cmd = connection.CreateCommand())
241
                        {
242
                            cmd.CommandText = string.Format(@"
243
                            SELECT l.UID, l.Name, sp.APID_SYMBOL, sp.DATA1 FROM {0} as l 
244
                                LEFT OUTER JOIN {1} as sp 
245
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, APID_SYMBOL_MAPPING_TABLE);
246
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
247
                                dt.Load(dr);
248
                        }
249
                        connection.Close();
250
                    }
251
                    catch (Exception ex)
252
                    {
253
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
254
                    }
255
                    finally
256
                    {
257
                        connection.Dispose();
258
                    }
259
                }
260
            }
261
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
262
            {
263
                using (SqlConnection connection = GetSqlConnection())
264
                {
265
                    try
266
                    {
267
                        if (connection != null && connection.State == ConnectionState.Open)
268
                        {
269
                            using (SqlCommand cmd = connection.CreateCommand())
270
                            {
271
                                cmd.CommandText = string.Format(@"
272
                            SELECT l.UID, l.Name, sp.APID_SYMBOL, sp.DATA1 FROM {0} as l 
273
                                LEFT OUTER JOIN {1} as sp 
274
                                    ON l.UID = SP.UID ;", LineTypes_TABLE, APID_SYMBOL_MAPPING_TABLE);
275
                                using (SqlDataReader dr = cmd.ExecuteReader())
276
                                    dt.Load(dr);
277
                            }
278
                            connection.Close();
279
                        }
280
                    }
281
                    catch (Exception ex)
282
                    {
283
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
284
                    }
285
                    finally
286
                    {
287
                        if (connection != null)
288
                            connection.Dispose();
289
                    }
290
                }
291
            }
292

    
293
            return dt;
294
        }
295
        public static DataTable SelectID2SymbolTable()
296
        {
297
            DataTable dt = new DataTable();
298
            Project_Info projectInfo = Project_Info.GetInstance();
299
            if (projectInfo.DBType == ID2DB_Type.SQLite)
300
            {
301
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
302
                {
303
                    try
304
                    {
305
                        connection.Open();
306
                        using (SQLiteCommand cmd = connection.CreateCommand())
307
                        {
308
                            cmd.CommandText = @"SELECT * FROM Symbol";
309
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
310
                                dt.Load(dr);
311
                        }
312
                        connection.Close();
313
                    }
314
                    catch (Exception ex)
315
                    {
316
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
317
                    }
318
                    finally
319
                    {
320
                        connection.Dispose();
321
                    }
322
                }
323
            }
324
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
325
            {
326
                using (SqlConnection connection = GetSqlConnection())
327
                {
328
                    try
329
                    {
330
                        if (connection != null && connection.State == ConnectionState.Open)
331
                        {
332
                            using (SqlCommand cmd = connection.CreateCommand())
333
                            {
334
                                cmd.CommandText = @"SELECT * FROM Symbol";
335
                                using (SqlDataReader dr = cmd.ExecuteReader())
336
                                    dt.Load(dr);
337
                            }
338
                            connection.Close();
339
                        }
340
                    }
341
                    catch (Exception ex)
342
                    {
343
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
344
                    }
345
                    finally
346
                    {
347
                        if (connection != null)
348
                            connection.Dispose();
349
                    }
350
                }
351
            }
352

    
353
            return dt;
354
        }
355
        public static DataTable SelectSymbolType()
356
        {
357
            DataTable dt = new DataTable();
358
            Project_Info projectInfo = Project_Info.GetInstance();
359
            if (projectInfo.DBType == ID2DB_Type.SQLite)
360
            {
361
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
362
                {
363
                    try
364
                    {
365
                        connection.Open();
366
                        using (SQLiteCommand cmd = connection.CreateCommand())
367
                        {
368
                            cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
369
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
370
                                dt.Load(dr);
371
                        }
372
                        connection.Close();
373
                    }
374
                    catch (Exception ex)
375
                    {
376
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
377
                    }
378
                    finally
379
                    {
380
                        connection.Dispose();
381
                    }
382
                }
383
            }
384
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
385
            {
386
                using (SqlConnection connection = GetSqlConnection())
387
                {
388
                    try
389
                    {
390
                        if (connection != null && connection.State == ConnectionState.Open)
391
                        {
392
                            using (SqlCommand cmd = connection.CreateCommand())
393
                            {
394
                                cmd.CommandText = string.Format("SELECT * FROM {0}", SymbolType_TABLE);
395
                                using (SqlDataReader dr = cmd.ExecuteReader())
396
                                    dt.Load(dr);
397
                            }
398
                            connection.Close();
399
                        }
400
                    }
401
                    catch (Exception ex)
402
                    {
403
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
404
                    }
405
                    finally
406
                    {
407
                        if (connection != null)
408
                            connection.Dispose();
409
                    }
410
                }
411
            }
412

    
413
            return dt;
414
        }
415
        public static DataTable SelectDrawings()
416
        {
417
            DataTable dt = new DataTable();
418
            Project_Info projectInfo = Project_Info.GetInstance();
419
            if (projectInfo.DBType == ID2DB_Type.SQLite)
420
            {
421
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
422
                {
423
                    try
424
                    {
425
                        connection.Open();
426
                        using (SQLiteCommand cmd = connection.CreateCommand())
427
                        {
428
                            cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
429
                            using (SQLiteDataReader dr = cmd.ExecuteReader())
430
                                dt.Load(dr);
431
                        }
432
                        connection.Close();
433
                    }
434
                    catch (Exception ex)
435
                    {
436
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
437
                    }
438
                    finally
439
                    {
440
                        connection.Dispose();
441
                    }
442
                }
443
            }
444
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
445
            {
446
                using (SqlConnection connection = GetSqlConnection())
447
                {
448
                    try
449
                    {
450
                        if (connection != null && connection.State == ConnectionState.Open)
451
                        {
452
                            using (SqlCommand cmd = connection.CreateCommand())
453
                            {
454
                                cmd.CommandText = string.Format("SELECT * FROM {0}", "Drawings");
455
                                using (SqlDataReader dr = cmd.ExecuteReader())
456
                                    dt.Load(dr);
457
                            }
458
                            connection.Close();
459
                        }
460
                    }
461
                    catch (Exception ex)
462
                    {
463
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
464
                    }
465
                    finally
466
                    {
467
                        if (connection != null)
468
                            connection.Dispose();
469
                    }
470
                }
471
            }
472

    
473
            return dt;
474
        }
475
        public static bool InsertSymbolMapping(List<Tuple<string, string, string, string>> datas)
476
        {
477
            Project_Info projectInfo = Project_Info.GetInstance();
478
            if (projectInfo.DBType == ID2DB_Type.SQLite)
479
            {
480
                using (SQLiteConnection connection = new SQLiteConnection(string.Format(CultureInfo.CurrentCulture, "Data Source = {0}", projectInfo.DBFilePath)))
481
                {
482
                    try
483
                    {
484
                        connection.Open();
485
                        using (SQLiteTransaction transaction = connection.BeginTransaction())
486
                        {
487
                            try
488
                            {
489
                                using (SQLiteCommand cmd = connection.CreateCommand())
490
                                {
491
                                    foreach (var item in datas)
492
                                    {
493
                                        cmd.Parameters.Clear();
494
                                        cmd.CommandText = string.Format("INSERT OR REPLACE INTO {0} (UID, NAME, APID_SYMBOL, DATA1) VALUES (@UID, @NAME, @APID_SYMBOL, @DATA1)", APID_SYMBOL_MAPPING_TABLE);
495
                                        cmd.Parameters.AddWithValue("@UID", item.Item1);
496
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
497
                                        cmd.Parameters.AddWithValue("@APID_SYMBOL", item.Item3);
498
                                        cmd.Parameters.AddWithValue("@DATA1", item.Item4);
499
                                        cmd.ExecuteNonQuery();
500
                                    }
501
                                }
502
                                transaction.Commit();
503
                                connection.Close();
504
                            }
505
                            catch (Exception ex)
506
                            {
507
                                Log.Write(ex.Message + "\r\n" + ex.StackTrace);
508
                                transaction.Rollback();
509
                                return false;
510
                            }
511
                            finally
512
                            {
513
                                transaction.Dispose();
514
                            }
515
                        }
516
                    }
517
                    catch (Exception ex)
518
                    {
519
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
520
                        return false;
521
                    }
522
                    finally
523
                    {
524
                        connection.Dispose();
525
                    }
526
                }
527
            }
528
            else if (projectInfo.DBType == ID2DB_Type.MSSQL)
529
            {
530
                using (SqlConnection connection = GetSqlConnection())
531
                {
532
                    try
533
                    {
534
                        if (connection != null && connection.State == ConnectionState.Open)
535
                        {
536
                            using (SqlCommand cmd = connection.CreateCommand())
537
                            {
538
                                foreach (var item in datas)
539
                                {
540
                                    cmd.Parameters.Clear();
541
                                    cmd.CommandText = string.Format(@"
542
                                    IF EXISTS (SELECT * FROM {0} WHERE UID = '{1}')
543
                                        UPDATE {0} SET NAME = @NAME, APID_SYMBOL = @APID_SYMBOL, DATA1 = @DATA1 WHERE UID = @UID
544
                                    ELSE
545
                                        INSERT INTO {0} (UID, NAME, APID_SYMBOL, DATA1) VALUES (@UID, @NAME, @APID_SYMBOL, @DATA1)", APID_SYMBOL_MAPPING_TABLE, item.Item1);
546
                                    cmd.Parameters.AddWithValue("@UID", item.Item1);
547
                                    if (string.IsNullOrEmpty(item.Item2))
548
                                        cmd.Parameters.AddWithValue("@NAME", DBNull.Value);
549
                                    else
550
                                        cmd.Parameters.AddWithValue("@NAME", item.Item2);
551
                                    if (string.IsNullOrEmpty(item.Item3))
552
                                        cmd.Parameters.AddWithValue("@APID_SYMBOL", DBNull.Value);
553
                                    else
554
                                        cmd.Parameters.AddWithValue("@APID_SYMBOL", item.Item3);
555
                                    if (string.IsNullOrEmpty(item.Item4))
556
                                        cmd.Parameters.AddWithValue("@DATA1", DBNull.Value);
557
                                    else
558
                                        cmd.Parameters.AddWithValue("@DATA1", item.Item4);
559

    
560
                                    cmd.ExecuteNonQuery();
561
                                }
562
                            }
563
                            connection.Close();
564
                        }
565
                    }
566
                    catch (Exception ex)
567
                    {
568
                        Log.Write(ex.Message + "\r\n" + ex.StackTrace);
569
                        return false;
570
                    }
571
                    finally
572
                    {
573
                        if (connection != null)
574
                            connection.Dispose();
575
                    }
576
                }
577
            }
578

    
579
            return true;
580
        }
581
        #endregion
582

    
583
        #region AVEVA
584
        public static string GetAvevaConnectionString()
585
        {
586
            string strConn = string.Empty;
587
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
588
            {
589
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLDatabaseName);
590
            }
591
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
592
            {
593
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
594
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLDatabaseName + ";" + strAccessString);
595
            }
596

    
597
            return strConn;
598
        }
599
        public static string GetAvevaConnectionString_Reports()
600
        {
601
            string strConn = string.Empty;
602
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
603
            {
604
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLReportsDatabaseName);
605
            }
606
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
607
            {
608
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
609
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLReportsDatabaseName + ";" + strAccessString);
610
            }
611

    
612
            return strConn;
613
        }
614
        public static string GetAvevaConnectionString_Admin()
615
        {
616
            string strConn = string.Empty;
617
            if (Utilities.strSQLWinAuthentication.ToUpper() == "YES")
618
            {
619
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Integrated Security=SSPI;Initial Catalog={0};", Utilities.strSQLAdminDatabaseName);
620
            }
621
            else if (Utilities.strSQLWinAuthentication.ToUpper() == "NO")
622
            {
623
                string strAccessString = "User ID=" + Utilities.strSQLUserName + ";Password=" + Utilities.strSQLPassword;
624
                strConn = String.Format("Data Source=" + Utilities.strSQLServerName + ";Initial Catalog=" + Utilities.strSQLAdminDatabaseName + ";" + strAccessString);
625
            }
626

    
627
            return strConn;
628
        }
629
        public static string GetDirectiveValue(string name)
630
        {
631
            string result = null;
632

    
633
            using (SqlConnection connection = new SqlConnection())
634
            {
635
                connection.ConnectionString = GetAvevaConnectionString_Admin();
636
                connection.Open();
637
                using (SqlCommand cmd = connection.CreateCommand())
638
                {
639
                    cmd.CommandText = string.Format("select DirectiveValue from ProjectDirectives where DirectiveName = '{0}'", name);
640
                    using (SqlDataReader dr = cmd.ExecuteReader())
641
                    {
642
                        if (dr.Read())
643
                            result = dr.GetString(0);
644
                    }
645
                }
646
                connection.Close();
647
            }
648

    
649
            return result;
650
        }
651
        public static DataTable GetDrawingTemplate()
652
        {
653
            DataTable dt = new DataTable();
654

    
655
            using (SqlConnection connection = new SqlConnection())
656
            {
657
                connection.ConnectionString = GetAvevaConnectionString_Reports();
658
                connection.Open();
659
                using (SqlCommand cmd = connection.CreateCommand())
660
                {
661
                    cmd.CommandText = "select ID, Name,IsDWG,DateCreation,MachineName,FutureUse1,FutureUse2 from DrawingTemplates where IsDWG=0";
662
                    using (SqlDataReader dr = cmd.ExecuteReader())
663
                        dt.Load(dr);
664
                }
665
                connection.Close();
666
            }
667

    
668
            return dt;
669
        }
670
        public static DataTable SelectStandardSymbolTable()
671
        {
672
            DataTable dt = new DataTable();
673

    
674
            using (SqlConnection connection = new SqlConnection())
675
            {
676
                connection.ConnectionString = GetAvevaConnectionString_Admin();
677
                connection.Open();
678
                using (SqlCommand cmd = connection.CreateCommand())
679
                {
680
                    cmd.CommandText = string.Format(@"
681
                    SELECT ss.ID, ss.Symbol_Name, sps.Relative_Path FROM {0} as ss
682
                    LEFT OUTER JOIN {1} as sps
683
                    ON ss.Relative_Path = sps.ID
684
                    ORDER BY sps.ID ASC
685
                    ;", "stdSymbols", "StdPrjFolStructure");
686

    
687
                    using (SqlDataReader dr = cmd.ExecuteReader())
688
                        dt.Load(dr);
689
                }
690
                connection.Close();
691
            }
692

    
693
            return dt;
694
        }
695
        public static DataTable SelectStandardSymbolStructureTable()
696
        {
697
            DataTable dt = new DataTable();
698

    
699
            using (SqlConnection connection = new SqlConnection())
700
            {
701
                connection.ConnectionString = GetAvevaConnectionString_Admin();
702
                connection.Open();
703
                using (SqlCommand cmd = connection.CreateCommand())
704
                {
705
                    cmd.CommandText = string.Format(@"
706
                    SELECT * FROM {0}
707
                    ;", "StdPrjFolStructure");
708

    
709
                    using (SqlDataReader dr = cmd.ExecuteReader())
710
                        dt.Load(dr);
711
                }
712
                connection.Close();
713
            }
714

    
715
            return dt;
716
        }
717
        public static DataTable SelectStandardSymbolImageTable()
718
        {
719
            DataTable dt = new DataTable();
720

    
721
            using (SqlConnection connection = new SqlConnection())
722
            {
723
                connection.ConnectionString = GetAvevaConnectionString_Admin();
724
                connection.Open();
725
                using (SqlCommand cmd = connection.CreateCommand())
726
                {
727
                    cmd.CommandText = string.Format(@"
728
                    SELECT ss.Symbol_Name, ss.Pic_Binary, sps.Relative_Path FROM {0} as ss
729
                    LEFT OUTER JOIN {1} as sps
730
                    ON ss.Relative_Path = sps.ID
731
                    ORDER BY sps.ID ASC
732
                    ;", "stdSymbols", "StdPrjFolStructure");
733

    
734
                    using (SqlDataReader dr = cmd.ExecuteReader())
735
                        dt.Load(dr);
736
                }
737
                connection.Close();
738
            }
739

    
740
            return dt;
741
        }
742
        public static DataTable SelectDrawingTable()
743
        {
744
            DataTable dt = new DataTable();
745

    
746
            using (SqlConnection connection = new SqlConnection())
747
            {
748
                connection.ConnectionString = GetAvevaConnectionString_Reports();
749
                connection.Open();
750
                using (SqlCommand cmd = connection.CreateCommand())
751
                {
752
                    cmd.CommandText = string.Format(@"
753
                    SELECT DrawingId, XDNLABEL, XDSLABEL FROM {0}
754
                    ;", "SynchroniseDetails");
755

    
756
                    using (SqlDataReader dr = cmd.ExecuteReader())
757
                        dt.Load(dr);
758
                }
759
                connection.Close();
760
            }
761

    
762
            return dt;
763
        }
764
        public static DataTable SelectSymbolTable()
765
        {
766
            DataTable dt = new DataTable();
767

    
768
            using (SqlConnection connection = new SqlConnection())
769
            {
770
                connection.ConnectionString = GetAvevaConnectionString_Admin();
771
                connection.Open();
772
                using (SqlCommand cmd = connection.CreateCommand())
773
                {
774
                    cmd.CommandText = string.Format(@"
775
                    SELECT * FROM {0}
776
                    ;", "SYMBOLS");
777

    
778
                    using (SqlDataReader dr = cmd.ExecuteReader())
779
                        dt.Load(dr);
780
                }
781
                connection.Close();
782
            }
783

    
784
            return dt;
785
        }
786
        #endregion
787

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