Delphi log development-inject "thoughts" to allow programs to manipulate data

Source: Internet
Author: User

ProgramAlthough the startup screen is already available, and then you can log on to the main form, there is no soul and you cannot interact with the database. This time, let the program manipulate the data.
Create a datamodule1 container to store the data component and name it frmdm. The unit name is untdm. Pas.
Put the database connection component in the frmdm window to connect to the database. adoconnection1 is named condb, and set its attribute: connectionstring to provider = sqloledb.1; persist Security info = false; user ID = sa; initial catalog = xfsoft; Data Source = .; use procedure for prepare = 1; Auto translate = true; packet size = 4096; workstation id = syz; use encryption for Data = false; tag with column collation when possible = false, you can set it step by step in the window. This is the string connecting to the database, and then set loginprompt to false. The logon window is not displayed during logon.
We need the program to design some information to connect to the database at runtime, and use the INI file to connect the read/write program to the Database Configuration File during runtime, and create a dbconfig under the program directory. INI, file content: [database]
Dbconstr = provider = sqloledb.1; persist Security info = false; user id = sa; initial catalog = xfsoft; Data Source = .; use procedure for prepare = 1; Auto translate = true; packet size = 4096; workstation id = syz; use encryption for Data = false; tag with column collation when possible = false

Add the INI read/write unit file to the program: The untinifile. Pas function is used to read and write the INI file just created.Code You can refer to the source code.
Next, set the login form and Data Binding. First, the user must appear in the drop-down list box, which does not require customer input, you can select the user in the drop-down list box after creating the login form. Therefore, the data in the initial list box in the form creation event is as follows:
Procedure tfrmlogin. formcreate (Sender: tobject );
VaR
I: integer;
Teststr: string;
Begin
Logincount: = 1;
With qrylogin do
Begin
Connection: = frmdm. condb;
Close;
SQL. Clear;
SQL. Add ('select * from [user] ');
Teststr: = SQL. text;
Open;
For I: = 1 to recordcount do
Begin
Cbbusername. Items. Add (fieldbyname ('username'). asstring );
Next;
End;
End;
End;
At this time, the debugging program will encounter errors. The problem is that we are not connected to the data because the connection string in the INI file is read during the program running. Therefore, we need to connect to the database when creating the data form, we create a connectdb process in untglobal and call it in the datamodulecreate event of the data form. The code of the connectdb process is as follows:
Procedure connectdb;
Begin
If fileexists (extractfilepath (paramstr (0) + 'dbconfig. ini ') then
Begin
Inioptions. loadfromfile (extractfilepath (paramstr (0) + 'dbconfig. ini '); // The extractfilepath function references the sysutils unit file.
Frmdm. condb. Connected: = false;
Frmdm. condb. connectionstring: = inioptions. databasedbconstr ;;
Try
Frmdm. condb. Connected: = true;
Except
Msgbox ('unable to connect to the database, please set the database! ', Mb_iconinformation); // mb_iconinformation constant references the Windows unit file
Application. Terminate;
End;
End else begin
Msgbox ('database configuration dbconfig. ini does not exist, the program is terminated! ', Mb_iconinformation); // mb_iconinformation constant references the Windows unit file
Application. Terminate;
End;
End;
The untinifile function is used to read the connection string. The msgbox function is an information prompt box function customized in untglobal.
Run the program again. You can select the user name in the user drop-down box of the logon form. But how can I match the password after clicking OK? How do I set the three-way logon limit? How can I use the Enter key to jump to the next focus? We will implement these functions one by one. First, we will implement the carriage return function. We only need to add the following code to the formkeypress event in the form:
If key = #13 then {judge to press the execution key}
Begin
Key: = #0;
Perform (wm_nextdlgctl, 0, 0); {move to the next control}
End
However, you must set the default attribute of the logon button to false.
The code for the Bento button event is as follows:
Gokpress: = true;
Qrylogin. close;
Qrylogin. SQL. Clear;
Qrylogin. SQL. add (format ('select * from [user] Where username = ''% s' and Password ='' % s' '', [cbbusername. text, edtpassword. text]);
Qrylogin. open;
If not qrylogin. isempty then
Begin
Gcanlogin: = true;
{Sysinfo. Username: = adoquery1.fieldbyname ('username'). asstring;
Sysinfo. userid: = adoquery1.fieldbyname ('userid'). asstring;
Sysinfo. No: = useredit. Text ;}
End;
Close;
Exit;
Description after being commented out.
Gokpress is used to determine when the logon is canceled.
Add the following code to formclosequery to determine three logon attempts:
If gokpress and not gcanlogin then
Begin
Glogincount: = glogincount + 1;
Gokpress: = false;
If (glogincount <4) and (glogincount> 0) then
Begin
Application. MessageBox ('the user number or password is incorrect. Please enter it again. ', 'Prompt information', mb_iconinformation );
Cbbusername. setfocus;
Cbbusername. selectall;
Canclose: = false;
End
Else
If glogincount> = 4 then
Application. MessageBox ('logon failed because the three inputs are incorrect. ', 'Prompt information', mb_iconinformation)
End
Now, all login forms for interacting with the database are complete.
Download the new source code:
[Url] http://files.cnblogs.com/edrp/demo.rar#/url]

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.