How does delphi connect to SQL by configuring the INI file?

Source: Internet
Author: User
How does delphi connect to SQL Delphi/Windows SDK/API by configuring the INI file?
Http://www.delphi2007.net/DelphiDB/html/delphi_20061221172852177.html
How does delphi configure the INI file to connect to SQL?
There are no examples or Code ?
I hope you can help me with this operation for the first time.

First, there is a configured INI file, and then read the configuration information of the INI file before connecting to the database to connect to SQL.

First, you need to process the INI. There is a class Tinifile In the INI File Read/Write delphi. With this type, you can easily create and read and write the INI file.

/// Ini
[Serverinfo]
Serverip =.
Sqldbname = AAAA
Sqluserid = sa
Sqlpwd = sa


/////////////
VaR
Filename: string;
Begin

Filename: = extractfilepath (application. exename) + 'Resource \ conf. ini ';
With Tinifile. Create (filename) Do
Begin
Serverip: = readstring ('serverinfo', 'serverip ','');
Sqldbname: = readstring ('serverinfo', 'sqldbname ','');
Sqlusername: = readstring ('serverinfo', 'sqluserid ','');
Sqlpwd: = readstring ('serverinfo', 'sqlpwd ','');
Destroy;
End;




///////////

Adoconnection1.connectionstring: = 'provider = sqloledb.1; Password = '+
Sqlpwd +
'; Persist Security info = true;' +
'User id = '+
Sqlusername +
'; Initial catalog =' +
Sqldbname +
'; Data Source =' +
Serverip;

The password on the third floor is almost the same. However, it is best to encrypt the password! In this case, you can find anything you like.

Thank you for your help!

Solution on the third floor

The password must be encrypted.

I found some examples of password encryption, and I thought it was a bit fuzzy,
Could you give me some detailed examples?
After all, I still don't quite understand the first such example.

On the third floor, how is the configuration of adoconnection1.connectionstring? during compilation, I always wrote an error here. It is not defined.

The configuration problem is fixed. Thank you!

However, I hope you can give me some guidance on encryption.
I think like this: first define two strings key and STR as the key and key. STR is the corresponding string from the key.
How do I write the decryption code for the corresponding encryption? I have been thinking for so long, but I still haven't figured it out.
How can I apply the encrypted data to the Code on the third floor? I hope you can give me some advice.

[039d2d4968b58091]
6294e987f260513a = 93f7bb025cd2ece209fb77c6868074e3
16cc347bb7a98df = 198e4bb5584f94bf
E7ebaccb860daafe = 198e4bb5584f94bf
13ae07a60ef35373 = 0ab3345601a7728a

This is my configuration

[039d2d4968b58091]
6294e987f260513a = 93f7bb025cd2ece209fb77c6868074e3 // server
16cc347bb7a98df = 198e4bb5584f94bf // Database Name
E7ebaccb860daafe = 198e4bb5584f94bf // user
13ae07a60ef35373 = 0ab3345601a7728a // Password

Can I give a specific example orAlgorithmGive it to me

Encryption can be implemented with one shift to the other. Do you have to worry about encryption algorithms ?????

I used it for reference. I don't know how to proceed.


// Db_con.ini this is your configuration file
[Serverinfo]
Serverip =.
Sqldbname = AAAA
Sqluserid = sa
Sqlpwd = sa




// This is the database connection function in your database connection unit file.
Function get_db_con_str (filename: string): string;
VaR
Serverip, sqldbname, sqlusername, sqlpwd: string;
Begin
Result: = '';
With Tinifile. Create (filename) Do
Begin
Try
Serverip: = readstring ('serverinfo', 'serverip ','');
Sqldbname: = readstring ('serverinfo', 'sqldbname ','');
Sqlusername: = readstring ('serverinfo', 'sqluserid ','');
Sqlpwd: = readstring ('serverinfo', 'sqlpwd ','');
Finally
Free;
End;
Result: = 'provider = sqloledb.1; Password = '+ sqlpwd +'; persist Security info = true; user id = '+ sqlusername +'; initial catalog = '+ sqldbname + '; data Source = '+ serverip;
End;


// Use
VaR
Ini_filename: string;

//.....

Ini_filename: = extractfilepath (application. exename) + 'db _ con. ini ';
If not fileexists (ini_filename) then
Begin
Showmessage ('database connection configuration file does not exist! ');
Application. terimate;
End;
With adoconnection1 do
Connection: = false;
Connectionstring: = get_db_con_str (ini_filename );
Try
Connection: = true;
//.....
Except
Showmessage ('database connection failed! Check the configuration file: '+ ini_filename );
//....
End;
End;

Here is a simple example:
Procedure tfrmsrv. btnsaveclick (Sender: tobject );
VaR I: integer;
Begin
PWD: = edtpwd. text;
For I: = 1 to length (PWD) Do
Begin
PWD [I]: = CHR (ord (PWD [I]) XOR 4 );
End;
Myinifile: = Tinifile. Create (extractfilepath (paramstr (0) + '\ setup. ini ');
Myinifile. writestring ('setup', 'Data _ source', edtsrv. Text );
Myinifile. writestring ('setup', 'initial _ catalog ', edtdb. Text );
Myinifile. writestring ('setup', 'user _ id', edtuser. Text );
Myinifile. writestring ('setup', 'Password', PWD );
Myinifile. Free;
End;
End;

As shown in the preceding figure, the storage and read operations are the same. You just need to perform the operation again. OK?

Ha, can be used directly

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.