使用 C# 和 Sql 服务器创建 Crystal 报表

本文关键字:创建 Crystal 报表 服务器 Sql 使用 | 更新日期: 2023-09-27 18:36:29

我正在使用 Crystal report 为我的应用程序生成报告,这是我使用的代码:

private void button5_Click(object sender, EventArgs e)
    {
        ReportDocument cryRpt = new ReportDocument();
        TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
        TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
        ConnectionInfo crConnectionInfo = new ConnectionInfo();
        Tables CrTables;
        cryRpt.Load("C:''Documents and Settings''Administrateur''Mes documents''MyApplication''MyApplication''CrystalReport1.rpt");
        crConnectionInfo.ServerName = ".''SQLEXPRESS";
        crConnectionInfo.DatabaseName = "database";
        crConnectionInfo.UserID = "";
        crConnectionInfo.Password = "";
        crConnectionInfo.IntegratedSecurity = true;
        CrTables = cryRpt.Database.Tables;
        foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
        {
            crtableLogoninfo = CrTable.LogOnInfo;
            crtableLogoninfo.ConnectionInfo = crConnectionInfo;
            CrTable.ApplyLogOnInfo(crtableLogoninfo);
        }
        cryRpt.SetDatabaseLogon("", "", ".''SQLEXPRESS", "database");
        crystalReportViewer1.ReportSource = cryRpt;
        crystalReportViewer1.Refresh();
    }

但是当我运行应用程序时,出现了一个登录屏幕,要求连接 ID 和密码,我尝试输入空值,但连接失败。

问题出在哪里??

使用 C# 和 Sql 服务器创建 Crystal 报表

您必须初始化 DataSet 类的实例,并用来自

您的数据集,因为水晶报表数据源基于数据集。这是

将水晶报表与数据集结合使用:

SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=.'SQLEXPRESS;AttachDbFilename=YOUR PATH'database.mdf;Integrated Security=True;User Instance=True";
        con.Open();
        string sql = "SELECT * FROM tablename";
        SqlDataAdapter dscmd = new SqlDataAdapter(sql, con);
        DataSet ds = new DataSet();
        dscmd.Fill(ds, "tablename");
        con.Close();
        CrystalReport1 objRpt = new CrystalReport1();
        objRpt.SetDataSource(ds.Tables["tablename"]);
        crystalReportViewer1.ReportSource = objRpt;
        crystalReportViewer1.Refresh();

当我们使用 Windows 资源管理器创建水晶报告时,我们必须提供数据库的位置,例如:"C:''data",然后一旦它完美运行,但如果我们更改数据库文件的位置或应用程序的位置,它会要求登录。刷新数据源也可以解决您的问题

考虑这个示例并尝试在此处解决您的问题

首先转到

数据库专家并检查右侧是否有单个连接.如果一个或多个连接删除不需要的连接,如访问连接,然后复制并粘贴以下代码,它工作得很好

 ReportDocument cryRpt = new ReportDocument();
            TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
            TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
            ConnectionInfo crConnectionInfo = new ConnectionInfo();
            Tables CrTables;
            string path = "" + Application.StartupPath + "''Mgc''Invoice.rpt";
            cryRpt.Load(path);
            cryRpt.SetParameterValue("billno", Program.billno);
            crConnectionInfo.UserID = "userid";
            crConnectionInfo.Password = "Password";
            crConnectionInfo.ServerName = "servernme";
            crConnectionInfo.DatabaseName = "database;

            CrTables = cryRpt.Database.Tables;
            foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
            {
                crtableLogoninfo = CrTable.LogOnInfo;
                crtableLogoninfo.ConnectionInfo = crConnectionInfo;
                CrTable.ApplyLogOnInfo(crtableLogoninfo);
            }

                crystalReportViewer1.ReportSource = cryRpt;
                crystalReportViewer1.Refresh();