使用C#和Sql Server创建Crystal报表 [英] Creating Crystal report with C# and Sql server

查看:94
本文介绍了使用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

您的DataSet的信息填充它Crystal报表数据源基于数据集。这是使用带有数据集的Crystal报表的

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

的基本代码:

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