API: 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
'=============================================================================== ' 以下一段为读写INI文件用到的API涵数 '=============================================================================== 'INI文件的文件名 Public File As String#If Win32 Then 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 Integer, ByVal lpFileName As String) As Integer Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal FileName As String) As Integer #Else Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal FileName As String) As Integer #End If'删除一个[OPITION] Sub DeleteSection(ByVal Section As String)Dim retval As Integer retval = WritePrivateProfileString(Section, 0&, "", File)End Sub '保存一个key Public Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)Dim retval As Integer SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)End Function '得到一个key值 Public Function GetSettingB(ByVal Section As String, ByVal KeyName As String) As StringDim retval As Integer Dim t As String * 255 retval = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File) If retval > 0 Then GetSettingB = Left$(t, retval) Else GetSettingB = "Unknown section or key" End If End Function'得到一个[OPITION] Public Function GetSection(ByVal Section As String, KeyArray() As String) As IntegerDim retval As Integer Dim t As String * 2500 Dim lastpointer As Integer Dim nullpointer As Integer Dim ArrayCount As Integer Dim keystring As String
' If there is one, return it If retval > 0 Then ' ' Separate the keys and store them in the array nullpointer = InStr(t, Chr$(0)) lastpointer = 1 Do While (nullpointer <> 0 And nullpointer > lastpointer + 1) ' ' Extract key string keystring = Mid$(t, lastpointer, nullpointer - lastpointer) ' ' Now add to array ArrayCount = ArrayCount + 1 ReDim Preserve KeyArray(ArrayCount) KeyArray(ArrayCount) = keystring ' ' Find next null lastpointer = nullpointer + 1 nullpointer = InStr(nullpointer + 1, t, Chr$(0)) Loop End If ' ' Return the number of array elements GetSection = ArrayCount
End Function
这是我读用户设置的代码 Open App.Path + "\conn.ini" For Input As #1 '開啟連線組態檔
Do While Not EOF(1)
Line Input #1, tmpStr
If Mid(tmpStr, 1, 10) = "ServerName" Then ServerName = Mid(Trim(tmpStr), 12) End If
If Mid(tmpStr, 1, 8) = "ServerID" Then ServerID = Trim(Mid(Trim(tmpStr), 10)) End If
If Mid(tmpStr, 1, 10) = "ServerPass" Then ServerPass = Trim(Mid(Trim(tmpStr), 12)) End If
If Mid(tmpStr, 1, 6) = "DBName" Then DBName = Mid(Trim(tmpStr), 8) End If
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
'===============================================================================
' 以下一段为读写INI文件用到的API涵数
'===============================================================================
'INI文件的文件名
Public File As String#If Win32 Then
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 Integer, ByVal lpFileName As String) As Integer
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal FileName As String) As Integer
#Else
Private Declare Function GetPrivateProfileString Lib "Kernel" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
Private Declare Function WritePrivateProfileString Lib "Kernel" (ByVal Appname As String, ByVal KeyName As Any, ByVal NewString As Any, ByVal FileName As String) As Integer
#End If'删除一个[OPITION]
Sub DeleteSection(ByVal Section As String)Dim retval As Integer retval = WritePrivateProfileString(Section, 0&, "", File)End Sub
'保存一个key
Public Function SaveSetting(ByVal Section$, ByVal Key$, ByVal Value$)Dim retval As Integer SaveSetting = WritePrivateProfileString(Section$, Key$, Value$, File)End Function
'得到一个key值
Public Function GetSettingB(ByVal Section As String, ByVal KeyName As String) As StringDim retval As Integer
Dim t As String * 255
retval = GetPrivateProfileString(Section, KeyName, "unknown value", t, Len(t), File)
If retval > 0 Then
GetSettingB = Left$(t, retval)
Else
GetSettingB = "Unknown section or key"
End If
End Function'得到一个[OPITION]
Public Function GetSection(ByVal Section As String, KeyArray() As String) As IntegerDim retval As Integer
Dim t As String * 2500
Dim lastpointer As Integer
Dim nullpointer As Integer
Dim ArrayCount As Integer
Dim keystring As String
ReDim KeyArray(0)
retval = GetPrivateProfileString(Section, 0&, "", t, Len(t), File)
' If there is one, return it
If retval > 0 Then
'
' Separate the keys and store them in the array
nullpointer = InStr(t, Chr$(0))
lastpointer = 1
Do While (nullpointer <> 0 And nullpointer > lastpointer + 1)
'
' Extract key string
keystring = Mid$(t, lastpointer, nullpointer - lastpointer)
'
' Now add to array
ArrayCount = ArrayCount + 1
ReDim Preserve KeyArray(ArrayCount)
KeyArray(ArrayCount) = keystring
'
' Find next null
lastpointer = nullpointer + 1
nullpointer = InStr(nullpointer + 1, t, Chr$(0))
Loop
End If
'
' Return the number of array elements
GetSection = ArrayCount
End Function
Open App.Path + "\conn.ini" For Input As #1 '開啟連線組態檔
Do While Not EOF(1)
Line Input #1, tmpStr
If Mid(tmpStr, 1, 10) = "ServerName" Then
ServerName = Mid(Trim(tmpStr), 12)
End If
If Mid(tmpStr, 1, 8) = "ServerID" Then
ServerID = Trim(Mid(Trim(tmpStr), 10))
End If
If Mid(tmpStr, 1, 10) = "ServerPass" Then
ServerPass = Trim(Mid(Trim(tmpStr), 12))
End If
If Mid(tmpStr, 1, 6) = "DBName" Then
DBName = Mid(Trim(tmpStr), 8)
End If
Loop
Close #1
strAdo_Connect_s1 = "PROVIDER=MSDASQL;DRIVER=SQL Server;SERVER=" & Trim(ServerName) & ";DATABASE=" & Trim(DBName) & ";uid=" & Trim(ServerID) & ";pwd=" & Trim(ServerPass) & ";"
adoConn1.Open strAdo_Connect_s1