我用ADO连SQL数据库,我现在想写个INI文件,每次连数据库都读这个INI文件,不知道该如何实现(INI文件的内容和VB中如何调用它),还望指教,谢谢!

解决方案 »

  1.   

    '模块说明:用于对INI文件的读写操作
    Option Explicit
    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 lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long'功能简介:对INI文件进行读操作
    '参数一:文件路径
    '参数二:条目的小节名称
    '参数三:项名或条目名
    Function GetProfileString(StrFileName As String, StrAppName As String, StrKeyName As String) As String
        GetProfileString = String(255, 0)
        GetPrivateProfileString StrAppName, StrKeyName, "", GetProfileString, 255, StrFileName
        GetProfileString = Left(GetProfileString, InStr(GetProfileString, Chr(0)) - 1)
    End Function'功能简介:对INI文件进行写操作
    '参数一:文件路径
    '参数二:条目的小节名称
    '参数三:项名或条目名
    '参数四:写操作字符串
    Function WriteProfilestring(StrFileName As String, StrAppName As String, StrKeyName As String, StrWrite As String) As Boolean
        On Error GoTo WriteErr
        WritePrivateProfileString StrAppName, StrKeyName, StrWrite, StrFileName
        WriteProfilestring = True
        Exit Function
    WriteErr:
    End Function
      

  2.   

    '模块说明:用于对INI文件的读写操作
    Option Explicit
    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 lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long'功能简介:对INI文件进行读操作
    '参数一:文件路径
    '参数二:条目的小节名称
    '参数三:项名或条目名
    Function GetProfileString(StrFileName As String, StrAppName As String, StrKeyName As String) As String
        GetProfileString = String(255, 0)
        GetPrivateProfileString StrAppName, StrKeyName, "", GetProfileString, 255, StrFileName
        GetProfileString = Left(GetProfileString, InStr(GetProfileString, Chr(0)) - 1)
    End Function'功能简介:对INI文件进行写操作
    '参数一:文件路径
    '参数二:条目的小节名称
    '参数三:项名或条目名
    '参数四:写操作字符串
    Function WriteProfilestring(StrFileName As String, StrAppName As String, StrKeyName As String, StrWrite As String) As Boolean
        On Error GoTo WriteErr
        WritePrivateProfileString StrAppName, StrKeyName, StrWrite, StrFileName
        WriteProfilestring = True
        Exit Function
    WriteErr:
    End Function
      

  3.   

    RWINI.BAS模块的程序代码如下:  在General-Declearation部分中声明使用到的WindowsAPI函数:Public Declare Function GetprivateprofileString Lib“Kernel”(ByVal lpAppName As String,ByVal lpKeyName As _
      String,ByVal lpDefault As String,ByVal lpRetrm-String As String,ByVal cbReturnString As Integer, _
      ByVal Filename As String)As Integer
    Public Declare Function GetPrivatePfileInt Lib“Kernel”(ByVal lpAppName As String,ByVal lpKeyName As String, _
      ByVal lpDefault As Integer,ByVal Filename As String)As Integer
    Public Declare Funciton WritePrivateprofileString Lib“Kernel”(ByVal lpApplicationName As String, _
      ByVal lpKeyName As String,ByVal lpString As String,ByVal lplFileName As String)As Integer
    Public Function GetIniS(ByVal SectionName As String,ByValKeyWord As String,ByVal DefString As String)As String
      Dim ResultString As String*144,Temp As Integer
      Dim s As String,i As Integer
      Temp%=GetPrivateProfileString(SectionName,KeyWord,"",ResultString,144,AppProfileName())
      ’检索关键词的值
      If Temp%>0 Then ’关键词的值不为空
      s=""
      For i=1 To 144
       If Asc(Mid$(ResultString,I,1))=0 Then
        ExitFor
       Else
        s=s & Mid$(ResultString,I,1)
       End If
      Next
      Else
      Temp%=WritePrivateProfilesString(sectionname,KeyWord,DefString,ppProfileName())
      ’将缺省值写入INI文件
      s=DefString
      End If
      GetIniS=s
    End FunctionPublic Function GetIniN(ByVal SectionName As String,ByVal KeyWord As String,ByVal DefValue As Ineger)As Integer
      Dim d As Long,s As String
      d=DefValue
      GetIniN=GetPrivateProfileInt(SectionName,
      KeyWord,DefValue,ppProfileName())
      If d<>DefValue Then
       s=""
       d=WritePrivateProfileString(SectionName,
       KeyWord,s,AppProfileName())
      End If
    End FunctionPublic Sub SetIniS(ByVal SectionName As String,BtVal KeyWord As String,ByVal ValStr As String)
      Dim res%
      res%=WritePrivateprofileString(SectionName,KeyWord,ValStr,AppProfileName())
    End Sub
    Public Sub SetIniN(ByVal SectionName As String,ByVal KeyWord As String,ByVal ValInt As Integer)
      Dim res%,s$
      s$=Str$(ValInt)
      res%=WriteprivateProfileString(SectionName,KeyWord,s$,AppProfileName())
    End SubSectionName为每一部分的标题,KeyWord为关键词,GetIniS和GetIniN中的DefValue为关键词的缺省值,SetIniS和SetIniN的ValStr和ValInt为要写入INI文件的关键词的值。
      

  4.   


    Option Explicit'declares for ini controlling
    Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
    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 WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
    Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long'when form is loaded
    Private Sub Form_Load()'if error occures resume still
    On Error Resume Next'local variables
    Dim File As String, OFLen As Double, _
        Str As String'set our varibles
    File = "C:\temp.txt"
    OFLen = FileLen(File)'write few example sections:
    WriteIniSection File, "Test1", ""
    WriteIniSection File, "Test2", "Here shoud be found some text"'write few ini strings
    WriteIni File, "Test3", "Ini1", "This is ini 1"
    WriteIni File, "Test1", "Ini2", "This is ini 2"'inform we're written the data
    MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to " & Chr(34) & File & Chr(34)'read the ini file
    Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") & vbCrLf
    Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") & vbCrLf
    Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") & vbCrLf
    Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") & vbCrLf'show data
    MsgBox Str'end application
    EndEnd Sub'// INI CONTROLLING PROCEDURES'reads ini string
    Public Function ReadIni(Filename As String, Section As String, Key As String) As String
    Dim RetVal As String * 255, v As Long
    v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
    ReadIni = Left(RetVal, v - 1)
    End Function'reads ini section
    Public Function ReadIniSection(Filename As String, Section As String) As String
    Dim RetVal As String * 255, v As Long
    v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
    ReadIniSection = Left(RetVal, v - 1)
    End Function'writes ini
    Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
    WritePrivateProfileString Section, Key, Value, Filename
    End Sub'writes ini section
    Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
    WritePrivateProfileSection Section, Value, Filename
    End Sub
      

  5.   

    转发类声明Dim strCharB, strCharA
    Dim strSectionTemp As String
    Dim strNameTemp As String
    Dim strreturn As String
    Global strPath As String
    '------------------------INI文件的操作---------------------------
     Function setProfile(strFileName As String, strSection As String, strName As String, strSave As String) As Boolean
      Dim strTemp As String
      Dim strfileback As String
      Dim strreturn As String
      strfileback = App.Path & "\ini\tmp.tmp" '临时文件是用来存放中转信息的
      
      Open strFileName For Input As #1
      Open strfileback For Output As #2
       Do While Not EOF(1)
        Line Input #1, strTemp
        strreturn = strTemp
        Print #2, strreturn
        If InStr(1, Trim(strTemp), "[") <> 0 Then
          If InStr(1, Trim(strTemp), Trim(strSection)) <> 0 Then
            Do While Not EOF(1)
                Line Input #1, strTemp
                If InStr(1, Trim(strTemp), Trim(strName)) <> 0 Then Exit Do  '找到所要修改的字段值
                strreturn = strTemp
                Print #2, strreturn  '拷贝不需要的字段值
             Loop
             strreturn = strName & "=" & strSave  '修改
             Print #2, strreturn
          End If
        End If
       Loop
      Close #1
      Close #2
      Open strfileback For Input As #1
      Open strFileName For Output As #2
      Do While Not EOF(1) And EOF(2)
      Line Input #1, strreturn
       Print #2, strreturn
      Loop
      Close #1
      Close #2
    End FunctionFunction GetProfile(strFileName As String, strSection As String, strName As String) As String
      '这个函数是用来对INI文件进行读操作的
      '函数说明:
      'strFileName 是所要读取的文件名
      'strSection  是这个文件中的一个节点名
      'strName 是所要查找的字段名
         strSectionTemp = ""
       strNameTemp = ""
       strreturn = ""
       On Error GoTo ErrSrchSection
       Open strFileName For Input As #1
       ' 下面这段程序是用来查找节点的
         Do While Not EOF(1)
            strCharA = Input(1, #1)
            If strCharA = "[" Then
               Do While Not EOF(1)
                 strCharB = Input(1, #1)
                 If strCharB = "]" Then Exit Do
                 strSectionTemp = strSectionTemp & strCharB
               Loop
            End If
            If strSectionTemp = strSection Then
              strCharA = Input(2, #1)
              Exit Do
            Else
              strSectionTemp = ""
            End If
         Loop
     On Error GoTo ErrReadFile
      
    aa:
        '下面这段程序是用来查找所要查找的字段的
        strNameTemp = ""
        Do While Not EOF(1)
          strCharA = Input(1, #1)
          If strCharA <> "=" Then
            strNameTemp = strNameTemp & strCharA  '得到名称
          Else
            Exit Do
          End If
        Loop
            If strNameTemp = strName Then
           Line Input #1, strreturn  '如果找到与它匹配的字段名,就返回得到的值
        Else
           Line Input #1, strreturn  '如果未找到与它匹配的字段名,就继续找
           GoTo aa
        End If
        Close #1
        GetProfile = strreturn
        Exit Function
    ErrReadFile:
        Dim inrRet As Integer
        intret = MsgBox("在文件中没有找到所要查找的字段", vbAbortRetryIgnore, "错误信息")
        Select Case intret
           Case vbAbort
              GetProfile = ""
              Close #1
              Exit Function
           Case vbRetry
              Resume
           Case vbIgnore
              Resume Next
         End Select
    ErrSrchSection:
         MsgBox "节点未找到", vbOKOnly
         GetProfile = ""
         Close #1
    End Function
    'end------------------INI文件的操作------------------
    '---------------------程序中调用,保存部分---------
    strPath = App.Path & "\ini\prnset.ini"
    setProfile strPath, "存根", "科目.X", T0x
    '---------------------程序中调用,调用部分---------'---------------------------INI配置读取-----------------
    strPath = App.Path & "\ini\prnset.ini"T0x = GetProfile(strPath, "存根", "科目.X")
      

  6.   

    http://www.merlin.com.cn/Forum/showthread.php?threadid=787:)