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

Creating Crystal report with C# and Sql server(使用 C# 和 Sql server 创建 Crystal 报表)

本文介绍了使用 C# 和 Sql server 创建 Crystal 报表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Crystal report to generate reports to my application, this is the code i used:

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 和密码,我尝试输入空值,但连接失败.

But when i run the application, a login screen appeared and demands the connection Id and password, i tried to enter null values but the connection is failed.

问题出在哪里??

推荐答案

你必须初始化 DataSet 类的一个实例,并用来自的信息填充它

You have to initialize an instance of the DataSet class, and fill it with information from

您的数据集,因为 Crystal 报表数据源基于数据集.这是一个基本的代码

your DataSet because Crystal report Datasource is based on a dataset. This is a basic code of

将 Crystal 报表与数据集一起使用:

using Crystal report with Dataset:

SqlConnection con = new SqlConnection();
        con.ConnectionString = @"Data Source=.SQLEXPRESS;AttachDbFilename=YOUR PATHdatabase.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();

这篇关于使用 C# 和 Sql server 创建 Crystal 报表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 C# 和 Sql server 创建 Crystal 报表

基础教程推荐