⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 etldemodlg.cpp

📁 Data Replication Prototype Using ADO
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		try 
		{
			_RecordsetPtr pRSet;
			if(FAILED(pRSet.CreateInstance(__uuidof(Recordset))))
			{
				ASSERT(0);
				return;
			}
			pRSet->Open(L"SELECT CustomerID, CompanyName, ContactName FROM CUSTOMERS ORDER BY CustomerID, CompanyName", 
				(LPCTSTR) m_connectStringFrom, 
				adOpenForwardOnly, adLockReadOnly, adCmdText);
			
			for (; !pRSet->adoEOF; pRSet->MoveNext())
			{
				CString item((LPCTSTR) (_bstr_t) pRSet->Fields->Item[1L]->Value);
				item += _T(" / ");
				item += (LPCTSTR) (_bstr_t) pRSet->Fields->Item[2L]->Value;

				DWORD id = 0;
				VERIFY(S_OKAY == ascii_to_radix40((Radix40*)&id, pRSet->Fields->Item[0L]->Value.bstrVal, 6));
				int idx = m_listCustomers.AddString(item);
				m_listCustomers.SetItemData(idx, id);
			}

		}
		catch (_com_error& e)
		{
			ReportComError(e);
		}
	}
}


static LPCWSTR g_arrTables[] =
{
	L"Order Details",
	L"Orders",
	L"Products",
	L"Categories",
	L"Customers",
	L"Employees",
	L"Shippers",
	L"Suppliers",
};

void CETLdemoDlg::OnClearTarget() 
{
	if (!m_connectStringTo.CompareNoCase(m_connectStringFrom))
	{
		MessageBox("Source and destination are the same", NULL, MB_OK | MB_ICONEXCLAMATION);
		return;
	}
	
	CWaitCursor waitCursor;
	try 
	{
		_ConnectionPtr connectionTo;
		VERIFY(SUCCEEDED(connectionTo.CreateInstance(__uuidof(Connection))));
		connectionTo->Open((LPCTSTR) m_connectStringTo
			, _bstr_t(), _bstr_t(), adConnectUnspecified);

		for (int i = 0; i < sizeof(g_arrTables)/sizeof(g_arrTables[0]); i++)
		{
			wstring query(L"DELETE FROM [");
			query += g_arrTables[i]; 
			query += L']';
			connectionTo->Execute(query.c_str(), NULL, adExecuteNoRecords);
		}

		connectionTo->Close();
	}
	catch (_com_error& e)
	{
		ReportComError(e);
	}
}

void CETLdemoDlg::OnQueryCustomers() 
{
	UpdateCustomersView();
}

void CETLdemoDlg::OnAllCustomers() 
{
	UpdateCustomersView();
}

void CETLdemoDlg::UpdateCustomersView()
{
	UpdateData();
	GetDlgItem(IDC_LIST_CUSTOMERS)->ShowWindow(
		(1 == m_nCustomersMode)? SW_SHOWNA : SW_HIDE);
	GetDlgItem(IDC_STATIC_CUSTOMERS)->ShowWindow(
		(2 == m_nCustomersMode)? SW_SHOWNA : SW_HIDE);
	GetDlgItem(IDC_EDIT_CUSTOMERS)->ShowWindow(
		(2 == m_nCustomersMode)? SW_SHOWNA : SW_HIDE);
}

void CETLdemoDlg::UpdateEmployeesView()
{
	UpdateData();
	GetDlgItem(IDC_LIST_EMPLOYEES)->ShowWindow(
		(1 == m_nEmployeesMode)? SW_SHOWNA : SW_HIDE);
	GetDlgItem(IDC_STATIC_EMPLOYEES)->ShowWindow(
		(2 == m_nEmployeesMode)? SW_SHOWNA : SW_HIDE);
	GetDlgItem(IDC_EDIT_EMPLOYEES)->ShowWindow(
		(2 == m_nEmployeesMode)? SW_SHOWNA : SW_HIDE);
}

void CETLdemoDlg::OnAllEmployees() 
{
	UpdateEmployeesView();
}

void CETLdemoDlg::OnSelectEmployees() 
{
	UpdateEmployeesView();

	if (m_listEmployees.GetCount() == 0)
	{
		CWaitCursor waitCursor;
		try 
		{
			_RecordsetPtr pRSet;
			if(FAILED(pRSet.CreateInstance(__uuidof(Recordset))))
			{
				ASSERT(0);
				return;
			}
			pRSet->Open(L"SELECT EmployeeID, LastName, FirstName FROM EMPLOYEES ORDER BY LastName, FirstName", 
				(LPCTSTR) m_connectStringFrom, 
				adOpenForwardOnly, adLockReadOnly, adCmdText);
			
			for (; !pRSet->adoEOF; pRSet->MoveNext())
			{
				CString item((LPCTSTR) (_bstr_t) pRSet->Fields->Item[1L]->Value);
				item += _T(", ");
				item += (LPCTSTR) (_bstr_t) pRSet->Fields->Item[2L]->Value;

				int idx = m_listEmployees.AddString(item);
				m_listEmployees.SetItemData(idx, (long) pRSet->Fields->Item[0L]->Value);
			}

		}
		catch (_com_error& e)
		{
			ReportComError(e);
		}
	}
}

