晓艳's profile晓艳的共享空间PhotosBlogNetwork Tools Help

Blog


    November 05

    动态创建Access数据库和压缩Access数据库

    在创建Access数据库和压缩数据库之前必须填加以下动态连接库msADOX.dll、msjro.dll且它们都是在系统盘的system/ado文件价下,然后必须引用以下命名空间
    using ADOX; //该命名空间包含创建ACCESS的类(方法)using JRO; //该命名空间包含压缩ACCESS的类(方法)
    创建Access数据库代码如下:
    //DBPath是属于数据库的物理路径;
     public static void CreateAccess(string DBPath)
            
    {
                
    if (File.Exists(DBPath))//检查数据库是否已存在
                {
                    
    throw new Exception("目标数据库已存在,无法创建");
                }
               
                DBPath 
    = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+DBPath;
                
    //创建一个CatalogClass对象实例
                ADOX.CatalogClass cat = new ADOX.CatalogClass();
                
    //使用CatalogClass对象的Create方法创建ACCESS数据库
                cat.Create(DBPath);

            }

    压缩数据库代码如下:

    public static void Compact()
        
    {
            
    string mdbPath =""//添写数据库的物理路径
            string temp2 = "";
            
    string mdbPath2 = "";
            
    if (!File.Exists(mdbPath)) //检查数据库是否已存在
            {
                
    throw new Exception("目标数据库不存在,无法压缩");
            }

            
    //声明临时数据库的名称
            string temp = DateTime.Now.Year.ToString();
            temp 
    += DateTime.Now.Month.ToString();
            temp 
    += DateTime.Now.Day.ToString();
            temp 
    += DateTime.Now.Hour.ToString();
            temp 
    += DateTime.Now.Minute.ToString();
            temp 
    += DateTime.Now.Second.ToString() + ".bak";
            temp 
    = mdbPath.Substring(0, mdbPath.LastIndexOf("\\"+ 1+ temp;
            
    //定义临时数据库的连接字符串
            temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp;
            
    //定义目标数据库的连接字符串
            mdbPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbPath;
            
    //创建一个JetEngineClass对象的实例
            JRO.JetEngineClass jt = new JRO.JetEngineClass();
            
    //使用JetEngineClass对象的CompactDatabase方法压缩修复数据库
            jt.CompactDatabase(mdbPath2, temp2);
            
    //拷贝临时数据库到目标数据库(覆盖)
            File.Copy(temp,mdbPath, true);
            
    //最后删除临时数据库
            File.Delete(temp);
        }

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://cid-1c90f5701b6cc864.spaces.live.com/blog/cns!1C90F5701B6CC864!165.trak
    Weblogs that reference this entry
    • None