Vb. NET fixes an access MDB through JRO

Source: Internet
Author: User
Tags compact tmp file

Imports System
Imports System.IO

Public Class Form1

Private Sub button1_click (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Button1.Click
' Will C:db1.mdb do Compact/repair
If Compact_mdb ("C:db1.mdb") Then MessageBox.Show ("OK!")
End Sub

Private Function Compact_mdb (ByVal strfile as String) as Boolean

' Jet Access (MDB) connection string; Jet (joint Engine Technology)
Dim STRCN = "Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}"

' or ' Provider=Microsoft.Jet.OLEDB.4.0;Data source={0}; Jet oledb:engine type=5 "

' Path.gettempfilename method: Create a unique staging file with a 0-bit life on the disk,
' And then return the full path to the file.
Dim strtmpfile as String = Path.GetTempFileName.Replace (". tmp", ". mdb") ' Change TMP file name to MDB

' Create an array of objects to store the arguments (parameters), source, purpose
Dim Objpara as Object () = New Object () {String.Format (STRCN, strfile), String.Format (STRCN, Strtmpfile)}

' Activator member: A method that includes the local or remote establishment of an object type, or obtains a reference to an existing remote object.
' Activator.CreateInstance method (type): Creates an instance of the specified type using the constructor that best matches the specified parameter.
Dim Objjro as Object = Activator.CreateInstance (Type.gettypefromprogid ("JRO. JetEngine "))
' Type.gettypefromprogid method: Gets the type associated with the specified program identifier (ProgID);
' Returns NULL if an error occurs while loading the type.
' JRO. JetEngine for Microsoft Jet and Replication Objects x.x Library

' Type.InvokeMember method
' Type.InvokeMember (String, BindingFlags, Binder, Object, object[])
ObjJRO.GetType.InvokeMember ("CompactDatabase", Reflection.BindingFlags.InvokeMethod, _
Nothing, Objjro, Objpara)

' uses the specified binding condition constraint and conforms to the specified list of arguments to call the specified member.
' BindingFlags enumeration Type, InvokeMethod specifies the method to be called.

File.delete (strfile) ' File.delete method: Delete The MDB file before the compact
File.move (Strtmpfile, strfile) ' File.move method: Change the Compact MDB file to (back) the correct file name

' Marshal.ReleaseComObject method to release JRO COM object
Runtime.InteropServices.Marshal.ReleaseComObject (OBJJRO)
Objjro = Nothing

Return True

End Function

End Class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.