SQL 数据库连接INI 文件
我在读取,连接数据库时,制作了SETUP.INI文件,在模块中加入如下代码,publicstr
'声明写入ini文件的API函数
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpString As Any, ByVal lpFilenchame As String) As Long
Public Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFilenchame As String) As Long
Dim str As String
Public Sub main()
Dim ServerName As String
Dim UserName As String
Dim PasswordName As String
'从Setup.ini中读取服务器的名字------------------------------------------------------------------
ServerName = GetKey(App.Path + "\Setup.ini", "Server Name")
UserName = GetKey(App.Path + "\Setup.ini", "User")
PasswordName = GetKey(App.Path + "\Setup.ini", "Password")
'如果读取不成功,退出------------------------------------------------------------------
If ServerName = "" Then
MsgBox "Setup.ini格式不正确,请重新设置"
End
End If
Server = ServerName
User = UserName
Password = PasswordName
publicstr = "driver={SQL Server};server=" + Trim(Server) + ";uid=" + Trim(User) + ";pwd=" + Trim(Password) + ";database=LF_DB"
frm_login.Show
End Sub
Public Function GetKey(Tmp_File As String, Tmp_Key As String) As String
Dim File As Long
'分配文件句柄
File = FreeFile
'如果文件不存在则创建一个默认的Setup.ini文件
If FileExist(Tmp_File) = False Then
GetKey = ""
Call WritePrivateProfileString("Setup Information", "Server Name ", " NtServer", App.Path + "\Setup.ini")
Call WritePrivateProfileString("Setup Information", "UserName ", " ", App.Path + "\Setup.ini")
Call WritePrivateProfileString("Setup Information", "PasswordName ", " ", App.Path + "\Setup.ini")
Exit Function
End If
'读取数据项值
Open Tmp_File For Input As File
Do While Not EOF(1)
Line Input #File, buffer
If Left(buffer, Len(Tmp_Key)) = Tmp_Key Then
pos = InStr(buffer, "=")
GetKey = Trim(Mid(buffer, pos + 1))
End If
Loop
Close File
End Function
'判断文件是否存在
Function FileExist(Fname As String) As Boolean
On Local Error Resume Next
FileExist = (Dir(Fname) <> "")
End Function
Public Function cnn() As ADODB.Connection
Set cnn = New ADODB.Connection
cnn.Open "driver={SQL Server};server=" + Trim(Server) + ";uid=" + Trim(User) + ";pwd=" + Trim(Password) + ";database=LF_DB"End Function
ini cnn --------------------编程问答-------------------- Public 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
用这个API函数读Ini文件,如:
dim server as string
server = String(255, " ")
GetPrivateProfileString "Setup Information", "Server Name", "", server, 255, App.Path + "\Setup.ini"
server = Replace(Trim(server), Chr(0), "") --------------------编程问答-------------------- 在模块中加入如下代码,publicstr 连接字符,可以正常使用,可是,CNN 却不能正常使用,读取,连接,插入的都是本地数据库的数据,不知道哪里设置错了,望提供帮助,谢谢 --------------------编程问答-------------------- 检查 Server user password这几个参数是否正确,除了看值还要看长度,这几个参数里面可能包含不可打印的字符,比如我上个回复里的chr(0),我没见到你有对这些特殊字符的处理。
打开SQL server查询分析器,用这三个参数登录试试,验证一下这三个参数是否正确。
最好是吧错误描述贴出来。
补充:VB , API