dim strconn as string strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =" & App.Path & "\aa.mdb" ....... 你能否把这个写完全。
INI文件保存在c:\test.ini [DB] path=程序: public Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" _ (ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, _ ByVal lpRetunedString As String, ByVal nSize As Long, _ ByVal lpFileName As String) As Long public Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" _ (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, _ ByVal lplFileName As String) As LongPublic Function GetSetting(AppName As String, KeyName As String, FileName As String) As String '功能:读取INI文件 Dim RetStr As String RetStr = String(255, Chr(0)) GetSetting = Left(RetStr, GetPrivateProfileString(AppName, ByVal KeyName, "", _ RetStr, Len(RetStr), FileName)) End Functionprivate sub Form1_Load() dim strPath as string
strPath=GetSetting("DB", "path", "c:\test.ini") if strPath="" then 指定路径的窗体.show else 连接strPath下的数据库 end if end sub指定路径的窗体代码: private sub CommandOK_Click() WritePrivateProfileString "DB", "Path", Text1.Text, "c:\test.ini" end sub
上面代码有一点问题,更改一下: private sub Form1_Load() dim strPath as string
strPath=GetSetting("DB", "path", "c:\test.ini") if strPath="" then 指定路径的窗体.show vbmodal ----这里弹出模式窗体 strPath=指定的路径 end if 连接strPath下的数据库 end sub指定路径的窗体代码: private sub CommandOK_Click() WritePrivateProfileString "DB", "Path", Text1.Text, "c:\test.ini" Unload Me end sub
对不起,我写错了,是这样的; dim strconn as string dim conn as new adodb.connection dim rs as new adodb.recordet strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =c:\aa.mdb" if conn.state=0 then conn.open strconn if rs.state=1 then rs.close 现在如果要变路径 一是写入注册表; 二是用.ini文件; 三是用app.path; 哪现在如果用app.path应该怎样写呢?app.path好象也是一个变量。 如果写成这样会出错: strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =" & app.path &"\aa.mdb" conn.open strconn
SaveSetting和GetSetting就可以进行读写注册表.
写一个界面用来设置这个值就可以了.
mvarServerName = GetSetting("KX", "DatabaseUtilities", "Server", "")
这两个函数的用法.
前面的代码你写,到这里时应该能选到一个路径存在一个字符串里.
SaveSetting("yourname","Database","Path",strpath)
完成了保存.
可动态修改,以后你想改时也用上面的方法.
........
使用时:
strpath=GetSetting("yourname","Database","Path","")
这就得到了以前你保存的路径了.
存在这个位置.
strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =" & App.Path & "\aa.mdb"
.......
你能否把这个写完全。
[DB]
path=程序:
public Declare Function GetPrivateProfileString Lib "Kernel32" Alias "GetPrivateProfileStringA" _
(ByVal lpApplicationName As String, lpKeyName As Any, ByVal lpDefault As String, _
ByVal lpRetunedString As String, ByVal nSize As Long, _
ByVal lpFileName As String) As Long
public Declare Function WritePrivateProfileString Lib "Kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, _
ByVal lplFileName As String) As LongPublic Function GetSetting(AppName As String, KeyName As String, FileName As String) As String
'功能:读取INI文件
Dim RetStr As String
RetStr = String(255, Chr(0))
GetSetting = Left(RetStr, GetPrivateProfileString(AppName, ByVal KeyName, "", _
RetStr, Len(RetStr), FileName))
End Functionprivate sub Form1_Load()
dim strPath as string
strPath=GetSetting("DB", "path", "c:\test.ini")
if strPath="" then
指定路径的窗体.show
else
连接strPath下的数据库
end if
end sub指定路径的窗体代码:
private sub CommandOK_Click()
WritePrivateProfileString "DB", "Path", Text1.Text, "c:\test.ini"
end sub
private sub Form1_Load()
dim strPath as string
strPath=GetSetting("DB", "path", "c:\test.ini")
if strPath="" then
指定路径的窗体.show vbmodal ----这里弹出模式窗体
strPath=指定的路径
end if
连接strPath下的数据库
end sub指定路径的窗体代码:
private sub CommandOK_Click()
WritePrivateProfileString "DB", "Path", Text1.Text, "c:\test.ini"
Unload Me
end sub
数据库放在当前目录就不用写注册表
将"c:\test.ini"改成app.path & "\test.ini"就可以了
那你的方法不是还要动态找test.ini吗?还不如写到注册表里.
internetibm():
你都用APP.path了就是说库在当前程序所在路径了,那还要别的方法存起这个路径干吗?
不用动态找,因为这个INI文件是你自己建立的,只是打包的时候要把它也打进去,安装的时候安装到程序所在的目录就可以了。前面的声明部分最好放在一个模块里,因为有两个窗体要调用。如果你放在一个窗体单元文件里,你要把public改为private
写入注册表与用app.path哪一个好一点。
如果不在一块那就存到注册表里用.
dim strconn as string
dim conn as new adodb.connection
dim rs as new adodb.recordet
strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =c:\aa.mdb"
if conn.state=0 then conn.open strconn
if rs.state=1 then rs.close
现在如果要变路径
一是写入注册表;
二是用.ini文件;
三是用app.path;
哪现在如果用app.path应该怎样写呢?app.path好象也是一个变量。
如果写成这样会出错:
strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =" & app.path &"\aa.mdb"
conn.open strconn
strconn="Provider=Microsoft.Jet.OLEDB.3.51;Persist Security Info=False;Data Source =" & strPath & "\aa.mdb"