=============================创建_绑定数据库
1.拉模式
2.pull模式
首先建立一个Dataset对象
打开资源服务器,选择要使用的表 直接拖到Dataset中 即可自动生成DataTable
3.打开报表页的字段资源管理器选中“数据库字段”右击选择“数据库专家”
单击确定添加完毕后就会出现相应的数据字段将这些字段拖到报表中即可
4.在ReportView页面中添加代码:
private void crystalReportViewer1_Load(object sender, EventArgs e)
{
this.ShowReport();
}
private void ShowReport()
{
ReportDocument report;
string strConn = "Data Source=.;Initial Catalog=pubs;Persist Security Info=True;User ID=sa;Password=123";
SqlConnection conn = new SqlConnection(strConn);
string sql = "select top 10 * from jobs";
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
DataSet ds = new DataSet();
da.Fill(ds, "jobs");//表名必须要有,因dataset中添加的表有表名
report = new ReportDocument();
string filepath = Application.StartupPath + "//CrystalReport1.rpt"; //Application.StartupPath:应用程序的完整路径
report.Load(filepath);
report.SetDataSource(ds);
this.crystalReportViewer1.ReportSource = report;
}
注意:
1.需要添加的命名空间 using CrystalDecisions.CrystalReports.Engine;
2.datatable的名字一定要与报表中的Dataset中的一致
3CrystalReport1.rpt和CrystalReport1.cs要复制到bin/Debug目录下,方法:点击项目目录,f4属性中,策略中,切换不复制为复制即可