【问题标题】:How do i view access database ini file records?如何查看访问数据库的ini文件记录?
【发布时间】:2015-03-13 05:51:08
【问题描述】:

我有一个 vb6 项目。是前辈开发的。现在我想更改现有代码。我是 vb6 的新手。类似ini格式的数据库文件。实际上,他使用的是访问数据库。我想在这个数据库中创建一些表。请给出在access数据库中打开ini文件的任何想法或打开ini文件的任何想法。

【问题讨论】:

    标签: ms-access vb6 ini


    【解决方案1】:

    天哪,这是旧东西:

    Jose 的 VB 提示和技巧

    使用初始化文件


    套用伟大的美国作家马克吐温的话说,关于死亡的报道 的 .ini 文件被大大夸大了。

    虽然 Microsoft 已宣布注册表是 初始化信息,.ini 文件仍有其用途。之间 .ini 文件的优点是:

    • 使用任何简单的文本编辑器,这些文件都易于“人类阅读” 例如记事本。
    • 处理 .ini 文件的 API 代码比 等效的注册表 API。
    • 文件可以通过网络轻松打开,只需一个 两端都安装了基本重定向器。
    • .ini 文件的安装非常简单,只需将文件复制到 Windows 目录。

    Windows 提供了多种用于处理 .ini 文件的 API,包括 专用于使用的 GetProfileXXX 和 WriteProfileXXX 函数 win.ini 及以下私有读写函数 初始化文件:

    GetPrivateProfileString 从 .ini 文件中读取字符串。 GetPrivateProfileInt 从 .ini 文件中读取一个整数。 WritePrivateProfileString 将字符串写入 .ini 文件。 WritePrivateProfileInt 将整数写入 .ini 文件。

    但是,鉴于 .ini 文件中的所有数据都是纯旧文本, 真的没有必要单独编码这些的 xxxInt 版本 职能。在 VB 中将字符串转换为 Int 非常简单,使用 CInt() 或 Val() 函数,所以只有 GetPrivateProfileString 和 需要 WritePrivateProfileString 函数。

    这两个都是简单的 API 调用。有一种例外情况 读取 .ini 文件,该文件返回一个 C 字符串,其中多个值由 空值。为了解析该字符串,我包含了 MultiCStringToStringArray 功能。

    在继续之前,您需要在声明中添加两个 Declare 某处的模块部分。作为习惯,我给 Win32 API 起别名 使用“w32_”,这样我就可以编写一个 VB 包装函数并为其命名 API 函数。

    这是声明部分:

    Option Explicit
    
    Private Declare Function w32_GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
      ByVal lpAppName As String, _
      ByVal lpKeyName As String, _
      ByVal lpDefault As String, _
      ByVal lpReturnedString As String, _
      ByVal nSize As Long, _
      ByVal lpFileName As String) As Long
    
    Private Declare Function w32_WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
      ByVal lpApplicationName As String, _
      ByVal lpKeyName As Any, _
      ByVal lpString As Any, _
      ByVal lpFileName As String) As Long
    

    这是 GetPrivateProfileString 的代码:

    Public Function GetPrivateProfileString( _
      psAppName As String, _
      psKeyName As String, _
      Optional pvsDefault As Variant, _
      Optional pvsFileName As Variant) As String
    '********************
    ' Purpose:    Get a string from a private .ini file
    ' Parameters:
    ' (Input)
    '   psApplicationName - the Application name
    '   psKeyName - the key (section) name
    '   pvsDefault - Default value if key not found (optional)
    '   pvsFileName - the name of the .ini file
    ' Returns:  The requested value
    ' Notes:
    '   If no value is provided for pvsDefault, a zero-length string is used
    '   The file path defaults to the windows directory if not fully qualified
    '   If pvsFileName is omitted, win.ini is used
    '   If vbNullString is passed for psKeyName, the entire section is returned in
    '     the form of a multi-c-string. Use MultiCStringToStringArray to parse it after appending the
    '     second null terminator that this function strips. Note that the value returned is all the
    '     key names and DOES NOT include all the values. This can be used to setup multiple calls for
    '     the values ala the Reg enumeration functions.
    '********************
    
      ' call params
      Dim lpAppName As String
      Dim lpKeyName As String
      Dim lpDefault As String
      Dim lpReturnedString As String
      Dim nSize As Long
      Dim lpFileName As String
      ' results
      Dim lResult As Long
      Dim sResult As String
    
      sResult = ""
    
      ' setup API call params
      nSize = 256
      lpReturnedString = Space$(nSize)
      lpAppName = psAppName
      lpKeyName = psKeyName
      ' check for value in file name
      If Not IsMissing(pvsFileName) Then
        lpFileName = CStr(pvsFileName)
      Else
        lpFileName = "win.ini"
      End If
      ' check for value in optional pvsDefault
      If Not IsMissing(pvsDefault) Then
        lpDefault = CStr(pvsDefault)
      Else
        lpDefault = ""
      End If
      ' call
      ' setup loop to retry if result string too short
      Do
        lResult = w32_GetPrivateProfileString( _
            lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize, lpFileName)
        ' Note: See docs for GetPrivateProfileString API
        ' the function returns nSize - 1 if a key name is provided but the buffer is too small
        ' the function returns nSize - 2 if no key name is provided and the buffer is too small
        ' we test for those specific cases - this method is a bit of hack, but it works.
        ' the result is that the buffer must be at least three characters longer than the
        ' longest string(s)
        If (lResult = nSize - 1) Or (lResult = nSize - 2) Then
          nSize = nSize * 2
          lpReturnedString = Space$(nSize)
        Else
          sResult = Left$(lpReturnedString, lResult)
          Exit Do
        End If
      Loop
    
      GetPrivateProfileString = sResult
    
    End Function
    

    这是 WritePrivateProfileString:

    Public Function WritePrivateProfileString( _
      psApplicationName As String, _
      psKeyName As String, _
      psValue As String, _
      psFileName As String) As Boolean
    '********************
    ' Purpose:    Write a string to an ini file
    ' Parameters: (Input Only)
    '   psApplicationName - the ini section name
    '   psKeyName - the ini key name
    '   psValue - the value to write to the key
    '   psFileName - the ini file name
    ' Returns:    True if successful
    ' Notes:
    '   Path defaults to windows directory if the file name
    '   is not fully qualified
    '********************
    
      Dim lResult As Long
      Dim fRV As Boolean
    
      lResult = w32_WritePrivateProfileString( _
          psApplicationName, _
          psKeyName, _
          psValue, _
          psFileName)
      If lResult <> 0 Then
        fRV = True
      Else
        fRV = False
      End If
    
      WritePrivateProfileString = fRV
    
    End Function
    

    最后,这里是 MultiCStringToStringArray:

    Public Sub MultiCStringToStringArray(psMultiCString As String, psaStrings() As String)
    'Created: Joe Garrick 01/06/97 9:28 AM
    '********************
    ' Purpose:    Convert a multi-string C string to an array of strings
    ' Parameters:
    '   (Input)
    '   psMultiCString - the multiple C string
    '   (Output)
    '   psaStrings - returned array of strings
    ' Notes:
    '   The original array should be empty and ReDim-able
    '********************
    
      Dim iNullPos As Integer
      Dim iPrevPos As Integer
      Dim iIdx As Integer
    
      ' initialize array, setting first element to a zero-length string
      iIdx = 0
      ReDim psaStrings(0 To iIdx + 1)
      psaStrings(iIdx + 1) = ""
    
      Do
        ' find null char
        iNullPos = InStr(iPrevPos + 1, psMultiCString, vbNullChar)
        ' double null encountered if next pos is old pos + 1
        If iNullPos > iPrevPos + 1 Then
          ' assing to the string array
          psaStrings(iIdx) = Mid$(psMultiCString, (iPrevPos + 1), ((iNullPos - 1) - iPrevPos))
          iIdx = iIdx + 1
          ReDim Preserve psaStrings(0 To iIdx)
          iPrevPos = iNullPos
        Else
          ' double null found, remove last (empty) element and exit
          ReDim Preserve psaStrings(0 To iIdx - 1)
          Exit Do
        End If
      Loop
    
    End Sub
    

    这就是编码 .ini 文件的全部内容。

    注意事项

    • 查看 SDK 文档以了解这些行为的细节 功能。
    • 如果你向写函数发送一个长度为零的字符串,键是 已删除。
    • 如果您尝试为不存在的键写入值或 不存在的部分,将创建键或部分。
    • 尽管 Microsoft 将注册表描述为中心 Win95下的配置信息库,win.ini和 system.ini 仍在使用,因此在使用这些文件时要小心 文件(换句话说,在您进行实验之前进行备份)。
    • GetPrivateProfileString 返回请求的数据,但 WritePrivateProfileString 返回一个表示成功的布尔值或 失败。虽然 GetPrivateProfileString 非常可靠,但它可以 很容易修改为返回布尔值或其他类型的状态 指示结果的代码。
    • 如果您使用 GetPrivateProfileString 返回整个部分, 记得添加一个额外的 null (字符串和 vbNullChar 会这样做) 在调用 MultiCStringToStringArray 之前,因为该函数需要 两个空值来终止字符串。另外,请记住,只有 返回的是键名,而不是值。

    返回页首[返回页首]


    |主页 | Jose 的 Visual Basic 世界 | Jose 的 VB 提示和技巧 | | © 1997 乔·加里克 |资讯中心 | [邮箱]jgarrick@citilink.com |


    【讨论】:

      猜你喜欢
      • 2019-09-09
      • 1970-01-01
      • 2011-01-02
      • 2017-06-21
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多