-
Notifications
You must be signed in to change notification settings - Fork 1
/
uConnection.pas
117 lines (104 loc) · 2.61 KB
/
uConnection.pas
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
unit uConnection;
interface
uses
SysUtils, ADODB, Classes, Dialogs, Windows;
const
connstrORA =
'Provider=MSDAORA.1;Password=%s;User ID=%s;Data Source=%s;Persist Security Info=True';
connstrMS =
//'Provider=SQLOLEDB.1;Password=%s;User ID=%s;Data Source=%s;Initial Catalog=IFRS;';
//'Provider=SQLNCLI10;Password=%s;User ID=%s;Data Source=%s;Initial Catalog=IFRS;';
'Provider=MSDASQL.1;Password=%s;User ID=%s;Data Source=%s;Persist Security Info=False';
type
TConnection = class
strict private
FConn: TADOConnection;
FConnected: boolean;
FLastError: string;
FOnChangeStatus: TNotifyEvent;
procedure SetConnected(const aValue: boolean);
procedure SetChangeStatus(aValue: TNotifyEvent);
public
Query: TADOQuery;
constructor Create(const aServer, aUser, aPass: string;
const aConnStr: string = connstrMS);
destructor Destroy; override;
procedure Connect;
procedure Disconnect;
property Connected: boolean read FConnected write SetConnected;
property LastError: string read FLastError;
property OnChangeStatus: TNotifyEvent read FOnChangeStatus write SetChangeStatus;
end;
implementation
{ TURConnection }
procedure TConnection.Connect;
begin
try
FConn.Connected := true;
FConnected := FConn.Connected;
if Assigned(FOnChangeStatus) then
OnChangeStatus(Self);
except
on E: Exception do
begin
FLastError := E.Message;
end;
end;
end;
constructor TConnection.Create(const aServer, aUser, aPass, aConnStr: string);
begin
inherited Create;
try
FConn := TADOConnection.Create(nil);
FConn.ConnectionString := Format(aConnStr, [aPass, aUser, aServer]);
FConn.LoginPrompt := false;
Query := TADOQuery.Create(nil);
Query.Connection := FConn;
except
on E: Exception do
begin
FLastError := E.Message;
end;
end;
end;
destructor TConnection.Destroy;
begin
Query.Close;
FConn.Close;
Query.Free;
FConn.Free;
inherited;
end;
procedure TConnection.Disconnect;
begin
try
FConn.Connected := false;
FConnected := FConn.Connected;
FOnChangeStatus(Self);
except
on E: Exception do
begin
FLastError := E.Message;
end;
end;
end;
procedure TConnection.SetChangeStatus(aValue: TNotifyEvent);
begin
FOnChangeStatus := aValue;
end;
procedure TConnection.SetConnected(const aValue: boolean);
begin
try
FConn.Connected := aValue;
FConnected := FConn.Connected;
FOnChangeStatus(Self);
except
on E: Exception do
begin
FConn.Connected := false;
FConnected := FConn.Connected;
FLastError:= E.Message;
end;
end;
end;
end.