Working with password-protected Paradox and dBASE tables
A session component can store passwords for password-protected Paradox and dBASE tables. Once you add a password to the session, your application can open tables protected by that password. Once you remove the password from the session, your application can抰 open tables that use the password until you add it again.
Using the AddPassword method
The AddPassword method provides an optional way for an application to provide a password for a session prior to opening an encrypted Paradox or dBASE table that requires a password for access. If you do not add the password to the session, when your application attempts to open a password-protected table, a dialog box prompts the user for a password.
AddPassword takes one parameter, a string containing the password to use. You can call AddPassword as many times as necessary to add passwords (one at a time) to access tables protected with different passwords.
var
Passwrd: String;
begin
Passwrd := InputBox('Enter password', 'Password:', '');
Session.AddPassword(Passwrd);
try
Table1.Open;
except
ShowMessage('Could not open table!');
Application.Terminate;
end;
end;
Note: Use of the InputBox function, above, is for demonstration purposes. In a real-world application, use password entry facilities that mask the password as it is entered, such as the PasswordDialog function or a custom form.
The Add button of the PasswordDialog function dialog has the same effect as the AddPassword method.
if PasswordDialog(Session) then
Table1.Open
else
ShowMessage('No password given, could not open table!');
end;
Using the RemovePassword and RemoveAllPasswords methods
RemovePassword deletes a previously added password from memory. RemovePassword takes one parameter, a string containing the password to delete.
Session.RemovePassword('secret');
RemoveAllPasswords deletes all previously added passwords from memory.
Session.RemoveAllPasswords;
Using the GetPassword method and OnPassword event
The OnPassword event allows you to control how your application supplies passwords for Paradox and dBASE tables when they are required. Provide a handler for the OnPassword event if you want to override the default password handling behavior. If you do not provide a handler, Delphi presents a default dialog for entering a password and no special behavior is provided梩he table open attempt either succeeds or an exception is raised.
If you provide a handler for the OnPassword event, do two things in the event handler: call the AddPassword method and set the event handler抯 Continue parameter to True. The AddPassword method passes a string to the session to be used as a password for the table. The Continue parameter indicates to Delphi that no further password prompting need be done for this table open attempt. The default value for Continue is False, and so requires explicitly setting it to True. If Continue is False after the event handler has finished executing, an OnPassword event fires again梕ven if a valid password has been passed using AddPassword. If Continue is
True after execution of the event handler and the string passed with AddPassword is not the valid password, the table open attempt fails and an exception is raised.
OnPassword can be triggered by two circumstances. The first is an attempt to open a password-protected table (dBASE or Paradox) when a valid password has not already been supplied to the session. (If a valid password for that table has already been supplied, the OnPassword event does not occur.)
The other circumstance is a call to the GetPassword method. GetPassword either generates an OnPassword event, or, if the session does not have an OnPassword event handler, displays a default password dialog. It returns True if the OnPassword event handler or default dialog added a password to the session, and False if no entry at all was made.
In the following example, the Password method is designated as the OnPassword event handler for the default session by assigning it to the global Session object抯 OnPassword property.
procedure TForm1.FormCreate(Sender: TObject);
begin
Session.OnPassword := Password;
end;
In the Password method, the InputBox function prompts the user for a password. The AddPassword method then programmatically supplies the password entered in the dialog to the session.
procedure TForm1.Password(Sender: TObject; var Continue: Boolean);
var
Passwrd: String;
begin
Passwrd := InputBox('Enter password', 'Password:', '');
Continue := (Passwrd > '');
Session.AddPassword(Passwrd);
end;
The OnPassword event (and thus the Password event handler) is triggered by an attempt to open a password-protected table, as demonstrated below. Even though the user is prompted for a password in the handler for the OnPassword event, the table open attempt can still fail if they enter an invalid password or something else goes wrong.
procedure TForm1.OpenTableBtnClick(Sender: TObject);
const
CRLF = #13 + #10;
begin
try
Table1.Open; { this line triggers the OnPassword event }
except
on E:Exception do begin { exception if cannot open table }
ShowMessage('Error!' + CRLF + { display error explaining what happened }
E.Message + CRLF +
'Terminating application...');
Application.Terminate; { end the application }
end;
end;
end;
这是Delphi帮助文件里的,如果小妹的英语还不错的话,这个一定对你有帮助。
据我了解女孩的英文都不错哟!:)