如何建立一个写有数据库连接语句的配置文件,然后在程序里调用?
dim Connectstring as string
If Dir(App.Path & "\Login.cfg") <> "" Then
Open App.Path & "\login.cfg" For Input As #1
Line Input #1, Connectstring
Close #1
Else
MsgBox "系统配置文件login.cfg丢失,请检查!", vbInformation, "提示"
Exit Sub End If
dim cn as new adodb.connection
cn.Open Connectstring '文件中
"driver={sql server};server=127.0.0.1;uid=sa;pwd= " & _
";database=pubs "
我不知道怎么建立一个这样的文件?
dim Connectstring as string
If Dir(App.Path & "\Login.cfg") <> "" Then
Open App.Path & "\login.cfg" For Input As #1
Line Input #1, Connectstring
Close #1
Else
MsgBox "系统配置文件login.cfg丢失,请检查!", vbInformation, "提示"
Exit Sub End If
dim cn as new adodb.connection
cn.Open Connectstring '文件中
"driver={sql server};server=127.0.0.1;uid=sa;pwd= " & _
";database=pubs "
我不知道怎么建立一个这样的文件?
Public Function WriteFile(strWhat As String, strFilePath As String, strFileName As String) As Boolean
WriteFile = False
Dim fso As New FileSystemObject
Dim fFile As File
Dim ts As TextStream
On Error GoTo ErrorHandl
Dim txtFile As String
Dim intFlag As Integer
txtFile = strFilePath & strFileName
intFlag = 8
If fso.FileExists(txtFile) = False Then
fso.CreateTextFile txtFile, True
intFlag = 2
End If
Set fFile = fso.GetFile(txtFile)
Set ts = fFile.OpenAsTextStream(intFlag, -2)
ts.WriteLine (strWhat)
ts.Close
WriteFile = True
Exit Function
ErrorHandl:
End Function这是个写文件的涵数,参考下,修改下就可以用了,好象还要引用个东东
If Dir(App.Path & "\Login.udl") <> "" Then
Connectstring="File Name=" & App.Path & "\Login.udl"
Else
MsgBox "系统配置文件login.cfg丢失,请检查!", vbInformation, "提示"
Exit Sub
End Ifdim cn as new adodb.connection
cn.Open Connectstring 在你的工程代码目录下,创建一个新的文本文件(什么也别编辑),直接更名为 Login.udl 保存。双击这个文件,会弹出一个连接向导窗口,设置它,测试连接并确定。将来在打包时,将它打进去就可以了。在安装后,在目标机上调试也方便,双击后重新设置就可以了。