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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
unit dbform;
{$mode objfpc}{$H+}
interface
uses
SysUtils, db, sqldb, Forms, Controls, Dialogs, DBGrids, dbloginform,
mssqlconn; // mssqlconn was added to 2.6.1, you need a recent 2.6.1
type
{ TForm1 }
TForm1 = class(TForm)
Datasource1: TDatasource;
DBGrid1: TDBGrid;
MSSQLConnection1: TMSSQLConnection;
SQLQuery1: TSQLQuery;
SQLTransaction1: TSQLTransaction;
SybaseConnection1: TSybaseConnection;
procedure FormCreate(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.FormCreate(Sender: TObject);
type
DBType=(MSSQL,SybaseASE);
var
ChosenDB: DBType;
Connection: TSQLConnection;
DBSelected: string;
GoodConnection: boolean;
LoginForm: dbloginform.TLoginForm;
Password: string;
UserCancel: boolean;
begin
// Let user login.
GoodConnection:=false;
UserCancel:=false;
LoginForm:=dbloginform.TLoginForm.Create(Nil);
try
while (GoodConnection=false) and (UserCancel=false) do
begin
if LoginForm.ShowModal=mrOK then
begin
DBSelected:=LoginForm.DatabaseType.Items[LoginForm.DatabaseType.ItemIndex];
// Use the text in the databasetype combobox to see what db the user wants.
// Then we point our Connection to the relevant TSQLConnection descendant.
case UpperCase(DBSelected) of
'MS SQL SERVER':
begin
ChosenDB:=MSSQL;
Connection:=MSSQLConnection1;
end;
'SYBASE ASE':
begin
ChosenDB:=SybaseASE;
Connection:=SybaseConnection1;
end
else
begin
showmessage('Unknown database type '+DBSelected+' chosen. Aborting. Pleae fix the code!');
Application.Terminate;
end;
end;
if LoginForm.OSAuthentication.Checked then
begin
// Use operating system credentials - mssqlconn
// expectes empty username/password then.
Connection.UserName:='';
Connection.Password:='';
end
else
begin
// Use regular username/password
Connection.UserName:=LoginForm.User.Text;
Connection.Password:=LoginForm.Password.Text;
end;
if LoginForm.Port.Text<>'' then
begin
Connection.HostName:=LoginForm.Server.Text+':'+LoginForm.Port.Text;
end
else
begin
// Default/no port. Let the connector sort it out.
Connection.HostName:=LoginForm.Server.Text;
end;
Connection.DatabaseName:=LoginForm.Database.Text;
// Actually, this should work both on MS SQL and Sybase server, so no need to change it:
//SQLQuery1.SQL.Text:='select * from sysservers';
// Everything set up, now connect to database.
// First make sure the other connection is switched off:
if ChosenDB=MSSQL then
begin
SybaseConnection1.Connected:=false;
end
else
begin
MSSQLConnection1.Connected:=false;
end;
SQLTransaction1.DataBase:=Connection;
SQLQuery1.DataBase:=Connection;
try
Connection.Connected:=true;
GoodConnection:=true;
except
on E: Exception do
begin
GoodConnection:=false;
showmessage('Error connecting to database. Technical details: '+E.ClassName+'/'+E.Message);
end;
end;
end
else
begin
showmessage('User cancelled login. Stopping.');
UserCancel:=true; //Tell the loop to release us.
Application.Terminate;
end;
end;
if UserCancel=false then
begin
// Now activate the components "downstream" of the database connection to get the data
// displayed to the user
try
SQLTransaction1.Active:=true;
SQLQuery1.Active:=true;
GoodConnection:=true;
except
on E: Exception do
begin
GoodConnection:=false;
showmessage('Error connecting to database. Technical details: '+E.ClassName+'/'+E.Message);
end;
end;
end;
finally
// Close the form and release memory
LoginForm.Release;
end;
end;
end.
|