This code compacts and repairs an MS Access database from a C# .NET application
2005-04-17 13:19
387 查看
Introduction
This code compacts and repairs an MS Access database from a C# .NET application, no matter if it's a simple ".mdb", or a ".mdw"-secured workgroup-shared DB. It performs exactly the same operation as "Tools - Database Utils - Compact and Repair Database..." menu item in the MS Access application. The code uses "late binding" (creating COM-objects in memory at runtime), and that's why you don't need any annoying interop COM references in your project. You don't even need MS Office installed. Just make sure you have a Jet Engine (Jet is included in MDAC package, which comes with any Windows installation starting from NT 4).
Background
Don't you hate COM-library references in .NET-projects? I believe that pure .NET-code has to be free of any interops, RCWs, and other referenced COM-stuff. Basically because there's a load of different versions of MS libraries (for example, MS Office Object Library 9, 10, 11 etc.). We never know what version of MS Office is installed on a client machine, that's why we should access a COM-object via ProgID, and not CLSID. For example: you want to be sure, that when you call for an "Excel.Application", you get Excel, no matter what version of MS Office is installed. And when you add a reference "MS Excel 10 Object library", you add a strong limitation to your software! So... use System.Reflection and late binding.
Using the code
Just call a method CompactAccessDB. This method compacts and repairs your database.
Parameters:
connectionString - connection string to your database.
mdwfilename - is a full name (path+name) of an MDB-file you want to compact and repair.
Due to Jet limitations, the method compacts your database to a new file, so we have to copy the new compacted file over an old one.
When you call this method, make sure that there's no open connections to your database. Stop your threads.
Now, to the code:
public static void CompactAccessDB(string connectionString, string mdwfilename)
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
This code compacts and repairs an MS Access database from a C# .NET application, no matter if it's a simple ".mdb", or a ".mdw"-secured workgroup-shared DB. It performs exactly the same operation as "Tools - Database Utils - Compact and Repair Database..." menu item in the MS Access application. The code uses "late binding" (creating COM-objects in memory at runtime), and that's why you don't need any annoying interop COM references in your project. You don't even need MS Office installed. Just make sure you have a Jet Engine (Jet is included in MDAC package, which comes with any Windows installation starting from NT 4).
Background
Don't you hate COM-library references in .NET-projects? I believe that pure .NET-code has to be free of any interops, RCWs, and other referenced COM-stuff. Basically because there's a load of different versions of MS libraries (for example, MS Office Object Library 9, 10, 11 etc.). We never know what version of MS Office is installed on a client machine, that's why we should access a COM-object via ProgID, and not CLSID. For example: you want to be sure, that when you call for an "Excel.Application", you get Excel, no matter what version of MS Office is installed. And when you add a reference "MS Excel 10 Object library", you add a strong limitation to your software! So... use System.Reflection and late binding.
Using the code
Just call a method CompactAccessDB. This method compacts and repairs your database.
Parameters:
connectionString - connection string to your database.
mdwfilename - is a full name (path+name) of an MDB-file you want to compact and repair.
Due to Jet limitations, the method compacts your database to a new file, so we have to copy the new compacted file over an old one.
When you call this method, make sure that there's no open connections to your database. Stop your threads.
Now, to the code:
public static void CompactAccessDB(string connectionString, string mdwfilename)
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
相关文章推荐
- [C#.NET][Entity Framework] Code First - Reverse engineering from an existing database at EF6.1.0
- HOWTO:Turn Off PDA Display while your application is running.(VB.Net/c#+.Net CF+PPC2003) from CodeProject
- Call C# Code from C++ and read an array of struct which contains strings
- MVC architecture in ASP.Net using C# and Microsoft Data Access Application block
- [导入]How can I run another application or batch file from my Visual C# .NET code?
- ASP.NET datagrid直接导出为Excel Export to Excel from DataGrid (Using an Microsoft Access database)
- This event supports the .NET Framework infrastructure and is not intended to be used directly from your code?继承自VScrollbar的自定义winform控件,某些事件不触发的问题(Winform控件开发学习)
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- Using C# to connect to and query from a SQL database
- ASP.NET 调试出现<%@ Application Codebehind="Global.asax.cs" Inherits="XXX.XXX.Global" Language="C#" %>
- Create an Asp.Net Web Forms Application using Bootstrap and Web API
- To consume a managed type from COM 之四 Deploying an Application for COM Access
- C# and VB.NET Code Searcher - Using Roslyn(使用Roslyn实现的C#和VB.Net的代码查询器)
- An article about Equals and GetHashCode functions in C#
- C# Datediff (from:http://aspcode.net/c-datediff)
- This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes
- Walkthrough: Editing an Access Database with ADO.NET
- csharp:SQLite and Access using C# code read data
- Implementing the Repository and Unit of Work Patterns in an ASP.NET MVC Application (9 of 10)
- This file is based on the Microsoft Data Access Application Block for .NET