按照网上的例子开发一个报表程序,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文件内容
AuditTime Date
UserID String 100
GroupByName String 100
Sumize Number
NodeType Number
OperationType Number
ParentGroupID String 100
------解决方案--------------------
一直在用这个
Print.h
====================================
/*********************************************************************
*
* CPrint
* Copyright (c) 2006 by xscansou
* All rights reserved.
* [email protected]
* 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
//
/////////////////////////////////////////////////////////////////////////////