数据转换服务包进度对话框






4.67/5 (5投票s)
2001年12月4日
2分钟阅读

76548

2013
在运行时执行 DTS 包时显示进度对话框。
引言
Microsoft® SQL Server™ 数据转换服务 (DTS) 是一组图形工具和可编程对象,可用于从各种来源提取、转换和整合数据到一个或多个目标。本文将不详细介绍 DTS 的具体内容。如果您想了解更多关于 DTS 的详细信息,请参阅 Microsoft 网站。
当您通过 SQL Server 企业管理器使用 DTS 编辑器执行 DTS 包时,您会看到一个不错的对话框,显示 DTS 包中每个步骤的进度。当您在 C++ 代码中在运行时执行包时,这个漂亮的进度对话框不会显示。为了在运行时获得进度对话框,我创建了 CDTSProgressThreadDlg
。
CDTSProgressThreadDlg
模仿在企业管理器中用于执行 DTS 包的进度对话框。
注意 - 您可以使用 SQL NS 执行显示进度对话框的 DTS 包,但是您需要重新分发 SQL NS 组件。
使用 DTS COM 对象
使用以下 import
语句访问 DTS COM 对象,这应该在您的 stdafx.h 文件中完成。
#import "c:\mssql7\binn\resources\1033\dtspkg.rll"
使用 CDTSProgressThreadDlg
由于使用了工作线程,调用进程有点不太整洁。以下代码显示了如何执行 DTS 包
bOK = ProcessDTS(m_sPackageName, sCaption, "SQL Server", "FoxPro", CDTSProgressThreadDlg::SQL_SERVER_ICON, CDTSProgressThreadDlg::FOXPRO_ICON, bCancelled);
调用一个名为 ProcessDTS
的辅助函数。它需要 DTS 包名称、将显示在进度对话框上的标题、源数据库的描述、目标数据库的描述、用于源数据库的图标、用于目标数据库的图标,最后传递一个 bool
,以查看包是否被取消。
如果您查看 ProcessDTS
的代码,您会发现此函数调用了一个名为 ExecuteDTSFunctionWithProgressDialog
的静态函数。(这种类型的执行基于 PJ Naughter 基于线程的进度对话框。)ExecuteDTSFunctionWithProgressDialog
使用 CDTSProgressThreadDlg
。
bool CDTSDlg::ProcessDTS(const CString& sDTSName, const CString& sDTSTitle, const CString& sSourceDesc, const CString& sDestinationDesc, int nSourceIcon, int nDestinationIcon, bool& bCancelled) { USES_CONVERSION; DTS::_PackagePtr pDTSPackage; HRESULT hr; CInfo info; try { if(SUCCEEDED(hr = pDTSPackage.CreateInstance(__uuidof(DTS::Package)))) { info.m_pPackage = pDTSPackage; info.m_sDTSName = sDTSName; info.m_bOK = true; info.m_bCancelled = false; info.m_bSaveDTSPackage = false; info.m_sSourceDescription = sSourceDesc; info.m_sDestinationDescription = sDestinationDesc; info.m_nSourceIcon = nSourceIcon; info.m_nDestinationIcan = nDestinationIcon; info.m_sServerName = m_sServerName; info.m_sUsername = m_sUsername; info.m_sPassword = m_sPassword; info.m_bUseTrusteConenction = m_bUseTrusteConenction; DWORD dwFlags = PSTAT_CONFIRMCANCEL; CString sCancelPrompt = "Are you sure you want to cancel ?\n\n" "This will terminate when the connection to the " "current task terminates"; if(!ExecuteDTSFunctionWithProgressDialog(DoDTSProcess, _T(sDTSTitle), &info, dwFlags, _T(sCancelPrompt), THREAD_PRIORITY_NORMAL, this, pDTSPackage)) AfxMessageBox("User cancelled transfer"); } else { IUnknown* pIUnk = NULL; pDTSPackage->QueryInterface(IID_IUnknown, (LPVOID*)&pIUnk); _com_issue_errorex(hr, pIUnk, __uuidof(DTS::Package)); }pDTSPackage->UnInitialize(); } catch(_com_error e) { AfxMessageBox(ReportError(e)); } bCancelled = info.m_bCancelled; return info.m_bOK; }
DTS 包事件(连接点)
DTS::PackageEvents
COM 对象提供了有关包在执行时的状态的所有信息(事件)。此 COM 对象用于 CDTSProgressThreadDlg
。一旦 DTS 执行过程触发了一个事件,该信息就会被捕获到接收器对象 (CPackageSink
) 上,并在进度对话框的列表控件上更新。
结论
这个类可以在运行时轻松地用来执行 DTS 包。
参考和鸣谢
- PJ Naughter 基于线程的进度对话框。
- Darren Green 网站 - 获取更多 DTS 示例的最佳网站。