读书人

水晶报表9 VC 推模式,该如何解决

发布时间: 2012-02-07 17:45:36 作者: rapoo

水晶报表9 VC 推模式
按照网上的例子开发一个报表程序,CS模式。
为什么推模式每次设置数据源 程序就异常?

界面初始化的时候装载报表,点击OK的时候初始化数据,确认m_pRecordset中绝对有数据,为什么每次
SetDataSource(m_pRecordset.GetInterfacePtr());
就会异常呢?
void CTestRep::OnOK()
{
// TODO: Add extra validation here
m_pRecordset.CreateInstance("ADODB.Recordset");
HRESULT hr;
try
{
hr = m_pConnection.CreateInstance("ADODB.Connection");///创建Connection对象
if(SUCCEEDED(hr))
{
hr = m_pConnection->Open("Driver={MySQL ODBC 5.1 Driver};Server=192.168.1.47;Port=3306;Database=xxx;User=xxx; Password=xxx;","","",adModeUnknown);///连接数据库
///上面一句中连接字串中的Provider是针对ACCESS2000环境的,对于ACCESS97,需要改为:Provider=Microsoft.Jet.OLEDB.3.51; }
}
}
catch(_com_error e)///捕捉异常
{
CString errormessage;
errormessage.Format("连接数据库失败!\r\n错误信息:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
return;
}

m_pRecordset->Open("SELECT AuditTime,UserID,GroupByName,Sumize,NodeType,OperationType,ParentGroupID FROM audit_website",_variant_t((IDispatch*)m_pConnection,true),adOpenDynamic,adLockOptimistic,adCmdText);
while(!m_pRecordset->adoEOF)
{
_variant_t vID;
vID = m_pRecordset->GetCollect("UserID");
MessageBox((_bstr_t)vID);
m_pRecordset->MoveNext();
}
m_Report->Database->SetDataSource(m_pRecordset.GetInterfacePtr());
m_CRViewer.Refresh();
}
int CTestRep::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;

// TODO: Add your specialized creation code here
return 0;

}
BOOL CTestRep::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: Add extra initialization here
m_Application.CreateInstance (__uuidof(Application));
m_Report =m_Application->OpenReport (".\\config\\Report1.rpt");
m_Report->put_ReportTitle (_bstr_t("hehe"));
m_Report->PutReportAuthor ("");
m_CRViewer.SetDisplayToolbar(TRUE);
m_CRViewer.SetReportSource(m_Report);
m_CRViewer.SetDisplayGroupTree (FALSE);
m_CRViewer.Refresh();
m_CRViewer.ViewReport();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}

void CTestRep::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);

// TODO: Add your message handler code here
}

void CTestRep::OnCancel()
{
// TODO: Add extra cleanup here

CDialog::OnCancel();
}

void CTestRep::OnButton1()
{
// TODO: Add your control notification handler code here


}


//附上audit_website.ttx文件内容


AuditTimeDate
UserIDString100
GroupByNameString100
SumizeNumber
NodeTypeNumber
OperationTypeNumber
ParentGroupIDString100



[解决办法]
一直在用这个
Print.h
====================================
/*********************************************************************
*
* CPrint
* Copyright (c) 2006 by xscansou
* All rights reserved.
* cnslong@163.com
* Description:Print
*
*********************************************************************/
//{{AFX_INCLUDES()
#include "crystalreportviewer9.h"
//}}AFX_INCLUDES
#if !defined(AFX_PRINT_H__BC265570_16D1_4775_A199_465E1A3C7883__INCLUDED_)
#define AFX_PRINT_H__BC265570_16D1_4775_A199_465E1A3C7883__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Print.h : header file
//

/////////////////////////////////////////////////////////////////////////////


// CPrint dialog

class CPrint : public CDialog
{
// Construction
public:
void SwitchPrint();
CString sContdition;//打印条件
CString sReport;//调用的报表文件
CString sOtherInformation;//其它信息
CPrint(CWnd* pParent = NULL); // standard constructor
void InitResultViewer();
IReportPtr m_Report;
IApplicationPtr m_Application;
_ConnectionPtr m_pConn;//数据库连接指针
_RecordsetPtr m_pRs;//记录集指针
// static UNIT OtherThread(LPVOID pParam);
// Dialog Data
//{{AFX_DATA(CPrint)
enum { IDD = IDD_PRINT };
CCrystalReportViewer9m_ReportViewer;
//}}AFX_DATA


// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CPrint)
public:
virtual BOOL PreTranslateMessage(MSG* pMsg);
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL

// Implementation
protected:

// Generated message map functions
//{{AFX_MSG(CPrint)
virtual BOOL OnInitDialog();
afx_msg void OnPrintreport();
//}}AFX_MSG
afx_msg LRESULT Print_SQL(WPARAM wParam,LPARAM lParam);
DECLARE_MESSAGE_MAP()
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_PRINT_H__BC265570_16D1_4775_A199_465E1A3C7883__INCLUDED_)

读书人网 >行业软件

热点推荐