报表的装载和查看
一.使用CrystalReportViewer跟水晶报表Crystal Report的绑定浏览;
1.Solution Explorer/Add/Add New Item…/DataSet,按照提示建立DataSet;
2.Solution Explorer/Add/Add New Item…/Crystal Report,建立表文件;
3。然后再建立一个窗体文件 CrystalReport.aspx
并在文件中建立或从工具箱中拖入一个CrystalReportViewer
4。在CrystalReport.aspx.cs中写入下列代码:
// 设置连接信息
string myConnectionString = "data source=RAINFISH;initial catalog=car;persist security info=False;user id=sa;workstation id=RAINFISH;packet size=4096";
SqlConnection myConnection = new SqlConnection(myConnectionString);
string mySelectQuery = "select * from TbAutoDispatchBill";
SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySelectQuery, myConnection);
//DataSet1为用XML 架构定义语言 (XSD) 描述的DataSet架构
DataSet1 dsBill = new DataSet1();
// 连接到数据库、从数据库中获取数据然后断开数据库连接
myDataAdapter.Fill(dsBill,"TbAutoDispatchBill");
//实例化水晶报表
CrystalReport_Bill CR_Bill = new CrystalReport_Bill();
//将填充的数据集传递给报表
CR_Bill.SetDataSource(dsBill);
//设置报表源,将该报表对象绑定到 Web 窗体查看器
CrystalReportViewer1.ReportSource=CR_Bill;----------------------------
如果数据库密码不为空的话,则应该添加以下代码安全登陆:
ReportDocument Report = new ReportDocument();
Report.Load(Server.MapPath("cr1.rpt"));
//设置按名称指定的表的 TableLogOnInfo 对象。
TableLogOnInfo logOnInfo = new TableLogOnInfo();//对报表中的每个表进行循环
//报表中数据集的Table 对象的个数
for(int i=0;i==Report.DataBase.Tables.Count-1;i++)
{
// 为报表中的表设置连接信息。
// 设置服务器或 ODBC 数据源名称、数据库名称、
// logonInfo.ConnectionInfo.ServerName = server
// logonInfo.ConnectionInfo.DatabaseName = database //设置用于登录到数据源的用户名
logOnInfo.ConnectionInfo.UserId = "sa";
//设置用于登录到数据源的“密码”
logOnInfo.ConnectionInfo.Password = "yourpwd";
// 将连接信息应用于表。
Report.DataBase.Tables[i].ApplyLogOnInfo(logOnInfo);
}
CrystalReportViewer1.ReportSource = Report;
//当然如果密码为空的话也同样可以使用这种方法的-------------
具体的你自己去做调整了,以上是我自己整理出来的一些资料,都是测试通过的GOOD LUCK TO YOU!
一.使用CrystalReportViewer跟水晶报表Crystal Report的绑定浏览;
1.Solution Explorer/Add/Add New Item…/DataSet,按照提示建立DataSet;
2.Solution Explorer/Add/Add New Item…/Crystal Report,建立表文件;
3。然后再建立一个窗体文件 CrystalReport.aspx
并在文件中建立或从工具箱中拖入一个CrystalReportViewer
4。在CrystalReport.aspx.cs中写入下列代码:
// 设置连接信息
string myConnectionString = "data source=RAINFISH;initial catalog=car;persist security info=False;user id=sa;workstation id=RAINFISH;packet size=4096";
SqlConnection myConnection = new SqlConnection(myConnectionString);
string mySelectQuery = "select * from TbAutoDispatchBill";
SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySelectQuery, myConnection);
//DataSet1为用XML 架构定义语言 (XSD) 描述的DataSet架构
DataSet1 dsBill = new DataSet1();
// 连接到数据库、从数据库中获取数据然后断开数据库连接
myDataAdapter.Fill(dsBill,"TbAutoDispatchBill");
//实例化水晶报表
CrystalReport_Bill CR_Bill = new CrystalReport_Bill();
//将填充的数据集传递给报表
CR_Bill.SetDataSource(dsBill);
//设置报表源,将该报表对象绑定到 Web 窗体查看器
CrystalReportViewer1.ReportSource=CR_Bill;----------------------------
如果数据库密码不为空的话,则应该添加以下代码安全登陆:
ReportDocument Report = new ReportDocument();
Report.Load(Server.MapPath("cr1.rpt"));
//设置按名称指定的表的 TableLogOnInfo 对象。
TableLogOnInfo logOnInfo = new TableLogOnInfo();//对报表中的每个表进行循环
//报表中数据集的Table 对象的个数
for(int i=0;i==Report.DataBase.Tables.Count-1;i++)
{
// 为报表中的表设置连接信息。
// 设置服务器或 ODBC 数据源名称、数据库名称、
// logonInfo.ConnectionInfo.ServerName = server
// logonInfo.ConnectionInfo.DatabaseName = database //设置用于登录到数据源的用户名
logOnInfo.ConnectionInfo.UserId = "sa";
//设置用于登录到数据源的“密码”
logOnInfo.ConnectionInfo.Password = "yourpwd";
// 将连接信息应用于表。
Report.DataBase.Tables[i].ApplyLogOnInfo(logOnInfo);
}
CrystalReportViewer1.ReportSource = Report;
//当然如果密码为空的话也同样可以使用这种方法的-------------
具体的你自己去做调整了,以上是我自己整理出来的一些资料,都是测试通过的GOOD LUCK TO YOU!
Web报表我做成功了.但是我想要的是Windows窗体的报表!烦请指教!
这是Web应用程序的吧,但是在Windows应用程序中,有好大的不同吧?
原来我按书上做的,每次运行时,就弹出来一个对话框,让我输入服务器名/用户名/密码,但是总是通不过!
后来我就想用Web方式的连接方式,但是看了看,有好大的不同!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace WinReport
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//public CrystalReport1 oRpt = new CrystalReport1();
private CrystalDecisions.Windows.Forms.CrystalReportViewer crystalReportViewer1;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent(); //
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
} /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
} #region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.crystalReportViewer1 = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// crystalReportViewer1
//
this.crystalReportViewer1.ActiveViewIndex = -1;
this.crystalReportViewer1.Location = new System.Drawing.Point(8, 56);
this.crystalReportViewer1.Name = "crystalReportViewer1";
this.crystalReportViewer1.ReportSource = null;
this.crystalReportViewer1.Size = new System.Drawing.Size(712, 288);
this.crystalReportViewer1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(208, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(728, 349);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1,
this.crystalReportViewer1});
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false); }
#endregion /// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
} private void button1_Click(object sender, System.EventArgs e)
{
// 设置连接信息
string myConnectionString = "data source=CC;initial catalog=GlobalMarket;persist security info=False;user id=sa;workstation id=CC;packet size=4096";
SqlConnection myConnection = new SqlConnection(myConnectionString);
string mySelectQuery = "select * from Orders";
SqlDataAdapter myDataAdapter = new SqlDataAdapter(mySelectQuery, myConnection);
//DataSet1为用XML 架构定义语言 (XSD) 描述的DataSet架构
Dataset1 dsBill = new Dataset1();
// 连接到数据库、从数据库中获取数据然后断开数据库连接
myDataAdapter.Fill(dsBill,"Orders");
//实例化水晶报表
CrystalReport1 oRpt = new CrystalReport1();
//将填充的数据集传递给报表
oRpt.SetDataSource(dsBill);
//设置报表源,将该报表对象绑定到 Web 窗体查看器
crystalReportViewer1.ReportSource = oRpt; }
}
}