Creating Crystal report with C# and Sql server(使用 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 报表
基础教程推荐
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 获取C#保存对话框的文件路径 2022-01-01
- 如果条件可以为空 2022-01-01
- 将数据集转换为列表 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
