使用C#和Sql server创建Crystal报表

时间:2015-01-07 09:55:14

标签: c# crystal-reports

我使用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和 密码,我试图输入空值但连接失败。

问题在哪里?

3 个答案:

答案 0 :(得分:2)

您必须初始化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();

答案 1 :(得分:0)

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

考虑这个exapmle并尝试解决您的问题here

答案 2 :(得分:0)

首先转到数据库专家并检查右侧是否有单个连接。如果有一个或多个连接删除不需要的连接,如访问连接,则复制并粘贴下面的代码,它完美地工作

 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();