用ini文件GetPrivateProfileString The GetPrivateProfileString function retrieves a string from the specified section in an initialization file. This function is provided for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry. DWORD GetPrivateProfileString( LPCTSTR lpAppName, // points to section name LPCTSTR lpKeyName, // points to key name LPCTSTR lpDefault, // points to default string LPTSTR lpReturnedString, // points to destination buffer DWORD nSize, // size of destination buffer LPCTSTR lpFileName // points to initialization filename );
Parameters lpAppName Pointer to a null-terminated string that specifies the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer. lpKeyName Pointer to the null-terminated string containing the key name whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter. lpDefault Pointer to a null-terminated default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. This parameter cannot be NULL. Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks. Windows 95: Although lpDefault is declared as a constant parameter, the system strips any trailing blanks by inserting a null character into the lpDefault string before copying it to the lpReturnedString buffer. Windows NT: The system does not modify the lpDefault string. This means that if the default string contains trailing blanks, the lpReturnedString and lpDefault strings will not match when compared using the lstrcmp function. lpReturnedString Pointer to the buffer that receives the retrieved string. nSize Specifies the size, in characters, of the buffer pointed to by the lpReturnedString parameter. lpFileName Pointer to a null-terminated string that names the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory. Return Values The return value is the number of characters copied to the buffer, not including the terminating null character. If neither lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string is truncated and followed by a null character, and the return value is equal to nSize minus one. If either lpAppName or lpKeyName is NULL and the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters. In this case, the return value is equal to nSize minus two. Res The GetPrivateProfileString function searches the specified initialization file for a key that matches the name specified by the lpKeyName parameter under the section heading specified by the lpAppName parameter. If it finds the key, the function copies the corresponding string to the buffer. If the key does not exist, the function copies the default character string specified by the lpDefault parameter. A section in the initialization file must have the following form: [section] key=string . . .If lpAppName is NULL, GetPrivateProfileString copies all section names in the specified file to the supplied buffer. If lpKeyName is NULL, the function copies all key names in the specified section to the supplied buffer. An application can use this method to enumerate all of the sections and keys in a file. In either case, each string is followed by a null character and the final string is followed by a second null character. If the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters. If the string associated with lpKeyName is enclosed in single or double quotation s, the s are discarded when the GetPrivateProfileString function retrieves the string. The GetPrivateProfileString function is not case-sensitive; the strings can be a combination of uppercase and lowercase letters. To retrieve a string from the WIN.INI file, use the GetProfileString function. Windows NT: Calls to private profile functions may be mapped to the registry instead of to the specified initialization files. This mapping occurs when the initialization file and section are specified in the registry under the following keys: HKEY_LOCAL_MACHINE\Software\Microsoft\ Windows NT\CurrentVersion\IniFileMappingThis mapping is likely if an application modifies system-component initialization files, such as CONTROL.INI, SYSTEM.INI, and WINFILE.INI. In these cases, the GetPrivateProfileString function retrieves information from the registry, not from the initialization file; the change in the storage location has no effect on the function's behavior. The Win32 profile functions (Get/WriteProfile*, Get/WritePrivateProfile*) use the following steps to locate initialization information: Look in the registry for the name of the initialization file, say MYFILE.INI, under IniFileMapping: HKEY_LOCAL_MACHINE\Software\Microsoft\ Windows NT\CurrentVersion\IniFileMapping\myfile.ini Look for the section name specified by lpAppName. This will be a named value under myfile.ini, or a subkey of myfile.ini, or will not exist. If the section name specified by lpAppName is a named value under myfile.ini, then that value specifies where in the registry you will find the keys for the section. If the section name specified by lpAppName is a subkey of myfile.ini, then named values under that subkey specify where in the registry you will find the keys for the section. If the key you are looking for does not exist as a named value, then there will be an unnamed value (shown as <No Name>) that specifies the default location in the registry where you will find the key. If the section name specified by lpAppName does not exist as a named value or as a subkey under myfile.ini, then there will be an unnamed value (shown as <No Name>) under myfile.ini that specifies the default location in the registry where you will find the keys for the section. If there is no subkey for MYFILE.INI, or if there is no entry for the section name, then look for the actual MYFILE.INI on the disk and read its contents. When looking at values in the registry that specify other registry locations, there are several prefixes that change the behavior of the .INI file mapping: ! - this character forces all writes to go both to the registry and to the .INI file on disk. # - this character causes the registry value to be set to the value in the Windows 3.1 .INI file when a new user logs in for the first time after setup. @ - this character prevents any reads from going to the .INI file on disk if the requested data is not found in the registry. USR: - this prefix stands for HKEY_CURRENT_USER, and the text after the prefix is relative to that key. SYS: - this prefix stands for HKEY_LOCAL_MACHINE\SOFTWARE, and the text after the prefix is relative to that key.
包含什么?引用什么?你不过是想利用文件中的信息连接数据库,你只要在连接数据库前打开文件,读出配置信息,合成连接串。我这有代码,请参阅'数据库连接信息 Public Type DBINFO sDriver As String sServer As String sDatabase As String sUser As String sPassword As String End TypePublic Function gfGetSysInfo(audtDBINFO As DBINFO) As Long '---------------------------------------------------------- ' 从系统配置文件中读取系统信息(数据库连接信息、机组号) ' 参数: audtDBINFO - 信息读出后要保存的变量 ' 返回值: 0 - 成功 ' 1 - 失败 ' 失败后要置系统状态标志 2 - 配置文件不存在 ' 3 - 配置文件打开失败 '----------------------------------------------------------
Dim strFileName As String '配置文件名 Dim lngFileNo As Long '配置文件号
'若配置文件不存在,退出 If Dir(strFileName) = "" Then '函数返回值 gfGetSysInfo = 1 Exit Function End If
'得到空闲文件号 lngFileNo = FreeFile() '打开文件 On Error GoTo ErrProc Open strFileName For Input As #lngFileNo
'---- 搜索数据库连接信息 ----
'查找[ODBC]项 Do Until EOF(lngFileNo) Line Input #lngFileNo, strTemp If InStr(strTemp, "[ODBC]") > 0 Then Exit Do End If Loop '读取数据库连接信息 Do Until EOF(lngFileNo) Line Input #lngFileNo, strTemp If InStr(strTemp, "Driver") > 0 Then '驱动程序 audtDBINFO.sDriver = Trim(Mid(strTemp, InStr(strTemp, "=") + 1)) ElseIf InStr(strTemp, "Server") > 0 Then '服务器 audtDBINFO.sServer = Trim(Mid(strTemp, InStr(strTemp, "=") + 1)) ElseIf InStr(strTemp, "Database") > 0 Then '数据库 audtDBINFO.sDatabase = Trim(Mid(strTemp, InStr(strTemp, "=") + 1)) ElseIf InStr(strTemp, "User") > 0 Then '用户 audtDBINFO.sUser = Trim(Mid(strTemp, InStr(strTemp, "=") + 1)) ElseIf InStr(strTemp, "Password") > 0 Then '密码 audtDBINFO.sPassword = Trim(Mid(strTemp, InStr(strTemp, "=") + 1)) ElseIf InStr(strTemp, "[") > 0 Then '[ODBC]顶搜索完毕、找到下一项([JZ]) Exit Do End If Loop
'---- 搜索机组代码 ----
'读取机组信息 Do Until EOF(lngFileNo) Line Input #lngFileNo, strTemp If InStr(strTemp, "JZDM") > 0 Then '机组代码 gstrJZDM = Trim(Mid(strTemp, InStr(strTemp, "=") + 1)) ElseIf InStr(strTemp, "[") > 0 Then '[JZ]顶搜索完毕、找到下一项 Exit Do End If Loop
'关闭文件 Close #lngFileNo
'成功 gfGetSysInfo = 0 Exit Function
ErrProc: gfGetSysInfo=3End FunctionPublic Function gfConnectDB() As Long '------------------ ' 连接数据库 ' 返回值: 0 - 成功 ' 1 - 出错 '------------------
On Error GoTo ErrProc
Dim udtDBINFO As DBINFO Dim strConn As String 'ADO连接串
'读取机组信息 Do Until EOF(lngFileNo) Line Input #lngFileNo, strTemp If InStr(strTemp, "JZDM") > 0 Then '机组代码 gstrJZDM = Trim(Mid(strTemp, InStr(strTemp, "=") + 1)) ElseIf InStr(strTemp, "[") > 0 Then '[JZ]顶搜索完毕、找到下一项 Exit Do End If Loop
Visual Basic 通过使用资源文件有效地把本地化信息从代码中分离出来。注意 工程中只能有一个资源文件,如果试图添加一个以上的资源文件 Visual Basic 将产生一个错误信息。把字符串保存在资源文件中的优点
编写代码时,可以调用 LoadResString、LoadResPicture 和 LoadResData 函数代替对文字、图象和数据的引用。把这些元素存储在资源文件中有两大好处: 提高性能和增加空间,这是因为字符串、位图、图标和数据可以不必在调用窗体或模块时就全部加载,而可以按需要从资源文件中加载。
把需要翻译的资源单独放在一个资源文件中,因此没有必要访问源代码或重新编译应用程序。
要创建资源文件,请按照以下步骤执行: 从“工程”菜单中选择“新资源文件”。
注意 该命令只有在资源编辑器外接程序加载的情况下才能使用。要加载资源管理器外接程序,请选择“外接程序”菜单中的“外接程序管理器”。在“外接程序管理器”对话框中,选择“VB6 资源编辑器”并复选“加载/卸载”。在“打开一个资源文件”对话框中,为资源文件输入一个名称。资源文件名称将被添加到“工程资源管理器”的“相关文档”节点。
Visual Basic 把具有 .res 扩展名的文件识别为资源文件。如果资源文件不具有正确的扩展名,Visual Basic 将不会加载它。相反地,任何具有 .res 扩展名的文件被加入到工程时都会被 Visual Basic 当作资源文件处理,如果该文件不具有标准的资源文件格式,Visual Basic 将在第一次使用资源文件支持函数(LoadResString、LoadResPicture 和 LoadResData)或试图编译成 .exe 文件时产生错误信息。试图添加 16 位的资源文件到工程中也会产生同样的错误信息。资源文件被添加到工程后,.res 文件将出现在“工程”窗口中。与窗体或模块不同,在 Visual Basic 中不能查看 .res 文件。该文件是标准的资源文件,可由 Microsoft Visual C++ 和其它大部分的 Windows 开发工具创建或使用。一旦在“文件”菜单中选中“生成 projectname.exe”一项,Visual Basic 将把该文件中的所有资源编译到 .exe 文件中作为 Windows 的资源。在编译 .exe 文件之前和之后,.res 文件都是标准的 Windows 资源文件,这意味着任何标准的 Windows 资源编辑器都可以加载该文件。要编辑资源文件,请按照以下步骤执行: 从“工具”菜单中选择“资源编辑器”。
注意 该命令仅在资源编辑器外接程序加载的情况下才能使用。要加载资源编辑器外接程序,请从“外接程序”菜单中选择“外接程序管理器”。在“外接程序管理器”对话框中,选择“VB6 资源编辑器”并复选“加载/卸载”框。从“资源编辑器工具栏”中选择一个按钮来编辑一个现存资源文件或添加一个新的。有关编辑资源文件的更详细信息,请参阅资源编辑器外接程序文档。
锁定资源文件
Visual Basic 可以对 .res 文件进行文件锁定,以防止同时有几个应用程序使用该文件。在下列条件下 .res 文件将被锁定: Visual Basic 处于运行模式或中断模式。
创建了一个 .exe 文件。
详细信息 关于如何使用资源文件开发多区域应用程序的示例,请参阅本章后面的“自动取款机应用程序示例”。 关于使用资源文件编程的背景资料,请参阅“再论编程”中的“利用资源文件进行工作”。
The GetPrivateProfileString function retrieves a string from the specified section in an initialization file. This function is provided for compatibility with 16-bit Windows-based applications. Win32-based applications should store initialization information in the registry. DWORD GetPrivateProfileString(
LPCTSTR lpAppName, // points to section name
LPCTSTR lpKeyName, // points to key name
LPCTSTR lpDefault, // points to default string
LPTSTR lpReturnedString, // points to destination buffer
DWORD nSize, // size of destination buffer
LPCTSTR lpFileName // points to initialization filename
);
Parameters
lpAppName
Pointer to a null-terminated string that specifies the section containing the key name. If this parameter is NULL, the GetPrivateProfileString function copies all section names in the file to the supplied buffer.
lpKeyName
Pointer to the null-terminated string containing the key name whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.
lpDefault
Pointer to a null-terminated default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. This parameter cannot be NULL.
Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks. Windows 95: Although lpDefault is declared as a constant parameter, the system strips any trailing blanks by inserting a null character into the lpDefault string before copying it to the lpReturnedString buffer. Windows NT: The system does not modify the lpDefault string. This means that if the default string contains trailing blanks, the lpReturnedString and lpDefault strings will not match when compared using the lstrcmp function. lpReturnedString
Pointer to the buffer that receives the retrieved string.
nSize
Specifies the size, in characters, of the buffer pointed to by the lpReturnedString parameter.
lpFileName
Pointer to a null-terminated string that names the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.
Return Values
The return value is the number of characters copied to the buffer, not including the terminating null character. If neither lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string is truncated and followed by a null character, and the return value is equal to nSize minus one. If either lpAppName or lpKeyName is NULL and the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters. In this case, the return value is equal to nSize minus two. Res
The GetPrivateProfileString function searches the specified initialization file for a key that matches the name specified by the lpKeyName parameter under the section heading specified by the lpAppName parameter. If it finds the key, the function copies the corresponding string to the buffer. If the key does not exist, the function copies the default character string specified by the lpDefault parameter. A section in the initialization file must have the following form: [section]
key=string
.
.
.If lpAppName is NULL, GetPrivateProfileString copies all section names in the specified file to the supplied buffer. If lpKeyName is NULL, the function copies all key names in the specified section to the supplied buffer. An application can use this method to enumerate all of the sections and keys in a file. In either case, each string is followed by a null character and the final string is followed by a second null character. If the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two null characters. If the string associated with lpKeyName is enclosed in single or double quotation s, the s are discarded when the GetPrivateProfileString function retrieves the string. The GetPrivateProfileString function is not case-sensitive; the strings can be a combination of uppercase and lowercase letters. To retrieve a string from the WIN.INI file, use the GetProfileString function. Windows NT: Calls to private profile functions may be mapped to the registry instead of to the specified initialization files. This mapping occurs when the initialization file and section are specified in the registry under the following keys: HKEY_LOCAL_MACHINE\Software\Microsoft\
Windows NT\CurrentVersion\IniFileMappingThis mapping is likely if an application modifies system-component initialization files, such as CONTROL.INI, SYSTEM.INI, and WINFILE.INI. In these cases, the GetPrivateProfileString function retrieves information from the registry, not from the initialization file; the change in the storage location has no effect on the function's behavior. The Win32 profile functions (Get/WriteProfile*, Get/WritePrivateProfile*) use the following steps to locate initialization information: Look in the registry for the name of the initialization file, say MYFILE.INI, under IniFileMapping:
HKEY_LOCAL_MACHINE\Software\Microsoft\
Windows NT\CurrentVersion\IniFileMapping\myfile.ini Look for the section name specified by lpAppName. This will be a named value under myfile.ini, or a subkey of myfile.ini, or will not exist.
If the section name specified by lpAppName is a named value under myfile.ini, then that value specifies where in the registry you will find the keys for the section.
If the section name specified by lpAppName is a subkey of myfile.ini, then named values under that subkey specify where in the registry you will find the keys for the section. If the key you are looking for does not exist as a named value, then there will be an unnamed value (shown as <No Name>) that specifies the default location in the registry where you will find the key.
If the section name specified by lpAppName does not exist as a named value or as a subkey under myfile.ini, then there will be an unnamed value (shown as <No Name>) under myfile.ini that specifies the default location in the registry where you will find the keys for the section.
If there is no subkey for MYFILE.INI, or if there is no entry for the section name, then look for the actual MYFILE.INI on the disk and read its contents.
When looking at values in the registry that specify other registry locations, there are several prefixes that change the behavior of the .INI file mapping: ! - this character forces all writes to go both to the registry and to the .INI file on disk.
# - this character causes the registry value to be set to the value in the Windows 3.1 .INI file when a new user logs in for the first time after setup.
@ - this character prevents any reads from going to the .INI file on disk if the requested data is not found in the registry.
USR: - this prefix stands for HKEY_CURRENT_USER, and the text after the prefix is relative to that key.
SYS: - this prefix stands for HKEY_LOCAL_MACHINE\SOFTWARE, and the text after the prefix is relative to that key.
Public Type DBINFO
sDriver As String
sServer As String
sDatabase As String
sUser As String
sPassword As String
End TypePublic Function gfGetSysInfo(audtDBINFO As DBINFO) As Long '----------------------------------------------------------
' 从系统配置文件中读取系统信息(数据库连接信息、机组号)
' 参数: audtDBINFO - 信息读出后要保存的变量
' 返回值: 0 - 成功
' 1 - 失败
' 失败后要置系统状态标志 2 - 配置文件不存在
' 3 - 配置文件打开失败
'----------------------------------------------------------
Dim strFileName As String '配置文件名
Dim lngFileNo As Long '配置文件号
Dim strTemp As String
'取得配置文件名
strFileName = App.EXEName
strFileName = App.Path & "\" & strFileName & ".ini"
'若配置文件不存在,退出
If Dir(strFileName) = "" Then
'函数返回值
gfGetSysInfo = 1
Exit Function
End If
'得到空闲文件号
lngFileNo = FreeFile()
'打开文件
On Error GoTo ErrProc
Open strFileName For Input As #lngFileNo
'---- 搜索数据库连接信息 ----
'查找[ODBC]项
Do Until EOF(lngFileNo)
Line Input #lngFileNo, strTemp
If InStr(strTemp, "[ODBC]") > 0 Then
Exit Do
End If
Loop
'读取数据库连接信息
Do Until EOF(lngFileNo)
Line Input #lngFileNo, strTemp
If InStr(strTemp, "Driver") > 0 Then
'驱动程序
audtDBINFO.sDriver = Trim(Mid(strTemp, InStr(strTemp, "=") + 1))
ElseIf InStr(strTemp, "Server") > 0 Then
'服务器
audtDBINFO.sServer = Trim(Mid(strTemp, InStr(strTemp, "=") + 1))
ElseIf InStr(strTemp, "Database") > 0 Then
'数据库
audtDBINFO.sDatabase = Trim(Mid(strTemp, InStr(strTemp, "=") + 1))
ElseIf InStr(strTemp, "User") > 0 Then
'用户
audtDBINFO.sUser = Trim(Mid(strTemp, InStr(strTemp, "=") + 1))
ElseIf InStr(strTemp, "Password") > 0 Then
'密码
audtDBINFO.sPassword = Trim(Mid(strTemp, InStr(strTemp, "=") + 1))
ElseIf InStr(strTemp, "[") > 0 Then
'[ODBC]顶搜索完毕、找到下一项([JZ])
Exit Do
End If
Loop
'---- 搜索机组代码 ----
'读取机组信息
Do Until EOF(lngFileNo)
Line Input #lngFileNo, strTemp
If InStr(strTemp, "JZDM") > 0 Then
'机组代码
gstrJZDM = Trim(Mid(strTemp, InStr(strTemp, "=") + 1))
ElseIf InStr(strTemp, "[") > 0 Then
'[JZ]顶搜索完毕、找到下一项
Exit Do
End If
Loop
'关闭文件
Close #lngFileNo
'成功
gfGetSysInfo = 0
Exit Function
ErrProc:
gfGetSysInfo=3End FunctionPublic Function gfConnectDB() As Long '------------------
' 连接数据库
' 返回值: 0 - 成功
' 1 - 出错
'------------------
On Error GoTo ErrProc
Dim udtDBINFO As DBINFO
Dim strConn As String 'ADO连接串
'从配置文件中读出系统配置信息(数据库连接信息、机组号)(--modSysInfo--)
Call gfGetSysInfo(udtDBINFO)
'生成ADO连接串
With udtDBINFO
strConn = strConn & "Provider=MSDASQL.1;"
strConn = strConn & "DRIVER={" & .sDriver & "};"
strConn = strConn & "SERVER=" & .sServer & ";"
strConn = strConn & "SRVR=" & .sServer & ";"
strConn = strConn & "DATABASE=" & .sDatabase & ";"
strConn = strConn & "UID=" & .sUser & ";"
strConn = strConn & "PWD=" & .sPassword
End With
'连接数据库
gcnSCSS.CursorLocation = adUseClient
gcnSCSS.Open strConn
gfConnectDB = 0
Exit Function
ErrProc:
'函数返回值
gfConnectDB = 1
End Function
没有看懂。不胜感激。什么叫机组代码?读取机组信息有什么用?
strFileName = App.EXEName
lngFileNo = FreeFile()
Line Input #lngFileNo, strTemp
'---- 搜索机组代码 ----
'读取机组信息
Do Until EOF(lngFileNo)
Line Input #lngFileNo, strTemp
If InStr(strTemp, "JZDM") > 0 Then
'机组代码
gstrJZDM = Trim(Mid(strTemp, InStr(strTemp, "=") + 1))
ElseIf InStr(strTemp, "[") > 0 Then
'[JZ]顶搜索完毕、找到下一项
Exit Do
End If
Loop
'关闭文件
Close #lngFileNo
'成功
gfGetSysInfo = 0
Exit Function
lngFileNo = FreeFile()
Line Input #lngFileNo, strTemp
可不可以麻烦你,解释解释这三句