9
mssqlconn, // mssqlconn was added to 2.6.1, you need a recent 2.6.1
10
db, sqldb, FileUtil, Forms, Controls, Graphics,
11
Dialogs, DBGrids, dbloginform;
18
Datasource1: TDatasource;
20
MSSQLConnection1: TMSSQLConnection;
22
SQLTransaction1: TSQLTransaction;
23
SybaseConnection1: TSybaseConnection;
24
procedure FormCreate(Sender: TObject);
26
{ private declarations }
28
{ public declarations }
41
procedure TForm1.FormCreate(Sender: TObject);
43
DBType=(MSSQL,SybaseASE);
46
Connection: TSQLConnection;
48
GoodConnection: boolean;
49
LoginForm: dbloginform.TLoginForm;
54
GoodConnection:=false;
56
LoginForm:=dbloginform.TLoginForm.Create(Nil);
58
while (GoodConnection=false) and (UserCancel=false) do
60
if LoginForm.ShowModal=mrOK then
62
DBSelected:=LoginForm.DatabaseType.Items[LoginForm.DatabaseType.ItemIndex];
63
// Use the text in the databasetype combobox to see what db the user wants.
64
// Then we point our Connection to the relevant TSQLConnection descendant.
65
case UpperCase(DBSelected) of
69
Connection:=MSSQLConnection1;
74
Connection:=SybaseConnection1;
78
showmessage('Unknown database type '+DBSelected+' chosen. Aborting. Pleae fix the code!');
79
Application.Terminate;
82
if LoginForm.OSAuthentication.Checked then
84
// Use operating system credentials - mssqlconn
85
// expectes empty username/password then.
86
Connection.UserName:='';
87
Connection.Password:='';
91
// Use regular username/password
92
Connection.UserName:=LoginForm.User.Text;
93
Connection.Password:=LoginForm.Password.Text;
95
if LoginForm.Port.Text<>'' then
97
Connection.HostName:=LoginForm.Server.Text+':'+LoginForm.Port.Text;
101
// Default/no port. Let the connector sort it out.
102
Connection.HostName:=LoginForm.Server.Text;
104
Connection.DatabaseName:=LoginForm.Database.Text;
106
// Actually, this should work both on MS SQL and Sybase server, so no need to change it:
107
//SQLQuery1.SQL.Text:='select * from sysservers';
108
// Everything set up, now connect to database.
109
// First make sure the other connection is switched off:
110
if ChosenDB=MSSQL then
112
SybaseConnection1.Connected:=false;
116
MSSQLConnection1.Connected:=false;
118
SQLTransaction1.DataBase:=Connection;
119
SQLQuery1.DataBase:=Connection;
121
Connection.Connected:=true;
122
GoodConnection:=true;
126
GoodConnection:=false;
127
showmessage('Error connecting to database. Technical details: '+E.ClassName+'/'+E.Message);
133
showmessage('User cancelled login. Stopping.');
134
UserCancel:=true; //Tell the loop to release us.
135
Application.Terminate;
138
if UserCancel=false then
140
// Now activate the components "downstream" of the database connection to get the data
141
// displayed to the user
143
SQLTransaction1.Active:=true;
144
SQLQuery1.Active:=true;
145
GoodConnection:=true;
149
GoodConnection:=false;
150
showmessage('Error connecting to database. Technical details: '+E.ClassName+'/'+E.Message);
155
// Close the form and release memory