客户端ini文件为什么连不上sql服务器?高手提路
网上找的代码,用来避免连服务器的代码修改,但不知怎么搞的,报getConnString函数无效,连不上数据库,本人严格按照下述方法做的,为什么不行啊?是不是代码有错?郁闷啊,盼高手指点.
文件名:DB.ini
内容:
[DB]
Server=服务器名或者ip
DataBase=数据库名
LogId=数据库用户名
LogPassword=密码
--------------------
'模块代码:
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
'自定义读取INI函數
Private Function getIni(ByVal pfileName As String, ByVal psection As String, ByVal pkey As String) As String
Dim x As Long, Buff As String * 128, i%
x = GetPrivateProfileString(psection, pkey, "", Buff, 128, pfileName)
i = InStr(Buff, Chr(0))
getIni = Trim(Left(Buff, i - 1))
End Function
Public Sub getConnString()
On Error Resume Next
Dim dataBase As String * 255
Dim serverName As String * 255
Dim userID As String * 255
Dim PWD As String * 255
Dim filePath As String
filePaht=App.Path & "\DB.ini"
userID = getIni(filePath, "DB", "LogID")
PWD = getIni(filePath, "DB", "LogPassword")
dataBase = getIni(filePath, "DB", "DataBase")
ServerName= getIni(filePath, "DB", "Server")
getConnString= "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=" & Trim(userID) & ";Password=" & Trim(PWD) & ";Initial Catalog=" & Trim(DataBase) & ";Data Source=" & Trim(serverName)
End Sub
'窗体代码(需要引用ADO):
Private Sub Command1_Click()
Dim cn As New ADODB.Connection
cn.ConnectionString=getConnString
cn.Open
......
End Sub