程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> Access數據庫 >> 關於Access數據庫 >> 程序: 創建、壓縮Access數據庫並修改密碼演示

程序: 創建、壓縮Access數據庫並修改密碼演示

編輯:關於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
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved