使用的是水晶报表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 类型不匹配"
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 类型不匹配"
一个或多个 SQL 数据类型与方法自变量的数据类型不匹配。
push方式,不存在登录数据库的问题。这是crystal的一个bug,到crystal官方网站上去搜一下这个补丁:CR90DBEXWIN_EN.ZIP补丁在:http://support.crystaldecisions.com/communityCS/FilesAndUpdates/cr90dbexwin_en.zip.asp
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();
直接用setdatasource不行吗?