使用的是水晶报表10.0
VC6.0开发.我看了一个水晶报表的手动连数据库的例子.代码是这样写的:Dim m_Report As New CrystalReport1' The ADO connection to the local database.
Dim cnn1 As ADODB.Connection
Dim datcmd1 As ADODB.Command' *************************************************************
' Demonstrate the use of AddADOCommand by opening an ADO data command,
' adding the data source to the report, and then adding a field
' to the report that uses that data source.
'
Private Sub cmdADO_Click()
    Dim fld As FieldObject
    Dim strCnn As String
    
    ' Open the data connection
    Set cnn1 = New ADODB.Connection
    strCnn = "Provider=MSDASQL;Persist Security Info=False;Data Source=Xtreme Sample Database 9;Mode=Read"
    cnn1.Open strCnn    ' Create a new instance of an ADO command object
    Set datcmd1 = New ADODB.Command
    Set datcmd1.ActiveConnection = cnn1
    datcmd1.CommandText = "Customer"
    datcmd1.CommandType = adCmdTable    ' Add the datasource to the report
    m_Report.Database.AddADOCommand cnn1, datcmd1
    ' Add a new field object to the report and set the field object to use
    ' the new data source.
    Set fld = m_Report.Section3.AddFieldObject("{ado.Customer Name}", 0, 0)
    LoadReport
End Sub
于是我也写了一个这样的VC代码: try {
m_CRViewer1.Create(NULL, "rptviewer", WS_CHILD, CRect(0,0,140,150), param, ID_THISREPORT, NULL);
m_Report =m_Application->OpenReport (filename);

m_pConnection.CreateInstance(__uuidof(Connection)); //初始化Connection指针
m_pRecordset.CreateInstance(__uuidof(Recordset));//初始化Recordset指针


m_pConnection->Open("Provider=MSDAORA;Data Source=test;User ID=cdjj; Password=cdjj","","",0);

m_pCommand.CreateInstance(__uuidof(Command));
m_pCommand->CommandType=adCmdTable;
m_pCommand->CommandText="Customer";
m_pCommand->ActiveConnection = m_pConnection;  // 将库连接赋于它

//CComVariant
CComVariant pConnection(m_pConnection);
CComVariant pCommand(m_pCommand); m_Report->Database->AddADOCommand(pConnection,pCommand);

//
m_Report->Database ->Tables ->Item [1]->SetLogOnInfo("test","test","cdjj","cdjj");
m_Report->Database->AddOLEDBSource(_bstr_t("Provider=MSDAORA;Data Source=test;User ID=cdjj; Password=cdjj"),_bstr_t("T_REALTIME_GATEWAY"));
m_CRViewer1.SetDisplayBorder (FALSE);
m_CRViewer1.SetEnableExportButton(TRUE);

//与使用的控件联系
m_CRViewer1.SetReportSource(m_Report);
c.Restore();
}
catch (_com_error& e) {
HandleError(e);
}
但是运行到:m_Report->Database->AddADOCommand(pConnection,pCommand);的时候,会出一个异常,
e的值为:"0x80020005 类型不匹配"

