[转贴]水晶报表数据填充(一种推模式,两种拉模式)类库

#region 声明
//----------------------------------------------------------------------
//
//
// 作者: 李淼(Nick.Lee)
//
// 水晶报表数据填充(一种推模式,两种拉模式)类库//
//
// boyorgril@msn.com
//
//----------------------------------------------------------------------
#endregion

#region 数据填充水晶报表函数集合

  /// <summary>
  /// 使用Engine.Database填充CrystalReport,为oleDB,odbc使用,此例子为sqlserver,拉模式
  /// </summary>
  /// <param name="SeverName">服务器名称</param>
  /// <param name="DatabaseName">数据库名称</param>
  /// <param name="UserID">用户id</param>
  /// <param name="Password">用户密码</param>
  /// <param name="sender">水晶报表CrystalReport对象</param>
  /*注意:使用Engine.Database填充必须先在水晶报表中设置相关数据字段*/
  public void cryDBFill(string SeverName,string DatabaseName,string UserID,string  Password,ReportClass sender)
  {
   TableLogOnInfo crTableLogOnInfo = new TableLogOnInfo();
   ConnectionInfo crConnectionInfo = new ConnectionInfo();
   CrystalDecisions.CrystalReports.Engine.Database crDatabase;
   CrystalDecisions.CrystalReports.Engine.Tables crTables;
   //   CrystalDecisions.CrystalReports.Engine.Table crTable;
   //对象定义
   crConnectionInfo.ServerName = SeverName;
   crConnectionInfo.DatabaseName = DatabaseName;
   crConnectionInfo.UserID = UserID;
   crConnectionInfo.Password = Password;
   crDatabase = sender.Database;
   crTables = crDatabase.Tables;
   //水晶报表与数据库连接
   foreach(CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
   {
    crTableLogOnInfo = crTable.LogOnInfo;
    crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
    crTable.ApplyLogOnInfo(crTableLogOnInfo);
   }
   //循环表并进行填充
  }
  /// <summary>
  /// 使用dataset填充CrystalReport和ReportDocument,为sqlserver专用,推模式,可以多次填充使用不同的表名在一个数据集中
  /// </summary>
  /// <param name="sqlText">数据库查询字段</param>
  /// <param name="cnString">数据库连接字段</param>
  /// <param name="DataTableName">虚拟数据集名称</param>
  public void crySetFill(string sqlText,string cnString,string DataTableName)
  {
   //          建立数据集合(局部变量)
   System.Data.SqlClient.SqlConnection connection1=new System.Data.SqlClient.SqlConnection(cnString);
   System.Data.SqlClient.SqlDataAdapter  Com=new System.Data.SqlClient.SqlDataAdapter (sqlText,connection1);
   //给数据对象分配内存空间
   //给数据集合分配内存空间
   Com.Fill(Set1,DataTableName);
   
  }
  /// <summary>
  /// 实现多个数据集设定填充CrystalReport
  /// </summary>
  /// <param name="sender">水晶报表CrystalReport对象</param>
  public void rptReportClassFill(ReportClass sender)
  {
   //数据填充
   sender.SetDataSource(Set1);
   //首先使用crySetFill
   //print1.cryDSetFill("select * from Customers","server=localhost;uid=sa;pwd=sa;database=Northwind","Customers");
   //  申明公共类型,例如:
   //public  printClass print1=new printClass();
   //public CrystalReport2 Rep1=new CrystalReport2();
   //然后调用
   //rptReportClassFill(Rep1);
   //CrystalReportViewer1.ReportSource = Rep1;
   //CrystalReportViewer1.DataBind();

   // string sqlText="select * from customers";
   // string cnString="server=localhost;uid=sa;pwd=sa;database=northwind;";
   /*注意:使用dataset填充必须先建立一个可视的dataset.xsd,在其中添加element,并在水晶报表中显示相关数据字段*/
  }

  /// <summary>
  /// 实现多个数据集设定填充CrystalReport
  /// </summary>
  /// <param name="sender">水晶报表ReportDocument对象</param>
  public void rptReportDocumentFill(ReportDocument sender)
  {
   //数据填充
   sender.SetDataSource(Set1);
   //首先使用crySetFill
   //print1.cryDSetFill("select * from Customers","server=localhost;uid=sa;pwd=sa;database=Northwind","Customers");
   //  申明公共类型,例如:
   //public  printClass print1=new printClass();
   //public ReportDocument RD1= new ReportDocument();
   //然后调用
   //rptReportDocumentFill(RD1);
   //CrystalReportViewer1.ReportSource = RD1;
   //CrystalReportViewer1.DataBind();

   // string sqlText="select * from customers";
   // string cnString="server=localhost;uid=sa;pwd=sa;database=northwind;";
   /*注意:使用dataset填充必须先建立一个可视的dataset.xsd,在其中添加element,并在水晶报表中显示相关数据字段*/
  }

  /// <summary>
  ///  使用ReportDocument填充CrystalReport,为oleDB,odbc使用,此例子为sqlserver,拉模式
  /// </summary>
  /// <param name="SeverName">服务器名称</param>
  /// <param name="DatabaseName">数据库名称</param>
  /// <param name="UserID">用户id</param>
  /// <param name="Password">用户密码</param>
  /// <param name="fileName">获取指定路径的水晶报表文件名称</param>
  /// <param name="sender">水晶报表ReportDocument对象</param>
  public void cryDocumentFill(string SeverName,string DatabaseName,string UserID,string  Password,string fileName,ReportDocument sender)
  {    
   string path = System.Web.HttpContext.Current.Server.MapPath(fileName);
   sender.Load(path);

   TableLogOnInfo crTableLogOnInfo = new TableLogOnInfo();

   crTableLogOnInfo.ConnectionInfo.ServerName = SeverName;
   crTableLogOnInfo.ConnectionInfo.Password=Password;
   crTableLogOnInfo.ConnectionInfo.DatabaseName=DatabaseName;
   crTableLogOnInfo.ConnectionInfo.UserID =UserID;
   //设置连接参数
 
   //ReportDocument装载路径
   sender.Database.Tables[0].ApplyLogOnInfo(crTableLogOnInfo);
   //设置对应表连接参数
   //使用命名空间
   //using CrystalDecisions.CrystalReports.Engine;
   //using CrystalDecisions.Shared;
   /*注意:使用Engine.Database填充必须先在水晶报表中设置相关数据字段*/
   //  申明公共类型,例如:
   //public  printClass print1=new printClass();
   //public ReportDocument RD1= new ReportDocument();
   //然后调用
   //print1.cryDocumentFill("10.141.165.136","Northwind","sa","sa","Customers","*.rpt",RD1);
   //CrystalReportViewer1.ReportSource = RD1;
   //CrystalReportViewer1.DataBind();
  }

  #endregion

posted @ 2005-02-18 23:38  Benny Ng  阅读(618)  评论(0编辑  收藏  举报