网上找的代码,用来避免连服务器的代码修改,但不知怎么搞的,报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
文件名: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
解决方案 »
- 菜鸟跪求怎样设置一个动态权限的分配,如果有个代码例子更好~~
- 希望陈辉当API版主,随便问个简单的问题
- 我想问一下现在学VB前途如何?
- VB链接局域网数据库时用SQL,服务器必须安装SQL Server马?
- 突然想不起怎么让一个窗口在最上面了,谁知道
- 高手谁知道这个透明效果是怎么做出来的呀!!!,谢谢
- 关于窗体与窗体的关系
- 100分,问题很简单........
- 多少钱你才满意
- 怎样判断文本框的值的长度,用Len()函数为中文汉字时是作为一个字节处理,而不是两个字节但数据库作为两个字节处理,会出错!用什么API函数判断长度?
- MSFlexGrid控件如何连接ADO对象,详细步骤,非常感谢
- 不同窗体能否共用OnComm?
cn.ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID="& Trim(userID)&";Password="& Trim(PWD) &";Initial Catalog=" & Trim(DataBase) & ";Data Source="& Trim(serverName)&""
cn.Open
.....
OR:cn.ConnectionString="Driver={SQL Server};Server=" & ServerName & ";Database=DataBase;Trusted_Connection=yes"
cn.Open
那我怎么调用INI里的文件啊
肯定要用过程调用啊
......
end function
......
end function
问题经过他的调试解决 代码 没有问题 filePaht=App.Path & "\DB.ini" 改成绝对路径就可以了