解决方案 »

  1.   

    用于存储返回的属性值或者方法返回值的 Transact-SQL 局部变量的数据类型与属性或方法返回值的 Visual Basic 数据类型不匹配。或者,要求属性或方法返回值,但该属性或方法未返回值。
      

  2.   

    类型不匹配 (0x80020005): 
    一个或多个 SQL 数据类型与方法自变量的数据类型不匹配。
      

  3.   

    看来还真没有几个人会用VC去做一些水晶报表的开发的呀.这VC是不是不好做这个报表方面的事呢?唉.好郁闷哟.连一个这样的代码都很能找到.找到的也只是VB代码.不能用.这要如何是好哟?
      

  4.   

    .net对直接带上了Cristal Report
      

  5.   

    水晶报表使用dataset数据源提示“登录失败”,这是水晶报表的一个bug,需要打补丁。相关讨论:http://expert.csdn.net/Expert/topic/2173/2173961.xml 
    push方式,不存在登录数据库的问题。这是crystal的一个bug,到crystal官方网站上去搜一下这个补丁:CR90DBEXWIN_EN.ZIP补丁在:http://support.crystaldecisions.com/communityCS/FilesAndUpdates/cr90dbexwin_en.zip.asp
      

  6.   

    m_Report->Database->AddADOCommand(pConnection,pCommand);你为什么要这么做?我没有这么做过,所以不好说原因。但是你说出你的目的,也许有其他的办法。我猜测是不是要把setlogoninfo放在出错这句话的前面。
      

  7.   

    我看了一下文档,我想使用push的方式动态的梆定数据源。今天我看到了一下这样的例子。是使用的"水晶报表9.0" VC6.0开发的。主要是使用ADO连接一个Access数据库。把查询出来的记录集,然后动态的加载到一个新建的报表中。 // Create and instance of the application object and create a new report.

    theApp.pApplication.CreateInstance("CrystalRuntime.Application");
    theApp.pReport = theApp.pApplication->NewReport();
    // Create a connection and recordset object and open the recordset off
    // of Xtreme Sample Database DSN
    _bstr_t conn("Provider=MSDASQL.1;Persist Security Info=False;Data Source=Xtreme Sample Database"); pConnection.CreateInstance(__uuidof(Connection));
    pConnection->Open(conn, "", "", NULL);
    ::SysFreeString(conn);
    pRecordSet.CreateInstance(__uuidof(Recordset));
    pRecordSet->Open("Customer", _variant_t((IDispatch *)pConnection,true), adOpenKeyset, adLockBatchOptimistic, 8); // for use in place of optional arguments
    VARIANT dummy;
    VariantInit(&dummy);
    dummy.vt = VT_EMPTY; // add the database to the report
    theApp.pReport->Database->Tables->Add("", dummy, _variant_t((IDispatch *)pRecordSet,true), dummy, "p2smon.dll");
    // a section and fieldobject to place the some fields in the Details section
    ISectionPtr pSection;
    IFieldObjectPtr pFieldObject;

    // get the Detail section using "D" as the index
    _bstr_t section("D");
    pSection = theApp.pReport->Sections->GetItem(section);
    ::SysFreeString(section);

    // add Customer Name and Last Year's Sales to the report
    _bstr_t Field1("{ado.Customer Name}");
    _bstr_t Field2("{ado.Last Year's Sales}");
    pFieldObject = pSection->AddFieldObject(Field1, 600, 0);
    pFieldObject = pSection->AddFieldObject(Field2, 3720, 0);
    ::SysFreeString(Field1);
    ::SysFreeString(Field2); // a DatabaseFieldDefinition, FieldObject and Section object
    // to add a group to the report
    IDatabaseFieldDefinitionPtr pFieldDef;
    IFieldObjectPtr ptheField;
    ISectionPtr ptheSection;
    // add the group to the report - we're adding the 13th field from the Recordset
    pFieldDef = theApp.pReport->Database->Tables->GetItem(1)->Fields->GetItem(13);
    theApp.pReport->AddGroup(0, pFieldDef, crGCAnyValue, crAscendingOrder);
    // add the field to the Group Header (identify the section using "GH" as the index
    _bstr_t gh("GH");
    ptheSection = theApp.pReport->Sections->GetItem(gh);
    ::SysFreeString(gh);
    _bstr_t aField("{ado.Country}");
    ptheField = ptheSection->AddFieldObject(aField, 100, 0);
    ::SysFreeString(aField); // pass the report to the viewer and preview the report
    m_Viewer.SetReportSource(theApp.pReport);

    m_Viewer.ViewReport();
      

  8.   

    有点搞复杂了
    直接用setdatasource不行吗?