* ——————————————–
* 程序: 创建、压缩access数据库并修改密码演示
* 设计: 红雨
* ——————————————–
local lcmdbfile, lcretustr
lcmdbfile = [c:\temp\testcreamdbfile.mdb]
lcpswd1 = [test1]
lcpswd2 = [test2]
lcpswd3 = [test3]
lcretustr = [创建、压缩access数据库并修改密码演示:] + chr(13)
if createmdb( lcmdbfile, lcpswd1)
lcretustr = lcretustr + chr(13) + [1、创建数据库成功 – 密码: ] + lcpswd1
if compactmdb( lcmdbfile, lcpswd1, lcpswd2 )
lcretustr = lcretustr + chr(13) + [2、压缩并修改密码成功 – 密码: ] + lcpswd2
if changemdbpassword( lcmdbfile, lcpswd2, lcpswd3 )
lcretustr = lcretustr + chr(13) + [3、单独修改数据库密码成功 – 密码: ] + lcpswd3
else
lcretustr = lcretustr + chr(13) + [3、单独修改数据库密码失败]
endif
else
lcretustr = lcretustr + chr(13) + [2、压缩并修改密码失败]
endif
else
lcretustr = lcretustr + chr(13) + [1、创建数据库失败]
endif
= messagebox( lcretustr, 0+64+0, [红雨提示] )
return
* ——————————————–
function createmdb( tcmdbfile, tcpswdstr )
* 创建 access 数据库文件(.mdb)
local isok
isok = .f.
tcmdbfile = iif(type([tcmdbfile])=[c], tcmdbfile, [])
tcpswdstr = iif(type([tcpswdstr])=[c], tcpswdstr, [])
if file(tcmdbfile)
erase (tcmdbfile)
endif
if !file(tcmdbfile)
isok = .t.
local loengine, lcolderror, lccmdstrs
lcolderror = on([error])
on error isok = .f.
locatalog = createobject( [adox.catalog] )
lccmdstrs = [provider=microsoft.jet.oledb.4.0] ;
+ [;data source=] + tcmdbfile ;
+ [;jet oledb:database password=] + tcpswdstr
locatalog.create( lccmdstrs )
release locatalog
locatalog = null
on error &lcolderror.
endif
return isok and file(tcmdbfile)
endfunc
* ——————————————–
function compactmdb ( tcmdbfile, tcoldpswd, tcnewpswd )
* 压缩 access 数据库并设置密码
local isok
isok = .f.
tcmdbfile = iif(type([tcmdbfile])=[c], tcmdbfile, [])
tcoldpswd = iif(type([tcoldpswd])=[c], tcoldpswd, [])
tcnewpswd = iif(type([tcnewpswd])=[c], tcnewpswd, tcoldpswd)
if file( tcmdbfile )
isok = .t.
local loengine, lctmpfile, lcolderror, lccompoldstr, lccompnewstr
lcolderror = on([error])
on error isok = .f.
lctmpfile = addb(justpath(tcmdbfile)) + subs(sys(2015),3) + [.mdb]
rename (tcmdbfile) to (lctmpfile)
if !file(tcmdbfile) and file(lctmpfile)
lccompoldstr = [provider=microsoft.jet.oledb.4.0] ;
+ [;data source=] + lctmpfile ;
+ [;jet oledb:database password=] + tcoldpswd
lccompnewstr = [provider=microsoft.jet.oledb.4.0] ;
+ [;data source=] + tcmdbfile ;
+ [;jet oledb:database password=] + tcnewpswd
loengine = createobject( [jro.jetengine] )
loengine.compactdatabase( lccompoldstr, lccompnewstr )
release loengine
loengine = null
if file(tcmdbfile)
erase (lctmpfile)
else
rename (lctmpfile) to (tcmdbfile)
endif
else
isok = .f.
endif
on error &lcolderror.
endif
return isok and file(tcmdbfile)
endfunc
* ——————————————–
function changemdbpassword ( tcmdbfile, tcoldpswd, tcnewpswd )
* 修改 access 数据库的密码,必须独占打开数据库,使用前请确保没有其他程序使用数据库
local isok
isok = .f.
lcretustr = []
tcmdbfile = iif(type([tcmdbfile])=[c], tcmdbfile, [])
tcoldpswd = iif(type([tcoldpswd])=[c], tcoldpswd, [])
tcnewpswd = iif(type([tcnewpswd])=[c], tcnewpswd, [])
if file( tcmdbfile )
isok = .t.
local loadodb, lcolderror
lcolderror = on([error])
on error isok = .f.
loadodb = createobject( [adodb.connection] )
loadodb.mode = 12
loadodb.provider = [microsoft.jet.oledb.4.0]
loadodb.properties([jet oledb:database password]) = tcoldpswd
loadodb.open([data source=] + tcmdbfile)
loadodb.execute(alter database password [ + tcnewpswd + ][ + tcoldpswd + ])
loadodb.close
release loadodb
loadodb = null
on error &lcolderror.
endif
return isok
endfunc
* ——————————————–