void CETLdemoDlg::OnQueryEmployees() 
{
	UpdateEmployeesView();
}

void CETLdemoDlg::OnBnClickedTest()
{
    clock_t start = clock();

	for (int i = 0; i < 10; ++i)
	{
		OnClearTarget();
		if (!DoTransform())
			return;
	}
	CString strBuf;
	strBuf.Format("Stuff was tested successfully in %g seconds."
		, (double)(clock() - start) / CLOCKS_PER_SEC);

	MessageBox(strBuf);
}

bool CETLdemoDlg::DoTransform()
{
	int i;
	UpdateData();

	m_connectStringFrom.TrimRight(); m_connectStringFrom.TrimLeft();
	m_connectStringTo.TrimRight(); m_connectStringTo.TrimLeft();

	if (m_connectStringFrom.IsEmpty())
	{
		MessageBox("Source connection string is empty", NULL, MB_OK | MB_ICONEXCLAMATION);
		return false;
	}
	if (m_connectStringTo.IsEmpty())
	{
		MessageBox("Destination connection string is empty", NULL, MB_OK | MB_ICONEXCLAMATION);
		return false;
	}
	if (!m_connectStringTo.CompareNoCase(m_connectStringFrom))
	{
		MessageBox("Source and destination are the same", NULL, MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	bool bOK = false;

	try 
	{
		_ConnectionPtr connectionFrom;
		VERIFY(SUCCEEDED(connectionFrom.CreateInstance(__uuidof(Connection))));
		connectionFrom->Open((LPCTSTR) m_connectStringFrom
			, _bstr_t(), _bstr_t(), adConnectUnspecified);

		_ConnectionPtr connectionTo;
		VERIFY(SUCCEEDED(connectionTo.CreateInstance(__uuidof(Connection))));
		connectionTo->Open((LPCTSTR) m_connectStringTo
			, _bstr_t(), _bstr_t(), adConnectUnspecified);

		AfxGetApp()->WriteProfileString(g_szConnectStrings, g_szSource, m_connectStringFrom);
		AfxGetApp()->WriteProfileString(g_szConnectStrings, g_szDestination, m_connectStringTo);

		CTableHolder DBProTableHolderFrom;
		DBProTableHolderFrom.SetDBManager(connectionFrom);
		CTableHolder DBProTableHolderTo;
		DBProTableHolderTo.SetDBManager(connectionTo);

		CDemoCopyHelper CopyHelper;
		CopyHelper.SetDataSources(&DBProTableHolderTo, &DBProTableHolderFrom);

		DWORD fltCustomers = fltNoFilter;
		CCopyIterator iterCustomers;
		CArray<Identity, Identity> arrCustomers;
		CString queryCustomers;
		GetDlgItemText(IDC_EDIT_CUSTOMERS, queryCustomers);

		switch (m_nCustomersMode)
		{
		case MODE_CUSTOMERS_SELECT:
			fltCustomers = fltPrimaryKey;

			for (i = m_listCustomers.GetCount(); i--; )
				if (m_listCustomers.GetCheck(i))
				{
					DWORD id = m_listCustomers.GetItemData(i);
					WCHAR buf[7];
					radix40_to_ascii(buf, (Radix40*)&id, 6);
					arrCustomers.Add(DBProTableHolderFrom.GetIdentity(buf));
				}

			iterCustomers.SetData(&arrCustomers);
			break;
		case MODE_CUSTOMERS_QUERY:
			fltCustomers = (DWORD) (LPCTSTR) queryCustomers;
			break;
		}

		DWORD fltEmployees = fltNoFilter;
		CCopyIterator iterEmployees;
		CArray<Identity, Identity> arrEmployees;
		CString queryEmployees;
		GetDlgItemText(IDC_EDIT_EMPLOYEES, queryEmployees);

		switch (m_nEmployeesMode)
		{
		case MODE_EMPLOYEES_SELECT:
			fltEmployees = fltPrimaryKey;

			for (i = m_listEmployees.GetCount(); i--; )
				if (m_listEmployees.GetCheck(i))
				{
					arrEmployees.Add(m_listEmployees.GetItemData(i));
				}

			iterEmployees.SetData(&arrEmployees);
			break;
		case MODE_EMPLOYEES_QUERY:
			fltEmployees = (DWORD) (LPCTSTR) queryEmployees;
			break;
		}

		CopyHelper.Init(iterCustomers, fltCustomers,
			iterEmployees, fltEmployees, m_bCustomersOrEmployeesOrders);

		CDlgCopyDesign	dlg;
		dlg.Create(IDD_CopyDesign, this);
		dlg.CenterWindow();
		dlg.ShowWindow(SW_SHOW);

		if(CopyHelper.CopyTables(&dlg.m_ProgressBar))
		{
			bOK = true;

			AfxGetApp()->WriteProfileString(g_szQueries, g_szCustomers, queryCustomers);
			AfxGetApp()->WriteProfileString(g_szQueries, g_szEmployees, queryEmployees);		
		}

		connectionFrom->Close();
		connectionTo->Close();

	}
	catch (_com_error& e)
	{
		ReportComError(e);
		return false;
	}
	return bOK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -