This project has moved. For the latest updates, please go here.

Using JetCompact

Jun 28, 2012 at 5:58 PM

I am trying to test whatever I can use JetCompact to reclaim disk space, but I couldn't get it to work.

Here is a test program:

 

class Program
{
	static void Main()
	{
		JET_INSTANCE instance;
		Api.JetCreateInstance(out instance, "test");
		Api.JetInit(ref instance);
		using (var session = new Session(instance))
		{
			JET_DBID dbid;
			Api.JetCreateDatabase(session, "test", null, out dbid, CreateDatabaseGrbit.OverwriteExisting);
			Api.JetAttachDatabase(session, "test", AttachDatabaseGrbit.None);

			JET_TABLEID tableid;
			Api.JetCreateTable(session, dbid, "names", 100,90, out tableid);
			JET_COLUMNID columnid;
			Api.JetAddColumn(session, tableid, "name", new JET_COLUMNDEF
			{
			    cbMax = 255,
			    coltyp = JET_coltyp.Text,
			    cp = JET_CP.Unicode,
			    grbit = ColumndefGrbit.ColumnNotNULL
			}, null, 0, out columnid);

			Api.JetCloseTable(session, tableid);
			Api.JetCloseDatabase(session, dbid, CloseDatabaseGrbit.None);
			Api.JetDetachDatabase(session, "test");
		}

		using (var session = new Session(instance))
		{
			Api.JetCompact(session, @"test",
									@"test2",
				StatusCallback, null, CompactGrbit.Stats);
		}

	}

	private static JET_err StatusCallback(JET_SESID sesid, JET_SNP snp, JET_SNT snt, object data)
	{
		Console.WriteLine("{0} {1} {2} {3}", sesid, snp, snt, data);
		return JET_err.Success;
	}

}

When I am running this, I get database not found error.

Any ideas why?

Developer
Jun 28, 2012 at 9:28 PM

(Thanks for the repro, it helped a lot.)

This is not obvious, and not listed on MSDN. You need to Attach the database first (JetAttachDatabase).

-martin

Jun 29, 2012 at 4:36 AM

Thanks