Compresser une BD Access par .NET

cs_oboudou Messages postés 30 Date d'inscription mercredi 9 octobre 2002 Statut Membre Dernière intervention 15 avril 2004 - 5 mars 2004 à 16:24
facdaar Messages postés 64 Date d'inscription lundi 24 mars 2003 Statut Membre Dernière intervention 23 février 2009 - 4 nov. 2004 à 11:55
J'aimerais savoir comment compresser une base de donnée Access depuis un programme en visual studio. Note je n'ai pas Access sur la machine j'ai juste un round time pour les fichier Access.

Olivier Boudou

2 réponses

cs_coq Messages postés 6349 Date d'inscription samedi 1 juin 2002 Statut Membre Dernière intervention 2 août 2014 101
5 mars 2004 à 23:26
je sais pas au juste ce que tu entend par compresser mais bon si tu le prend dans le sens "zip" tu peux tjs essayer ça :
http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx

Cocoricoooooooo !!!!
0
facdaar Messages postés 64 Date d'inscription lundi 24 mars 2003 Statut Membre Dernière intervention 23 février 2009
4 nov. 2004 à 11:55
Voici le code que j'utilise, glané quelque part sur le net, et adapté pour mes besoins :

/// <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>
/// connection string to your db

/// FULL name
///     of an MDB file you want to compress.

private void CompactAccessDB(string connectionString, string mdwfilename) {
object[] oParams;

try {
//first check if a ldb file is not existing (opened connection to the db).
if (File.Exists(Path.Combine(_sAssemblyDir,"ArcDataArchiver.ldb"))) return;

// make a copy of the database before compacting it.
File.Copy(Path.Combine(_sAssemblyDir,"ArcDataArchiver.mdb"),Path.Combine(_sAssemblyDir,"ArcDataArchiver.mdb.sav"), true);

//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=" + Path.Combine(_sAssemblyDir,"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

File.Delete(mdwfilename);
File.Move(Path.Combine(_sAssemblyDir,"tempdb.mdb"), mdwfilename);

//clean up (just in case)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
objJRO=null;
}
catch (Exception e) {
Trace.WriteLine("ERROR in CompactAccessDB : " + e.Message + " / " + e.Source);
}
}



David.
0
Rejoignez-nous