Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of C # compressing Access database?

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the method of C# compressing Access database". In the daily operation, I believe that many people have doubts about the method of C# compressing Access database. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the method of C# compressing Access database?" Next, please follow the editor to study!

I don't know if you're tired of adding complex COM library references to your project, but I'm sure this pure .NET code will save extra interoperability, RCWs and COM references. Basically, due to the different Microsoft class libraries installed in the system (for example, MS Office Object Library 9, 10, 10, 11, and so on), we do not know the version of Office installed in the user's PC, so we have to access the COM object through ProgID instead of using CLSID. For example, when you call "Excel.Application", you get Excel, regardless of the version of MS Office installed in the system, when you add a "MS Excel 10 Object library" reference to the code, you only add a very restricted function to the application. So we use System.Reflection and late binding.

1. Instance code

Simply call the CompactAccessDB function to compress and repair the target database.

two。 Parameters:

◆ connectionString-used to connect to the Access database.

◆ mdwfilename-the full name of the MDB file to be compressed (path + file name).

Due to the limitation of the Jet engine, executing this method to compress the Access database in C # will generate the result into a new file, so we also need to copy the new Access file to the destination to overwrite the original uncompressed file.

When calling this method, make sure that there is no open connection to the compressed database.

The code is as follows:

/ MBD compact method (c) 2004 Alexander Youmashev / /!! make sure there's no open connections / to your db before calling this method! /! imposter! / connection string to your db / / FULL name / of an MDB file you want to compress. 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 / (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;} at this point, the study of "what is the method of C # compressing Access database" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report