Functions used to read ini configuration files using ASP

Source: Internet
Author: User
Requirements:
You can read the corresponding values according to the Section and key of the INI file.
For example, a configuration file
Smsvote. ini
---------------------------------
[Smsvote]
Server = (local)
DB = smsvote
User = sa
Password = 123
[Db2vote]
Server = 192.168.0.1
DB = DB2
User = sa
Password =
---------------------------------
 
 
Main Program (method ):
 
INIFILE. asp
-----------------------------------------------
<%
Set inifiledictionary = Createobject ("scripting. Dictionary ")
 
Sub inifileload (byval filspc)
Inifiledictionary. removeall
Filspc = lcase (filspc)
If left (filspc, 1) = "p" then
'Physical path
Phypth = mid (filspc, instr (filspc, "=") + 1)
Else
'Virtual path
Phypth = server. mappath (mid (filspc, instr (filspc, "=") + 1 ))
End if
 
Set filsys = Createobject ("scripting. FileSystemObject ")
Set inifil = filsys. opentextfile (phypth, 1)
Do while not inifil. atendofstream
Strbuf = inifil. Readline
If strbuf <> "" then
'There is data on this line
If left (strbuf, 1) <> ";" then
'It's not a comment
If left (strbuf, 1) = "[" then
It's a section header
Hdrbuf = mid (strbuf, 2, Len (strbuf)-2)
Else
'It's a value
Strptr = instr (strbuf, "= ")
Altbuf = lcase (hdrbuf & "Left" & left (strbuf, strptr-1 ))
Do While inifiledictionary. exists (altbuf)
Altbuf = altbuf &"_"
Loop
Inifiledictionary. Add altbuf, mid (strbuf, strptr + 1)
End if
End if
End if
Loop
Inifil. Close
Set inifil = nothing
Set filsys = nothing
End sub
 
Function inifilevalue (byval valspc)
Dim ifarray
Strptr = instr (valspc, "weight ")
Valspc = lcase (valspc)
If strptr = 0 then
'They want the whole section
Strbuf = ""
Strptr = Len (valspc) + 1
Valspc = valspc + "canonical"
Ifarray = inifiledictionary. Keys
For I = 0 to inifiledictionary. Count-1
If left (ifarray (I), strptr) = valspc then
'This is from the Section
If strbuf <> "" then
Strbuf = strbuf &"~ "
End if
Strbuf = strbuf & ifarray (I) & "=" & inifiledictionary (ifarray (I ))
End if
Next
Else
'They want a specific value
Strbuf = inifiledictionary (valspc)
End if
Inifilevalue = strbuf
End Function
Function CHR (section, key)
Char1 = inifilevalue (Section)
Searchstring = char1
Searchchar = Key
Mypos = instr (1, searchstring, searchchar, 1)
'Char2 = Section + key
Char1 = mid (char1, Mypos + Len (key) + 1, Len (char1)-Mypos + 1)
Searchstring = char1
Searchchar = "~ "
Mypos = instr (1, searchstring, searchchar, 1)
If Mypos <> 0 then
Char1 = mid (char1, 1, MyPos-1)
Else
Char1 = mid (char1, 1)
End if
CHR = char1
End Function
%>
 
How to use it?
 
Look at this:
Conn. asp
-----------------------------------------------
<! -- # Include file = "INIFILE. asp" -->
 
<%
On Error resume next
Dim Conn, connstr, dbuid, dbpwd, dbname, dbip
Call inifileload ("virtual = smsvote. ini") 'name of the configuration file
Dbuid = CHR ("smsvote", "user") 'section = "smsvote", key = "user"
Dbpwd = CHR ("smsvote", "password") 'section = "smsvote", key = "password"
Dbname = CHR ("smsvote", "DB") 'section = "smsvote", key = "DB"
Dbip = CHR ("smsvote", "server") 'section = "smsvote", key = "server"
 
Set conn = server. Createobject ("ADODB. Connection ")
Connstr = "provider = sqloledb; Data Source =" & dbip & "; uid =" & dbuid & "; Pwd =" & dbpwd & "; database =" & dbname
Conn. Open connstr
'Response. Write Conn
Response. Write err. Description
%>

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.