Recently, I learned about Delphi and needed to configure the connection to the Oracle database. However, many methods on the Internet do not contain IP addresses. I finally found it for a long time, but I forgot where I found it. Share it with you!
Call this function when you need to introduce inifiles to create a form.
Add inifiles to the user
Procedure tform1.formcreate (Sender: tobject );
Const
Connstr = 'provider = oraoledb. oracle.1; Password = % s; persist Security info = true; user id = % s; Data Source = (description = (address_list = (address = (Protocol = TCP) (host = % s) (Port = % s) (CONNECT_DATA = (SERVICE_NAME = % s )))';
VaR
INIFILE: Tinifile;
IP, port, Sid, username, password, filename, filepath: string;
Begin
Filepath: = extractfilepath (paramstr (0 ));
Filename: = filepath + 'dbconfig. ini ';
If fileexists (filename) then
Begin
INIFILE: = Tinifile. Create (filename );
IP: = INIFILE. readstring ('database', 'IP', '2017. 0.0.1 ');
Port: = INIFILE. readstring ('database', 'Port', '123 ');
Sid: = INIFILE. readstring ('database', 'sid ', 'orcl ');
Username: = INIFILE. readstring ('database', 'username', 'admin ');
Password: = INIFILE. readstring ('database', 'Password', 'admin ');
Form1.adoconnection1. connectionstring: = format (connstr, [password, username, IP, port, Sid]);
End
Else
Showmessage ('The dbconfig. ini file is not found. Check the file! ');
End;
INI File Content
[Database]
IP = 127.0.0.1
Port = 1521
SID = orcl
Password = Admin
Username = Admin
// Note: extractfilepath (paramstr (0) + 'dbconfig. ini '); it seems that I cannot write it here. The result is not followed by the file name.