如何以编程方式从Access数据库中删除已知密码? [英] How do I programmatically remove a known password from an Access DB?

查看:72
本文介绍了如何以编程方式从Access数据库中删除已知密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我无法控制的原因,我必须处理一个新的Access MDB文件,该文件每个月通过我编写的自动化过程进行下载,解密和解压缩.尽管使用了PGP加密,但是发件人(保险公司)拒绝发送不受密码保护的MDB.

For reasons beyond my control, I have to deal with a new Access MDB file that is downloaded, decrypted, and unzipped every month by an automated process I wrote. Despite the PGP encryption, the sender (an insurance company) refuses to send the MDB non-password-protected.

不幸的是,下载文件后立即对其进行了处理,并假定没有密码,因此由于OleDbException表明我们输入了错误的密码,因此未对这些文件进行处理.我们知道密码,并且知道连接字符串的使用数据库密码"选项:

Unfortunately, immediately after the file is downloaded, it's processed, and is assumed to have no password, so these files aren't being processed due to an OleDbException showing that we have the wrong password. We know the password, and we know about the "with database password" option for connection strings:

Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\mydatabase.mdb;Jet OLEDB:Database Password=MyDbPassword;

那只能解决部分问题,因为另一个部门以后需要访问文件,而且他们不知道密码.到目前为止,我只能通过在打开文件时按住Shift才能使其正常工作,在密码提示下取消,通过打开的Access过程再次打开文件,同时再次按住Shift并单击打开独占",然后继续在浏览密码对话框时按住Shift,然后通过安全工具取消设置密码.

That only solves part of the problem, since another department needs to access the files later, and they don't know the password. So far, I've only been able to get it to work by holding Shift while I open the file, cancel at the password prompt, open the file again through an open Access process while holding Shift again and clicking "Open Exclusive", continuing to hold Shift while going through the password dialog, then unsetting the password through the security tools.

我想做的只是使用C#以编程方式在MDB文件下载后立即取消设置DB密码.有没有办法做到这一点,还是每次我们得到一个新文件时都要亲自干预,完全违背了自动化的目的?

What I'd like to do is just programmatically unset the DB password on the MDB file as soon as it's downloaded, using C#. Is there a way to do that, or do I have to personally intervene every time we get a new file, completely defeating the purpose of automation?

推荐答案

以编程方式更改密码的方法详细

The way to change the password programmatically is detailed here.

本质上,需要执行以下操作:

Essentially, one needs to do the following:

  1. 使用ADO.NET打开与数据库的连接
  2. 在数据库上执行一条alter语句,将其密码设置为NULL:

  1. Open a connection to the database using ADO.NET
  2. Execute an alter statement on the database setting it's password to NULL as so:

ALTER DATABASE PASSWORD [您的密码] NULL;

ALTER DATABASE PASSWORD [Your Password] NULL;

  • 释放连接

  • Dispose the connection

    取自源代码的示例代码:

    Sample code taken from the source:

    Private Function CreateDBPassword(ByVal Password As String, _
        ByVal Path As String) As Boolean
    Dim objConn as ADODB.Connection
    Dim strAlterPassword as String
    On Error GoTo CreateDBPassword_Err
    ' Create the SQL string to initialize a database password.
    strAlterPassword = "ALTER DATABASE PASSWORD [Your Password] NULL;"
    
    ' Open the unsecured database.
    Set objConn = New ADODB.Connection
    With objConn
        .Mode = adModeShareExclusive
        .Open "Provider=Microsoft.Jet.OLEDB.4.0;Data " & _
            "Source=[Your Path];" 
    
     ' Execute the SQL statement to secure the database.
     .Execute (strAlterPassword)
    End With
    
    ' Clean up objects.
    objConn.Close
    Set objConn = Nothing
    
    ' Return true if successful.
    CreateDBPassword = True
    
    CreateDBPassword_Err:
    Msgbox Err.Number & ":" & Err.Description
    CreateDBPassword = False 
    End Function
    

    这篇关于如何以编程方式从Access数据库中删除已知密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