-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDBConnection.cs
69 lines (60 loc) · 2.26 KB
/
DBConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using ABB.Vtrin;
using ABB.Vtrin.Drivers;
namespace CalcEngineTutorialSetup
{
class DBConnection: IDisposable
{
private readonly cDataLoader dataloader = new cDataLoader();
public cDriverSkeleton RTDBDriver;
private readonly string RTDBHost;
private readonly string RTDBUsername;
private readonly SecureString RTDBPassword;
public DBConnection(string _RTDBHost, string _RTDBUsername, SecureString _RTDBPassword)
{
RTDBHost = _RTDBHost;
RTDBUsername = _RTDBUsername;
RTDBPassword = _RTDBPassword;
}
public void ConnectOrThrow()
{
// Set up a memory stream to catch exceptions
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
var listener = new System.Diagnostics.TextWriterTraceListener(memoryStream, "connectlistener");
System.Diagnostics.Trace.Listeners.Add(listener);
// Set connection options
dataloader.ConnectOptions =
ABB.Vtrin.cDataLoader.cConnectOptions.AcceptNewServerKeys
| ABB.Vtrin.cDataLoader.cConnectOptions.AcceptServerKeyChanges;
// Initialize the database driver
RTDBDriver = dataloader.Connect(
RTDBHost,
RTDBUsername,
RTDBPassword,
false);
// Unbind the connect listener
System.Diagnostics.Trace.Listeners.Remove("connectlistener");
// Case: driver is null, something went wrong
// > throw an error
if (RTDBDriver == null)
{
// Read stack trace from the memorystream buffer
string msg = System.Text.Encoding.UTF8.GetString(memoryStream.GetBuffer());
throw new System.ApplicationException(msg);
}
}
}
public void Dispose()
{
if (dataloader != null)
dataloader.Dispose();
}
}
}