How to use c # To operate ACCESS databases

Source: Internet
Author: User
How to use c # To operate ACCESS databases: Microsoft ACCESS is not available at hand, and how to create a database is still simple. first, reference C: ProgramFilesCommonFilesSystemadomsadox. dll, which contains the ADOX namespace; then reference C: ProgramFilesCommonFilesSystemadomsjro. dll, which contains the JRO namespace SxS

How to use c # To operate ACCESS databases: Microsoft Access is not available at hand, and how to create a database is still simple. first, reference C: Program FilesCommon FilesSystemadomsadox. dll, which contains the ADOX namespace. Then reference C: Program FilesCommon FilesSystemadomsjro. dll, which contains the JRO namespace SxS

How to use c # To operate ACCESS databases:

There is no Microsoft Access at hand. It is still easy to create a database.

Reference C: Program FilesCommon FilesSystemadomsadox. dll, which contains the ADOX namespace;

Then reference C: Program FilesCommon FilesSystemadomsjro. dll, which contains the JRO namespace

Tips for SxS: for example, if the dll Import fails, manually import the com component as a. net Component and use vs.net to import it.

Using System;

Using System. IO;

Using ADOX; // The namespace contains the class (method) for creating ACCESS -- Solution ==> reference ==> add reference ==> 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 );

}/** // Back up the database, mdb1, absolute path of the source database, and 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 ());

}

}

}

**************************************** **************************************** **********************

In BETA2 ,. NET provides the following NAMESPACE:

System. Data Namespace

System. Data. OleDb (it is different from BETA1, so it is definitely not possible to run the program in BETA1 to BETA2)

If I want to clarify these things, I don't think I can do it, so I want to use some specific programs to demonstrate the most basic database operations (SELECT, UPDATE, DELETE, INSERT, etc, other things need to be learned by friends!

To operate a database, you must first open the database. The following uses an ACCESS database as an example to illustrate how to open a database connection! Here we need to use: System. Data. OleDb. OleDbConnection class! (If you operate SQL databases, we 'd better use the System. Data. SqlClient. SqlConnection class)

I will first write my own program:

Using System. Data

Using System. Data. OleDb

Public OleDbConnection getConn ()

{

String connstr = "Provider = Microsoft. Jet. OLEDB.4.0; Data Source = F: webnotesbookclassleavenotes. mdb ";

OleDbConnection tempconn = new OleDbConnection (connstr );

Return (tempconn );

}

I believe that anyone who has used ADO can understand it! First, we define a variable of the String type, which stores the connection String that we connect to the database, and then define a System. data. oleDb. oleDbConnection type object, instantiate it, and finally return this object! It should be noted that I didn't put the statement: tempconn. Open (); into this function. The reason is that I will explain it later. Here I just want to remind you!

Through the above function, we have obtained a Connection object similar to the Connection object in ADO! The following is the specific database operation!

Before specific operations, I think it is necessary to first understand the following two classes:

System. Data. OleDb. OleDbDataAdapter

System. Data. OleDb. OleDbDataReader

System. Data. OleDb. OleDbDataAdapter: You can directly contact DataSet and operate the Data source. It is more powerful and consumes System resources!

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.