asp读配置文件的两种方法

方法一:(在项目中成功使用)

 

< head >
< title > RnNews </ title >
</ head >

< body >


< %

' strconnection="Driver={MySQL ODBC 3.51 Driver};Server={local};Database=gmdb; User=richman;Password=qazwsxedc;Option=3;"


strconnection
= " Driver= " + GetIni( " DBConnectstring " , " dsn " ,Server.MapPath( " ip.ini " )) + " ;Server= " + GetIni( " DBConnectstring " , " Server " ,Server.MapPath( " ip.ini " )) + " ;Database= " + GetIni( " DBConnectstring " , " Database " ,Server.MapPath( " ip.ini " )) + " ;User= " + GetIni( " DBConnectstring " , " User " ,Server.MapPath( " ip.ini " )) + " ;Password= " + GetIni( " DBConnectstring " , " Password " ,Server.MapPath( " ip.ini " )) + " ;Option= " + GetIni( " DBConnectstring " , " Option " ,Server.MapPath( " ip.ini " ))
' response.write strconnection
'
response.end
set  conn  =  server.createobject( " adodb.connection " )
conn.open strconnection
sql 
=   " select * from News where NewsFlag=1 order by NewsID desc "   ' SQL查询语句
set  rs  =  conn.execute(sql)
if   not  rs.bof  then
%
>

< %
 
' do while not rs.eof
  Function  GetIni(strPrimary,strSubKey,striniFilePath)
    
Set  myFso  =  Server.CreateObject( " Scripting.FileSystemObject "
    
Set  MyFile  =  myFso.OpenTextFile(striniFilePath)
  
    
dim  intCount 
    
dim  strState 
      
do  until MyFile.AtEndOfStream
              
              
if  intCount = 0   then
                    
if  MyFile.ReadLine = " [ "   &  strPrimary  &   " ] "   then
                          intCount
= 1
                         
                        
end   if
              
else
                   
                   strState
= MyFile.ReadLine
                    
if   UCase ( Left (strState, Len (strSubKey  &   " = " ))) = UCase (strSubKey  &   " = " then
                       GetIni
= Right (strState, Len (strState) - Len (strSubKey  &   " = " ))
                       
                  
end   if
             
end   if
           
Loop
           MyFile.Close
           
Set  MyFile = Nothing
           
Set  myFso = Nothing
 
End Function
%
>
< p >
< % = rs( " NewsContent " )% >
</ p >
< %
 
'  rs.movenext
  ' loop
% >
< %
 rs.close
else
 response.write(
" 对不起,没有找到数据。 " )
end   if
set  rs  =   nothing
conn.close
set  conn  =   nothing
%
>


</ body >

</ html >

 方法二:

 

Private    Declare    Function    GetPrivateProfileString   Lib    " Kernel32 "    Alias    " GetPrivateProfileStringA "    (ByVal   lpApplicationName    As     String ,   ByVal   lpKeyName    As    Any,   ByVal   lpDefault    As     String ,   ByVal   lpReturnedString    As     String ,   ByVal   nSize    As     Long ,   ByVal   lpFileName    As     String )    As     Long    
  
Private    Declare    Function    WritePrivateProfileString   Lib    " Kernel32 "    Alias    " WritePrivateProfileStringA "    (ByVal   AppName    As     String ,   ByVal   KeyName    As     String ,   ByVal   keydefault    As     String ,   ByVal   FileName    As     String )    As     Long    
    
    
  
Public     Function    ReadINI(FileInfo    As     String ,   Section    As     String ,   Key    As     String ,   iniDefault    As     String )    As     String    
  
' 文件、主键、键值、默认值   
   On     Error     Resume     Next    
    
  
Dim    DefaultValue    As     String    
  
Dim    ReturnedString    As     String    
  
Dim    nSize    As     Long    
  
Dim    Retval    As     Long    
    
  DefaultValue   
=     Space $( 254 )   
  DefaultValue   
=    iniDefault   
  ReturnedString   
=     Space $( 254 )   
  nSize   
=     254    
    
  Retval   
=    GetPrivateProfileString(Section,   Key,   DefaultValue,   ReturnedString,   nSize,   FileInfo)   
  ReadINI   
=     Trim (ReturnedString)   
  
End     Function    
    
  
Public     Function    WriteINI(inifile    As     String ,   inisection    As     String ,   inikey    As     String ,   Info    As     String )    As     String    
  
' 写入INI文件   
   ' 文件、主键、键值、默认值   
   On     Error     Resume     Next    
  
Dim    Retval    As     Long    
      Retval   
=    WritePrivateProfileString(inisection,   inikey,   Info,   inifile)   
      myWriteINI   
=     LTrim $(STR$(Retval))   
  
End     Function
这方法使用范围很广。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值