Crystal Report无法连接到数据库

时间:2012-12-19 14:23:39

标签: vb.net visual-studio-2010 crystal-reports database-connection

我正在使用Visual Studio在VB.net中创建程序,某些表单使用Crystal Reports来显示PDF报告,但我遇到了数据库连接问题。 VB.net代码可以毫无问题地访问数据库,但是当表单显示报告时它会询问我的用户名和密码,如果我写它们就无法连接。应用程序和报告共享同一个数据库,我使用相同的数据进行连接,但Crystal Reports失败。你能救我吗?

3 个答案:

答案 0 :(得分:1)

这是我的一个代码片段(在C#中,但应该让你知道我是如何做到的):

    CrystalReportSource CrystalReportSource1 = new CrystalReportSource();
    CrystalReportViewer CrystalReportViewer1 = new CrystalReportViewer();

    CrystalReportViewer1.ReportSource = CrystalReportSource1;
    CrystalReportViewer1.EnableParameterPrompt = false;
    CrystalReportSource1.Report.FileName = "Report3.rpt";
    CrystalReportSource1.EnableCaching = false;

    CrystalReportSource1.ReportDocument.SetParameterValue(0, ponumber);
    CrystalReportSource1.ReportDocument.SetParameterValue(1, receiptno);



    TableLogOnInfo logOnInfo = new TableLogOnInfo();

    logOnInfo.ConnectionInfo.ServerName = ConfigurationManager.AppSettings["WarehouseReportServerName"];
    logOnInfo.ConnectionInfo.DatabaseName = ConfigurationManager.AppSettings["WarehouseReportDatabaseName"];
    logOnInfo.ConnectionInfo.UserID = ConfigurationManager.AppSettings["WarehouseReportUserID"];
    logOnInfo.ConnectionInfo.Password = ConfigurationManager.AppSettings["WarehouseReportPassword"];

    TableLogOnInfos infos = new TableLogOnInfos();
    infos.Add(logOnInfo);
    CrystalReportViewer1.LogOnInfo = infos;

    maindiv.Controls.Add(CrystalReportSource1);
    maindiv.Controls.Add(CrystalReportViewer1);


    CrystalReportViewer1.DataBind();

答案 1 :(得分:0)

虽然您说您正在使用设计师,但我会发布一些对我有用的代码,也许它会对您有所帮助:

       Dim cryRpt As New ReportDocument
       Dim strReportPath As String = 'The Path of the rpt file
       cryRpt.Load(strReportPath)
       cryRpt.SetDataSource(Me.crData) 'crData is a datatable with data for the report

       crvReport.ReportSource = cryRpt 'crvReport is the CrystalReportViewer in my form

答案 2 :(得分:0)

在“SetDataSource”

中设置时,检查数据中是否存在所有字段