簡體   English   中英

使用C#和Sql server創建Crystal報表

[英]Creating Crystal report with C# and Sql server

我正在使用Crystal報告為我的應用程序生成報告,這是我使用的代碼:

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和密碼,我試圖輸入空值但連接失敗。

問題出在哪兒??

您必須初始化DataSet類的實例,並使用其中的信息填充它

您的DataSet,因為Crystal報表Datasource基於數據集。 這是一個基本代碼

使用帶有數據集的Crystal報表:

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資源管理器創建水晶報告時,我們必須提供數據庫ex:“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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM