📄 main.cpp
字号:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Main.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "ABSMain"
#pragma link "CSPIN"
#pragma link "CGAUGES"
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
Dbtables::Session->GetAliasNames(lbImportAliases->Items);
lbImportAliasTablesClick(NULL);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::lbImportAliasesClick(TObject *Sender)
{
Dbtables::Session->GetTableNames(lbImportAliases->Items->Strings[lbImportAliases->ItemIndex],"",True,True,lbImportAliasTables->Items);
lbImportAliasTablesClick(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::lbImportAliasTablesClick(TObject *Sender)
{
btStart->Enabled = (lbImportAliasTables->SelCount > 0);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btBrowseImportDestinationDBClick(TObject *Sender)
{
if (odAbsDb->Execute())
edImportDestDB->Text = odAbsDb->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btStopImportClick(TObject *Sender)
{
IsStopped = True;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btStartClick(TObject *Sender)
{
int i, tableCount;
TListBox *tables;
AnsiString tableName;
bool PromptOverwrite;
TModalResult mr;
AnsiString Log;
PromptOverwrite = True;
IsStopped = False;
AbsDB->Close();
Table->Close();
mImportDetails->Clear();
AbsDB->DatabaseFileName = edImportDestDB->Text;
mImportDetails->Lines->Add(Format("Import tables from '%s' to '%s'",
ARRAYOFCONST((lbImportAliases->Items->Strings[lbImportAliases->ItemIndex],AbsDB->DatabaseFileName))));
if (!AbsDB->Exists)
AbsDB->CreateDatabase();
try
{
AbsDB->Open();
}
catch(...)
{
MessageDlg(Format("Cannot open '%s' database file",
ARRAYOFCONST((AbsDB->DatabaseFileName))),mtError,TMsgDlgButtons()<<mbOK,0);
return;
}
tableCount = lbImportAliasTables->Items->Count;
tables = lbImportAliasTables;
Table->DatabaseName = lbImportAliases->Items->Strings[lbImportAliases->ItemIndex];
gOverallImport->MaxValue = tables->SelCount;
gOverallImport->Progress = 0;
btStart->Enabled = False;
btStopImport->Enabled = True;
// import tables
for (i=0;i<tableCount;i++)
{
if (tables->Selected[i])
{
if (IsStopped) break;
ABSTable->Close();
Table->Close();
tableName = tables->Items->Strings[i];
Table->TableName = tableName;
// SQL Server: dbo->table -> table
if (LowerCase(tableName).Pos("dbo->") == 1)
ABSTable->TableName = tableName.SubString(5, tableName.Length()-4);
else
if (ExtractFileExt(tableName) != "")
ABSTable->TableName = tableName.SubString(1,
tableName.Length()-ExtractFileExt(tableName).Length());
else
ABSTable->TableName = tableName;
// overwrite existing table?
if (ABSTable->Exists && PromptOverwrite)
{
mr = MessageDlg(Format("Table '%s' exists in '%s' database-> Do you want to overwrite it?",
ARRAYOFCONST((ABSTable->TableName, AbsDB->DatabaseFileName))),
mtConfirmation,TMsgDlgButtons()<<mbYes<<mbNo<<mbAll,0);
if (mr == mrNo)
{
mImportDetails->Lines->Add(Format("Tables '%s' already exists, its import cancelled by user",
ARRAYOFCONST((ABSTable->TableName))));
gOverallImport->Progress = gOverallImport->Progress + 1;
continue;
}
else
if (mr == mrAll)
PromptOverwrite = False;
}
// import table
lImportTable->Caption = Format("Importing table '%s'", ARRAYOFCONST((tableName)));
mImportDetails->Lines->Add(lImportTable->Caption);
Log = "";
try
{
Table->Open();
ABSTable->ImportTable(Table, Log, Table->IndexDefs);
ABSTable->Open();
if (Log == "") Log = "No errors";
mImportDetails->Lines->Add(Format("Table '%s' imported-> %d records transferred, %d records skipped-> ErrorLog: %s",
ARRAYOFCONST((tableName, ABSTable->RecordCount, Table->RecordCount-ABSTable->RecordCount, Log))));
}
catch(Exception &E)
{
mImportDetails->Lines->Add(Format("Table '%s' import failed-> Error: %s-> ErrorLog: %s",
ARRAYOFCONST((tableName, E.Message, Log))));
}
gOverallImport->Progress = gOverallImport->Progress + 1;
}
}
if (IsStopped)
mImportDetails->Lines->Add("Import stopped by user");
else
mImportDetails->Lines->Add("Import finished");
btStart->Enabled = True;
btStopImport->Enabled = False;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ABSTableBeforeImport(TObject *Sender)
{
gTableImport->Progress = 0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::ABSTableImportProgress(TObject *Sender,
int PercentDone, bool &Continue)
{
gTableImport->Progress = PercentDone;
Continue = !IsStopped;
Application->ProcessMessages();
}
//---------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -