jjccx

jjccx's blog
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

修改Access数据库密码

Posted on 2005-01-24 02:59  jjccx  阅读(684)  评论(1)    收藏  举报

public void CompactAccessDB(string connectionString, string newFile, string password)

{

     string tempName = System.IO.Path.Combine(

         Environment.GetFolderPath(Environment.SpecialFolder.Templates),

         @"~tempdb.mdb");

 

     string tempDestConnection = string.Format(

         @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};" +

         @"Jet OLEDB:Encrypt Database=true;Jet OLEDB:Database Password={1}",

         tempName,

         password

         );

 

     object[] myParams;

 

     object jro = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));

 

     myParams = new object[]{

                                     connectionString,

                                     tempDestConnection

                                 };

 

     jro.GetType().InvokeMember("CompactDatabase",

         System.Reflection.BindingFlags.InvokeMethod,

         null,

         jro,

         myParams);

 

     System.IO.File.Delete(newFile);

 

     System.IO.File.Move(tempName, newFile);

 

     System.Runtime.InteropServices.Marshal.ReleaseComObject(jro);

 

     jro = null;

}