INI file is essentially a plain text file that has a standard structure. For more details, please see http://en.wikipedia.org/wiki/Ini_file
In PowerBuilder programming, there are 2 function that already prepared, to access INI file. INI files are typically used in applications created with PowerBuilder programming language, to deviate settings that are rarely changed, and locally, in the sense of the word that the only effect on the computer itself. The example most frequently used is to store data about the database connection from application them.
By storing database connection information in an INI file, will simplify the process of changing the data connection, if such a time there change the server name, etc. All it takes is changing the data connection stored in INI file, without having to change the script in application.
Here's the example:
Create a new INI file. Since INI file just a ordinary text file, you can use notepad to create it. Save and name it as SAMPLE.INI.
Type inside the SAMPLE.INI below:
1: [DATABASE]
2: DBMS=MSS Microsoft SQL Server 6.x
3: Database=MyDatabase
4: UserId=sa
5: DatabasePassword=
6: LogPassword=
7: ServerName=MyDBServer
8: LogId=sa
9: Lock=
10: DbParm=Connectstring='Log=0,SystemProcs=0'
11: Prompt=0
To read the INI file, you can use Profilestring function with this syntax below
1: ProfileString (filename, section, key, default)
where:
filename: A string whose value is the name of the profile file. If you do not specify a full path, ProfileString uses the operating system's standard file search order to find the file
section: A string whose value is the name of a group of related values in the profile file. In the file, section names are in square brackets. Do not include the brackets in section. Section is not case-sensitive
key: A string specifying the setting name in section whose value you want. The setting name is followed by an equal sign in the file. Do not include the equal sign in key. Key is not case-sensitive
default: A string value that ProfileString will return if filename is not found, if section or key does not exist in filename, or if the value of key cannot be converted to an integer
Example script to connect into database, database parameters and properties is reading from SAMPLE.INI file.
1: SQLCA.DBMS = ProfileString("SAMPLE.INI","DATABASE","DBMS","")
2: SQLCA.Database = ProfileString("SAMPLE.INI","DATABASE","Database","")
3: SQLCA.UserId = ProfileString("SAMPLE.INI","DATABASE","UserId","sa")
4: SQLCA.DatabasePassword= ProfileString("SAMPLE.INI","DATABASE","Database","passworddefault")
5: SQLCA.ServerName=ProfileString("SAMPLE.INI","DATABASE","ServerName","myserver")
6:
7: CONNECT USING SQLCA;
Compare with this script below:
1: SQLCA.DBMS="MSS Microsoft SQL Server 6.x"
2: SQLCA.Database="MyDatabase"
3: SQLCA.UserId="sa"
4: SQLCA.ServerName="MyDBServer"
5:
6: CONNECT USING SQLCA;
At first glance, it seems the same. But imagine if one day you required to upgrade your server database or rename the database your server, for the latter example, you must change the script PowerBuilder you and to distribute them to all your users. Unlike the first example, you simply change the INI file only.
If the above we have learned about the function ProfileString, which in essence take the configuration data in an INI file, now we learn another function that is the opposite, purposing storing a value into an INI file. The function called SETPROFILESTRING.
1: SetProfileString ( filename, section, key, value )
value: A string whose value is the value you want to specify for key
This function is useful if we want to change or save a value to in the INI file. Example"
1: SetProfileString ("SAMPLE.INI", "Database", "ServerName, "MyNewServerName" )
The function above will replacing the key values of ServerName to be MyNewServerName on file SAMPLE.INI, and in the section DATABASE
No comments:
Post a Comment