Import Excel data to the database (access, MSSQL, etc)

Source: Internet
Author: User

The following code is from http://blog.csdn.net/hellogv/. for reference, see the source!

First, this article is intended for friends with database development experience. If you think that this article is not helpful to you, do not laugh. The following code is written by the younger brother.

Before importing Excel data to the database, perform some simple preparation:

1. Save the Excel table in DBF 4 format.

2. Open BCB and add the adotable (renamed dbftable) and datasource controls.

OK. The preparation is over. The rest is code.

1. In the form_load () event, add the following code:

Ansistring filepath = extractfilepath (filename); // filename is the DBF File Name ansistring tablename = extractfilename (filename ). substring (0, extractfilename (filename ). length ()-4); // remove the file extension

// -------------------- Connect to the DBF File ---------------------------------- dbftable-> connectionstring = "provider = Microsoft. jet. oledb.4.0; Password =/"/"; Data Source =/"" + filepath + "/"; extended properties = DBASE 5.0; persist Security info = true "; dbftable-> tablename = tablename; dbftable-> open ();

2. Create a class member function void _ fastcall exceltodb (tadoquery * adoquery );

The specific implementation of the function is as follows:

Void _ fastcall tdbftodbform: exceltodb (tadoquery * adoquery) {// The implementation is very simple, just add dbftable data to the local database dbftable-> first () one by one (); for (Int J = 0; j <dbftable-> recordcount; j ++) {adoquery-> append (); For (INT I = 0; I <dbftable-> fieldcount; I ++) adoquery-> fieldbyname (dbftable-> fieldlist-> strings [I])-> asstring = dbftable-> fieldbyname (dbftable-> fieldlist-> strings [I]) -> asstring; adoquery-> post (); dbftable-> next ();} showmessage ("Export Inbound successful! ");}

Note: dbftable is used to load the data stored in another Excel file, while adoquery is used to load the data in the program itself (of course, you can also change it ),

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.