Convert Excel data to MDB file

Source: Internet
Author: User

Required Components:

Microsoft ADO Ext. 2.8 for DDL and security or updated components.

Add to:

Using ADOX;
Using System.Runtime.InteropServices;
Using System.IO;

Then take advantage of the OleDbCommand component, set its 2 links, a link to find and open the Excel data source, and another link to insert the data source into the MDB file.

Operation Page:

Background Source:

  Private voidButton1_Click (Objectsender, EventArgs e) {            varMdbpath = This. Txtmdbpath.text; if(File.exists (mdbpath) = =false) {ADOX. Catalog Catalog=NewADOX. Catalog (); Dynamic CN =NULL; Try{cn= Catalog. Create (string. Format ("Provider=Microsoft.Jet.OLEDB.4.0;Data source={0};", Mdbpath)); }                finally                {                    if(CN! =NULL) {Marshal.finalreleasecomobject (CN);                } marshal.finalreleasecomobject (catalog); }                stringExcelpath = This. Txtexcelpath.text; stringExcelconnection =string. Format ("Provider=microsoft.ace.oledb.12.0;data source={0}; Extended properties=\ "Excel 8.0; Hdr=yes\ "", Excelpath); using(System.Data.OleDb.OleDbConnection Accessconn =NewSystem.Data.OleDb.OleDbConnection (excelconnection))                    {Accessconn.open (); stringInsertcommandtext =string. Format ("SELECT * into [MS access;database={0}]. [Sheet1] from [sheet1$];", Mdbpath); using(System.Data.OleDb.OleDbCommand Accesscommand =NewSystem.Data.OleDb.OleDbCommand (Insertcommandtext, Accessconn))                    {accesscommand.executenonquery ();                } accessconn.close (); }} MessageBox.Show ("The data was written successfully. "); }

Note: The Data Source link section in Insertcommandtext must be written: database= ..., cannot write data source= ..., otherwise a "table already exists" exception appears.

Convert Excel data to MDB file

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.