Home > Back-end >  When connecting to the database failed, how can any clue, rather than the error.
When connecting to the database failed, how can any clue, rather than the error.

Time:09-16


IniFile:=TIniFile. Create (ExtractFilePath (Paramstr (0)) + 'Connect. Ini');//connected via INI

ServerName:=IniFile ReadString (' Option ', 'DbServer', 'computer');//read computer name

DbName:=IniFile ReadString (' Option ', 'DbName', 'database');//read the database name

The DbSa:=IniFile ReadString (' Option ', 'the DbA,' SA ');//SA

Dbpwd:=IniFile ReadString (' Option ', 'DbPw', "password");//SA password

ADOConnection1. The ConnectionString:='Provider=SQLOLEDB. 1; Password='+ Dbpwd +'; Persist Security Info=True; User ID='+ DbSa +'; Initial Catalog='+ DbName +'; Data Source='+ +' ServerName ';

I am by running directory in the software, INI, records the information database, the connect in this way, but after connection, if I change the database name or SA password, will be an error, how to use the TRY to put the database if the connection is not on a predetermined hint,

INI the following
Connect the ini

[Option]
DbServer=127.0.0.1//host
Database DbName=Da//
The DbA=sa
DbPw=123//password

CodePudding user response:

 
Try
//code might be wrong
Except,
On E: the Exception do
The begin
Application. MessageBox (PChar (' custom content '), 'the title bar, 16);//custom error content
//Application. MessageBox (PChar (E.M essage), 'the title bar, 16);//the actual error content
end;
end;

CodePudding user response:

This code, you should have two place need to use the try

1, in most cases, a class created, as far as possible, using a try... Finally, make sure that whether operation error, both for the release of the class;
2, and some may run as the parameter or other factors, there may be some abnormal situation, using a try... Except, and after the except to deal with the possible exception,

 
//the INI file here, usually need to use to try
IniFile:=TIniFile. Create (ExtractFilePath (Paramstr (0)) + 'Connect. Ini');//connected via INI
Try
ServerName:=IniFile ReadString (' Option ', 'DbServer', 'computer');//read computer name
DbName:=IniFile ReadString (' Option ', 'DbName', 'database');//read the database name
The DbSa:=IniFile ReadString (' Option ', 'the DbA,' SA ');//SA
Dbpwd:=IniFile ReadString (' Option ', 'DbPw', "password");//SA password
The finally
FreeAndNil (IniFile);//here, no matter whether there is wrong, in front of the to be released
end;


ADOConnection1. The ConnectionString:='Provider=SQLOLEDB. 1; Password='+ Dbpwd +'; Persist Security Info=True; User ID='+ DbSa +'; Initial Catalog='+ DbName +'; Data Source='+ +' ServerName ';

Try
ADOConnection1. Open;
Except on E: the Exception do
The begin
//processing error message here
end;
end;

  • Related