C++ to implement the data import EXCEL database of ACCESS MSSQL and other instance code
- 2020-05-17 06:06:26
- OfStack
In C++, the implementation of EXCEL data into the database (ACCESS, MSSQL, etc.) instance code
Before importing EXCEL's data into the database, do a few simple things:
1. Save the EXCEL table where the data resides as DBF 4.
2. Open BCB and add the controls AdoTable(renamed DBFTable) and DataSource
OK, preparation, that's all, all you have to do is code
1. In the Form_Load() event, add the following code:
AnsiString filepath=ExtractFilePath(FileName);
//FileName for DBF The file name
AnsiString tablename=ExtractFileName(FileName).SubString(0,ExtractFileName(FileName).Length()-4);// Remove file extensions
//-------------------- Used to connect to 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. New member function of 1 class, void s 34en s 35en (TADOQuery *ADOQuery);
The following is the specific implementation of the function:
void __fastcall TDBFToDBForm::ExcelToDB(TADOQuery *ADOQuery)
{// Implementation is very simple, just put DBFTable The data is added item by item to the local database
DBFTable->First();
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(" Import success !"); }
Note :DBFTable is used to load data saved by EXCEL, while the program itself loads data by ADOQuery(you can change, of course).
Thank you for reading, I hope to help you, thank you for your support of this site!