Use. Net to operate access databases

Source: Internet
Author: User
Use. Net to operate access databases (create, compress, back up, and restore components)
Write a smallProgramThere is no Microsoft Access at hand. It is still easy to create a database.
First, reference c: \ Program Files \ common files \ System \ ADO \ msadox. dll, which contains the ADOX namespace;
Then reference c: \ Program Files \ common files \ System \ ADO \ msjro. dll, which contains the jro namespace
Note: For example, if the DLL Import fails, manually import the COM component as a. NET Component and use the vs.net tool to import it.

Using system;
Using system. IO;
Using ADOX; // This namespace contains the class (method) for creating access)-- Solution ==> reference ==> add reference ==> visit to find. dll
Using jro; // The namespace contains the class (method) for compressing access)

Public class access
{
/// Create an Access database based on the specified file name
/// Mdbpath: the absolute access path of the file to be created
 Public void create (string mdbpath)
{
If (file. exists (mdbpath) // check whether the database already exists
{
Throw new exception ("the target database already exists and cannot be created ");
}
// You can add a password so that the created database can be opened only after the password is entered.
Mdbpath = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + mdbpath;
// Create an instance of the catalogclass object,
ADOX. catalogclass cat = new ADOX. catalogclass ();
// Use the create method of the catalogclass object to create an Access database
Cat. Create (mdbpath );
}

/// Compress and fix the ACCESS database. The mdbpath is the absolute path of the database.
Public void compact (string mdbpath)
{
If (! File. exists (mdbpath )) // Check whether the database already exists
{
Throw new exception ("the target database does not exist and cannot be compressed ");
}
// Declare the temporary database name
String temp = datetime. Now. year. tostring ();
Temp + = datetime. Now. Month. tostring ();
Temp + = datetime. Now. Day. tostring ();
Temp + = datetime. Now. Hour. tostring ();
Temp + = datetime. Now. Minute. tostring ();
Temp + = datetime. Now. Second. tostring () + ". Bak ";
Temp = mdbpath. substring (0, mdbpath. lastindexof ("\") + 1) + temp;
// Define the connection string of the temporary database
Temp2 = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + temp;
// Define the connection string of the target database
Mdbpath2 = "provider = Microsoft. Jet. oledb.4.0; Data Source =" + mdbpath;
// Create an instance of the jetengineclass object
Jro. jetengineclass Jt = new jro. jetengineclass ();
// Use the compactdatabase method of the jetengineclass object to compress and restore the database
JT. compactdatabase (mdbpath2, temp2 );
// Copy the temporary database to the target database (overwrite)
File. Copy (temp, mdbpath, true );
// Delete the temporary database
File. Delete (temp );
}

/// Backup database, mdb1, absolute path of the source database; mdb2: absolute path of the target database
Public void backup (string mdb1, string mdb2)
{
If (! File. exists (mdb1 ))
{
Throw new exception ("the source database does not exist ");
}
Try
{
File. Copy (mdb1, mdb2, true );
}
Catch (ioexception IXP)
{
Throw new exception (IXP. tostring ());
}
}

/// Restore the database. mdb1 indicates the absolute path of the backup database, and mdb2 indicates the absolute path of the current database.
Public void recover (string mdb1, string mdb2)
{
If (! File. exists (mdb1 ))
{
throw new exception ("the backup database does not exist ");
}
try
{
File. copy (mdb1, mdb2, true);
}
catch (ioexception IXP)
{
throw new exception (IXP. tostring ();
}< BR >}

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.