我正在尝试包含一个预先创建的CrystalReport,它通过我们的WPF应用程序中的系统DataSource(由我们的设计师创建)链接到mysql数据库。然而,我遇到了障碍,无论何时在我们的应用程序中加载报告,它都会要求数据库登录,并且即使有效信息也无法登录。
我有以下代码:
string path = System.IO.Path.Combine(Environment.CurrentDirectory, @"FrontPageReport.rpt");
ReportDocument cryRpt = new ReportDocument();
cryRpt.Load(path);
ParameterFieldDefinitions crParameterFieldDefinitions;
ParameterFieldDefinition crParameterFieldDefinition;
ParameterValues crParameterValues = new ParameterValues();
ParameterDiscreteValue crParameterDiscreteValue = new ParameterDiscreteValue();
//System.Net.NetworkCredential networkCredentials = new System.Net.NetworkCredential("ShippingClient", "1234", "domain");
cryRpt.SetDatabaseLogon("ShippingClient", "1234");
crParameterDiscreteValue.Value = shipmentID;
crParameterFieldDefinitions = cryRpt.DataDefinition.ParameterFields;
crParameterFieldDefinition = crParameterFieldDefinitions["shipment_id"];
crParameterValues = crParameterFieldDefinition.CurrentValues;
crParameterValues.Clear();
crParameterValues.Add(crParameterDiscreteValue);
crParameterFieldDefinition.ApplyCurrentValues(crParameterValues);
FrontPageReport.ViewerCore.ReportSource = cryRpt;
我创建了一个数据源,它使用ODBC DataSource Administrator工具镜像CrystalReport的数据源。但是我仍然得到始终失败的登录弹出窗口。
有人可以帮忙吗?
编辑:
原来是与子报告没有任何关系实际上它似乎完全随机报告工作而且不起作用,都使用dame数据库,相同的表,相同的数据源,相同的登录。
如果我在Visual Studio中打开报告并单击“主报告预览”,我输入一个有效的参数,它就像一个魅力,所以为什么加载应用程序时它不会工作?
我错过了一些重要的东西吗?
答案 0 :(得分:0)
好的我有一个解决方案,它不是我心目中的理想解决方案,但它有效。基本上,您必须使用visual studio中的数据表替换报表的数据源,然后手动填充该数据表。
您创建一个数据集,并在其中创建一个数据表。然后向该数据表添加列,表示为报表返回的数据(例如,从select语句返回的列)
然后你使用类似下面的代码。请记住这是使用mysql,但对于sql服务器来说,这个过程不会有太大变化。
ReportDocument cryRpt = new ReportDocument();
MySqlConnection cn = new MySqlConnection("Server=IPADDRESS;Uid=USERNAME;Pwd=PASSWORD;Database=DATABASENAME;");
//This is where you run the sql to select, as you can see, we're using a procedure
MySqlDataAdapter da = new MySqlDataAdapter("CALL letter_head_address()", cn);
DataSet ds = new DataSet();
//This is where you fill the data table, the second parameter is the name of the data table you previously created.
da.Fill(ds, "my_dt");
cryRpt.Load(path);
cryRpt.DataSourceConnections.Clear();
cryRpt.SetDataSource(ds);
希望这会帮助其他人在路上,我会继续看看为什么原始问题会发生。
答案 1 :(得分:0)
我的问题是通过更改app.config文件来解决的:
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
为:
<startup useLegacyV2RuntimeActivationPolicy="true"><supportedRuntime version="v4.0"/></startup>