📄 dbporterdlg.cpp
字号:
// dbporterDlg.cpp : implementation file
//
#include "stdafx.h"
#include "dbporter.h"
#include "dbporterDlg.h"
#include "ErrDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#import "C:\Program Files\Common Files\System\ADO\msado15.dll" \
no_namespace rename("EOF", "EndOfFile")
/////////////////////////////////////////////////////////////////////////////
// CDbporterDlg dialog
CDbporterDlg::CDbporterDlg(CWnd* pParent):CDialog(CDbporterDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDbporterDlg)
m_sSrcConnect = _T("");
m_sSrcLogin = _T("");
m_sSrcPassword = _T("");
m_sSrcTable = _T("");
m_sTgtConnect = _T("");
m_sTgtLogin = _T("");
m_sTgtPassword = _T("");
m_sTgtTable = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CDbporterDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDbporterDlg)
DDX_Control(pDX, ID_COPY, m_butCopy);
DDX_Control(pDX, IDC_TGTCONNECT, m_TgtBut);
DDX_Control(pDX, IDC_SRCCONNECT, m_SrcBut);
DDX_Text(pDX, IDC_SRCCONSTR, m_sSrcConnect);
DDX_Text(pDX, IDC_SRCLOGIN, m_sSrcLogin);
DDX_Text(pDX, IDC_SRCPASS, m_sSrcPassword);
DDX_Text(pDX, IDC_SRCTABLE, m_sSrcTable);
DDX_Text(pDX, IDC_TGTCONSTR, m_sTgtConnect);
DDX_Text(pDX, IDC_TGTLOGIN, m_sTgtLogin);
DDX_Text(pDX, IDC_TGTPASS, m_sTgtPassword);
DDX_Text(pDX, IDC_TGTTABLE, m_sTgtTable);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDbporterDlg, CDialog)
//{{AFX_MSG_MAP(CDbporterDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_SRCCONNECT, OnSrcconnect)
ON_BN_CLICKED(IDC_TGTCONNECT, OnTgtconnect)
ON_EN_CHANGE(IDC_SRCCONSTR, OnChangeSrcconstr)
ON_EN_CHANGE(IDC_SRCLOGIN, OnChangeSrclogin)
ON_EN_CHANGE(IDC_SRCPASS, OnChangeSrcpass)
ON_EN_CHANGE(IDC_SRCTABLE, OnChangeSrctable)
ON_EN_CHANGE(IDC_TGTCONSTR, OnChangeTgtconstr)
ON_EN_CHANGE(IDC_TGTLOGIN, OnChangeTgtlogin)
ON_EN_CHANGE(IDC_TGTPASS, OnChangeTgtpass)
ON_EN_CHANGE(IDC_TGTTABLE, OnChangeTgttable)
ON_BN_CLICKED(ID_COPY, OnCopy)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDbporterDlg message handlers
BOOL CDbporterDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
m_bIsTargetConnected=FALSE;
m_bIsSourceConnected=FALSE;
RECT r;
r.left=20;r.top=170;r.right=510;r.bottom=340;
m_PropList.Create(WS_CHILD | WS_VISIBLE,r,this,1001);
m_PropList.ShowWindow(SW_SHOW);
m_butCopy.EnableWindow(FALSE);
return TRUE;
}
void CDbporterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
CDialog::OnSysCommand(nID, lParam);
}
void CDbporterDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDbporterDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// Connect to the source
void CDbporterDlg::OnSrcconnect()
{
UpdateData();
CString error;
if (m_DBCopier.ConnectSource(m_sSrcConnect,m_sSrcLogin,m_sSrcPassword,m_sSrcTable,error))
{
// success
m_bIsSourceConnected=TRUE;
m_SrcBut.EnableWindow(FALSE);
}
else
{
MessageBox(error,"Connection Error");
return;
}
if (m_bIsTargetConnected)
{
PopulateMapList();
m_butCopy.EnableWindow(TRUE);
}
}
// Connect to the table
void CDbporterDlg::OnTgtconnect()
{
UpdateData();
CString error;
if (m_DBCopier.ConnectTarget(m_sTgtConnect,m_sTgtLogin,m_sTgtPassword,m_sTgtTable,error))
{
// success
m_bIsTargetConnected=TRUE;
m_TgtBut.EnableWindow(FALSE);
}
else
{
MessageBox(error,"Connection Error");
return;
}
if (m_bIsSourceConnected)
{
PopulateMapList();
m_butCopy.EnableWindow(TRUE);
}
}
void CDbporterDlg::OnChangeSrcconstr()
{
m_bIsSourceConnected=FALSE;
m_SrcBut.EnableWindow(TRUE);
m_butCopy.EnableWindow(FALSE);
EmptyMapList();
}
void CDbporterDlg::OnChangeSrclogin()
{
m_bIsSourceConnected=FALSE;
m_SrcBut.EnableWindow(TRUE);
m_butCopy.EnableWindow(FALSE);
EmptyMapList();
}
void CDbporterDlg::OnChangeSrcpass()
{
m_bIsSourceConnected=FALSE;
m_SrcBut.EnableWindow(TRUE);
m_butCopy.EnableWindow(FALSE);
EmptyMapList();
}
void CDbporterDlg::OnChangeSrctable()
{
m_bIsSourceConnected=FALSE;
m_SrcBut.EnableWindow(TRUE);
m_butCopy.EnableWindow(FALSE);
EmptyMapList();
}
void CDbporterDlg::OnChangeTgtconstr()
{
m_bIsTargetConnected=FALSE;
m_TgtBut.EnableWindow(TRUE);
m_butCopy.EnableWindow(FALSE);
EmptyMapList();
}
void CDbporterDlg::OnChangeTgtlogin()
{
m_bIsTargetConnected=FALSE;
m_TgtBut.EnableWindow(TRUE);
m_butCopy.EnableWindow(FALSE);
EmptyMapList();
}
void CDbporterDlg::OnChangeTgtpass()
{
m_bIsTargetConnected=FALSE;
m_TgtBut.EnableWindow(TRUE);
m_butCopy.EnableWindow(FALSE);
EmptyMapList();
}
void CDbporterDlg::OnChangeTgttable()
{
m_bIsTargetConnected=FALSE;
m_TgtBut.EnableWindow(TRUE);
m_butCopy.EnableWindow(FALSE);
EmptyMapList();
}
void CDbporterDlg::PopulateMapList()
{
m_PropList.RemoveAllItems();
PROPERTYITEMSETUP ItemInfo;
ItemInfo.dwType = FPSPROPERTYITEMTYPE_COMBOBOX;
ItemInfo.bAllowComboEdit = FALSE;
_RecordsetPtr rsS=m_DBCopier.GetSourceRecordset();
int sfc=rsS->Fields->Count;
ItemInfo.ComboBoxList.AddTail("");
ItemInfo.ComboBoxList.AddTail("Autonumber this field");
for(int i=0;i<sfc;i++)
{
FieldPtr field=rsS->GetFields()->GetItem(_variant_t((long)i,VT_I4));
_bstr_t fieldname=field->GetName();
ItemInfo.ComboBoxList.AddTail((char*)fieldname);
}
_RecordsetPtr rsT=m_DBCopier.GetTargetRecordset();
int tfc=rsT->Fields->Count;
for(i=0;i<tfc;i++)
{
FieldPtr field=rsT->GetFields()->GetItem(_variant_t((long)i,VT_I4));
_bstr_t fieldname=field->GetName();
ItemInfo.strName = (char*) fieldname;
m_PropList.AddItem(&ItemInfo);
}
m_PropList.Invalidate();
}
void CDbporterDlg::EmptyMapList()
{
m_PropList.RemoveAllItems();
m_PropList.Invalidate();
}
// Copy data from source to target
// according to the selected mapping
void CDbporterDlg::OnCopy()
{
CErrDialog erd;
CStringArray errors;
erd.m_pErrors=&errors;
BOOL bHasErrors=FALSE;
m_PropList.NotifyItems();
int fc = m_PropList.GetItemCount();
_RecordsetPtr rsS=m_DBCopier.GetSourceRecordset();
_RecordsetPtr rsT=m_DBCopier.GetTargetRecordset();
try
{
rsS->MoveFirst();
}catch(_com_error &e)
{
errors.Add((char*)e.Description());
bHasErrors=TRUE;
}
long rc=0;
try
{
rc=rsS->GetRecordCount();
}
catch(_com_error& e)
{
errors.Add((char*)e.Description());
bHasErrors=TRUE;
}
int serial=1;
while (rsS->AbsolutePosition != adPosEOF )
{
BOOL bRSCreated=FALSE;
for(int i=1;i<=fc;i++)
{
PROPERTYITEMSETUP ps;
m_PropList.GetItemInfo(i,ps);
CString item=ps.strName;
CString selection;
m_PropList.GetItemValue(i,selection);
if (selection.GetLength()>1)
{
if (!bRSCreated)
{
rsT->AddNew();
bRSCreated=TRUE;
}
if (selection.CompareNoCase("Autonumber this field")==0)
{
rsT->GetFields()->GetItem((LPCTSTR)item)->Value=_variant_t((long)serial);
}
else
{
_variant_t sval;
try
{
sval=rsS->GetFields()->GetItem((LPCTSTR)selection)->Value;
rsT->GetFields()->GetItem((LPCTSTR)item)->GetType();
rsT->GetFields()->GetItem((LPCTSTR)item)->Value=sval;
}catch(_com_error& e)
{
TRACE("error %s\n",(char*)e.Description());
errors.Add((char*)e.Description());
bHasErrors=TRUE;
}
}
}
TRACE("combo:target field %s\t\t\tsource: %s\n",item,selection);
}
serial++;
rsS->Update();
try
{
rsS->MoveNext();
}catch(_com_error &e)
{
TRACE("move next error %s\n",(char*)e.Description());
errors.Add((char*)e.Description());
bHasErrors=TRUE;
break;
}
}// end of while loop
if (bHasErrors)
{
if (MessageBox("Do you want to see the errors ?","Errors in copying data",MB_YESNO)==IDYES)
{
erd.DoModal();
}
}
else
{
MessageBox("Successfully copied the data","Success",MB_OK);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -