How to save the database connection parameter code and detailed steps

Source: Internet
Author: User

When designing database applications, you often need to extract some information from the program to ensure the portability of the program. The most important information is the database connection parameters.

In Delphi, it is very easy to obtain the correct database connection parameters. You only need to create a data module, add an ADO connection to it, and double-click it, select "use connection string" in the pop-up window, click "build", and select the appropriate driver engine in the "Data Link Properties" window, set the connection parameters. Click "OK". The connection string is automatically generated and displayed in the connection string of the object inspector.

Taking Microsoft SQL Server as an example, the connection string is similar:

 


      Provider=SQLOLEDB.1;Password=map;Persist Security Info=True;            User ID=map;Initial Catalog=SuperWorkFlow;Data Source=GTSERVER

Parameters in a connection string are separated by semicolons (;). The meanings are as follows:

 

Provider -- driver type;

Data source -- server name;

Initial catalog -- database name;

User ID -- User Name;

Password -- password;

Persist Security info -- whether to use continuous security verification.

 

The first task we need to do now is to write parameters into an INI file.

Windows INI file, which can be interpreted as a Windows initialization file. It is a text file used to save application initialization information and runtime environment information. For example, Windows 3.1 contains two famous INI files: win. ini and system. during Windows Startup, ini defines the mouse response speed, shell, and other settings in Windows. Many applications attached to Windows also have their own ini files. Obtaining and Saving Windows software initialization parameters is achieved by reading text files with the INI extension. That is, you can search for the required parameters from the specified INI file before running the program, it is also reflected in the running environment of the program. When the program exits, it saves the current environment parameters to the specified INI file.

An INI file is a text file consisting of several sections. under each heading with parentheses, there are several Keyword and an equal sign. Each Keyword controls how a function of the application works, the Value on the right of the equal sign specifies the Keyword operation method. The general format is as follows:

 


      [section1]            keyword1=value1            keyword2=value2            [section2]            keyword1=value1            keyword2=value2

Specifically, SectionName and KeywordName are the segment names and keyword keywords respectively. Value is the set value corresponding to the keyword. If there is no content on the right side of the equal sign (Value is blank ), it indicates that the Windows application has specified the default value for the Keyword. If a Keyword cannot be found in the entire file, it also indicates that the default value is specified for the Keyword, the order in which each Section appears is irrelevant. In each Section, the order of each Keyword is also irrelevant. The Keyword value type is String or Integer, read/write operations should be performed in two cases.

The writing of INI files has strict requirements:

1. The Section name must be "[" and "]", and "[" must be in the first column of the screen.

2. the Keyword name must also start from the first column on the screen and be followed by "="

3. You can comment on the file. Each line of comment must start.

According to the above rules, we compile the following INI file storage database connection information:

 


      [dbParam]            Data Source=GTSERVER            Initial Catalog=SuperWorkFlow            User ID=map            Password=map

The following is how to write code. When the program is executed, the database connection parameters are read from the INI file. For this reason, I have compiled the following code:

 


      // Rwini. PAS {Module name: configuration file reader: Blue Writing Date: 2003-4-26 module function: read the required parameters from the system configuration file and write the required parameters to the system configuration file to generate a database connection string} unit rwini; interface uses inifiles; const striname = 'db. INI '; // The Name Of The INI file. Modify var iniparam: Tinifile; strlocal: string; Procedure readparam (out server: string; out Database: string; Out user: string; out password: string); Procedure writeparam (server: string; Database: string; User: string; Password: string); function getconnstr: string; implementation procedure readparam (out server: string; out Database: string; Out user: string; out password: string); begin // read the database connection parameter server: = iniparam from the configuration file. readstring ('dbparam', 'Data source', '); database: = iniparam. readstring ('dbparam', 'initial catalog ','); User: = iniparam. readstring ('dbparam', 'user id', '); Password: = iniparam. readstring ('dbparam', 'Password', '); end; Procedure writeparam (server: string; Database: string; User: string; Password: string ); begin // write database connection parameters to the configuration file iniparam. writestring ('dbparam', 'Data source', server); iniparam. writestring ('dbparam', 'initial catalog ', database); iniparam. writestring ('dbparam', 'user id', user); iniparam. writestring ('dbparam', 'Password', password); end; function getconnstr: string; var server, database, user, password: string; begin // generate the database connection string readparam (server, database, user, password); Result: = 'provider = sqloledb.1; Password = '+ password +'; persist Security info = true; user ID = '+ User +'; initial catalog = '+ database +'; Data Source = '+ server; end; Initialization getdir (0, strlocal); iniparam: = Tinifile. create (strlocal + '/' + striname); finalization iniparam. free; end.

Add the following code to the Create event of the program data module:

 


      procedure TDM.DataModuleCreate(Sender: TObject);            var             confForm : TConf;            begin             conn.ConnectionString := RWini.getConnStr;             try            conn.Open;             except            on Err : EOLEException do            begin             confForm := TConf.Create(self);             confForm.ShowModal;            end;             end;            end;

In this way, the database connection parameters are automatically read from the INI file during program execution and connected to the database. If an error occurs during the connection, the parameter configuration window is displayed, allowing you to configure database connection parameters. The Configuration window code is as follows:

 


      // The above code omitting uses RWini; {$ R *. dfm} procedure TConf. formShow (Sender: TObject); var sServer, sDatabase, sUser, sPassword: String; begin readParam (sServer, sDatabase, sUser, sPassword); self. edtServer. text: = sServer; self. edtDatabase. text: = sDatabase; self. edtUser. text: = sUser; self. edtPassword. text: = sPassword; self. activeControl: = self. btnOK; end; procedure TConf. btnCancelClick (Sender: TObject); begin close; end; procedure TConf. btnOKClick (Sender: TObject); var sServer, sDatabase, sUser, sPassword: String; begin sServer: = self. edtServer. text; sDatabase: = self. edtDatabase. text; sUser: = self. edtUser. text; sPassword: = self. edtPassword. text; writeParam (sServer, sDatabase, sUser, sPassword); close; end;

 

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.