如何使用 LINQ to SQL 查看 crystal 报告中的数据

How to view data on crystal report using LINQ to SQL

我有 Form.cs,上面有 "crystal report viewer" 和 "button" 当我按下按钮时,将执行以下代码。但是当我 运行 这段代码时,出现以下错误 "Application has stopped working"。为什么?

 private void button1_Click(object sender, EventArgs e)
 {
    DateTime dt = DateTime.ParseExact("2012/11/11", "yyyy/MM/dd", CultureInfo.InvariantCulture);
    DataClasses1DataContext dc = new DataClasses1DataContext();
    var data = (from a in dc.GetTable<Voucher>()
               where a.V_Date == dt
               select new { a.V_No }).ToList();

    CrystalReport1 cr = new CrystalReport1();
    cr.Load(@"CrystalReport1.rpt");
    cr.SetDataSource(data);
    crystalReportViewer1.ReportSource = cr;
 }

此外,我使用的是 SQL 服务器 2012,当我创建 CrystalReport1 时,我选择了 "OLE DB >> Microsoft OLE DB Provider for SQL server >> Integrated Security >> Table >> Table field"

已编辑:

使用调试器我发现这一行有错误 cr.SetDataSource(Data);

An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll 

Additional information: Could not load file or assembly 'file:///C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0\Common\SAP BusinessObjects Enterprise XI 4.0\win32_x86\dotnet1\crdb_adoplus.dll' or one of its dependencies. The system cannot find the file specified.

我找到了答案,代码没有任何问题。在 App.config 文件中。我替换了这个

<startup > 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>

有了这个

<startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>

现在一切正常。 谢谢大家的帮助。

在解决方案资源管理器的 App.config 文件中,只需替换此行

<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>

有了这个

<startup useLegacyV2RuntimeActivationPolicy="true"> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>

它应该可以工作。