vb.net 读取ini文件在vb6中能读取,升迁到vb.net2008后读取不到ini文件
vb.net 读取ini文件在vb6中能读取,升迁到vb.net2008后读取不到ini文件
Option Strict Off
Option Explicit On
Module ModConString
Private strIPAddress As String
Private iniPath As String
Function GetFromINI(ByRef AppName As String, ByRef KeyName As String, ByRef FileName As String) As String
Dim RetStr As String
RetStr = New String(Chr(0), 255)
GetFromINI="driver=SQL Server;server=.;uid=sa;pwd=102030;database=RYGL_DATA;" 'Left(RetStr, GetPrivateProfileString(AppName, KeyName, "", RetStr, Len(RetStr), FileName))
End Function
'定义一个属性过程反映连接字符串
Public ReadOnly Property conString() As Object
Get
iniPath = My.Application.Info.DirectoryPath & "\RYGL.ini"
strIPAddress = GetFromINI("ConnectionString", "conString", iniPath)
'UPGRADE_WARNING: 未能解析对象 conString 的默认属性。 单击以获得更多信息:“ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
conString = "" & strIPAddress & ""
End Get
End Property
'定义一个提供者反映数据库类型
Public ReadOnly Property conProvide() As Object
Get
'UPGRADE_WARNING: 未能解析对象 conProvide 的默认属性。 单击以获得更多信息:“ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"”
conProvide = "sqloledb"
End Get
End Property
End Module
rygl.ini 如下:
[ConnectionString]
conString="driver=SQL Server;server=192.168.1.200;uid=sa;pwd=102030;database=RYGL_DATA;"
问题:
GetFromINI=是指本地数据库能连接上,如换成远程数据库就连不上。
但在GetFromINI=中换成远程数据库就能连上
说明没有能够读取ini文件
如何解决,请高手不吝指教。
[解决办法]
你这样转换,GetFromINI过程肯定不能返回你需要读取的String
首先,你的GetPrivateProfileString在VB6下的声明转换正确么?应当象下面这样,你可以复制粘贴
Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Int32, ByVal lpFileName As String) As Int32
Function GetFromINI(ByVal AppName As String, ByVal KeyName As String, ByVal FileName As String) As String
Dim RetStr As String
Dim n As Int32
RetStr = Space(1024)
'GetFromINI = "driver=SQL Server;server=.;uid=sa;pwd=102030;database=RYGL_DATA;"
n = GetPrivateProfileString(AppName, KeyName, "", RetStr, RetStr.Length, FileName)
If n > 0 Then
Return RetStr.Substring(0, n)
Else
Return ""
End If
End Function