如题...
还有数据库里的主键是自动编号的.如果前面的被删除了,能不能让该主键自动收缩..
比如本来是1-2000.现在我删了1-1000.能不能让1001-2000的自动变成1-1000

解决方案 »

  1.   

    菜单----工具----压缩和修复数据库第二个问题不能,因为也许你其他的表有数据和这个表中的主键联系了,比如1001对应你自己,
    ID    NAME
    1001  __smallPig__ (小猪哼哼...) 另外一张表结构如下
    ID    Salary
    1001  5000你改了数据后,变成
    ID    NAME
    1001  无间道III其他的表就找不到先前的你了,你的钱就是我的了
      

  2.   

    忘记说了,压缩数据库的代码是:/// <summary>
    /// MBD compact method (c) 2004 Alexander Youmashev
    /// !!IMPORTANT!!
    /// !make sure there's no open connections
    ///    to your db before calling this method!
    /// !!IMPORTANT!!
    /// </summary>
    /// <param name="connectionString">connection string to your db</param>
    /// <param name="mdwfilename">FULL name
    ///     of an MDB file you want to compress.</param>
    public static void CompactAccessDB(string connectionString, string mdwfilename)
    {
        object[] oParams;    //create an inctance of a Jet Replication Object
        object objJRO = 
          Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));    //filling Parameters array
        //cnahge "Jet OLEDB:Engine Type=5" to an appropriate value
        // or leave it as is if you db is JET4X format (access 2000,2002)
        //(yes, jetengine5 is for JET4X, no misprint here)
        oParams = new object[] {
            connectionString,
            "Provider=Microsoft.Jet.OLEDB.4.0;Data" + 
            " Source=C:\\tempdb.mdb;Jet OLEDB:Engine Type=5"};    //invoke a CompactDatabase method of a JRO object
        //pass Parameters array
        objJRO.GetType().InvokeMember("CompactDatabase",
            System.Reflection.BindingFlags.InvokeMethod,
            null,
            objJRO,
            oParams);    //database is compacted now
        //to a new file C:\\tempdb.mdw
        //let's copy it over an old one and delete it    System.IO.File.Delete(mdwfilename);
        System.IO.File.Move("C:\\tempdb.mdb", mdwfilename);    //clean up (just in case)
        System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
        objJRO=null;
    }
    Points of Interest
    Interesting, that Jet Engine 5 is used for JET4X databases. Be careful. See the table:Jet OLEDB:Engine Type Jet x.x Format MDB Files 
    1 JET10 
    2 JET11 
    3 JET2X 
    4 JET3X 
    5 JET4X