Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查mdb文件是否有密码?_C#_Ms Access_Passwords - Fatal编程技术网

C# 如何检查mdb文件是否有密码?

C# 如何检查mdb文件是否有密码?,c#,ms-access,passwords,C#,Ms Access,Passwords,我使用此方法为.mdb文件设置密码: private static bool SetPassword(string filePath, string newPassword) { try { if (File.Exists(filePath)) { string OledbConnectionString = string.Format("Provider=Micros

我使用此方法为.mdb文件设置密码:

    private static bool SetPassword(string filePath, string newPassword)
    {
        try
        {
            if (File.Exists(filePath))
            {
                string OledbConnectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Mode=12;", filePath);

                using (OleDbConnection con = new OleDbConnection(OledbConnectionString))
                {
                    string sql = string.Empty;
                    sql = string.Format("ALTER DATABASE PASSWORD [{0}]", newPassword);
                    OleDbCommand cmd = new OleDbCommand(sql, con);

                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            CommonLogUtility.GetLogger(LoggerType.Other).Error(ex.ToString());
            return false;
        }
    }
但是我想检查.mdb文件是否已经有密码保护,这样我就不必每次运行应用程序时都设置密码。另外,当我已经将密码从null设置为字符串时,下次运行应用程序时,它将在方法SetPassword中引发异常。
因此,每次运行SetPassword方法时,我都想检查.mdb文件是否有密码,我如何才能做到这一点?

我认为,如果您尝试访问受密码保护的数据库,而不在连接字符串中提供用户/密码,它将抛出异常。因此,您可以尝试在没有用户/密码的情况下连接到数据库,如果连接失败,请尝试使用用户/密码连接,而如果可以在没有用户/密码的情况下连接,则只需设置密码。或者,当.mdb文件不受密码保护时,我可以调用此方法,如果该文件受密码保护,我将不调用此方法。是,无论哪种方法都可以。如何检查文件是否受密码保护正是我所困惑的问题。如何避免抛出异常。