水晶报表使用基本代码(拉模式)

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using CrystalDecisions.CrystalReports.Engine;

namespace WebApplication1.Basic.Reports
{
 /// <summary>
 /// Summary description for rptExchRefReport1.
 /// </summary>
 public class rptExchRefReport1 : System.Web.UI.Page
 {
  protected CrystalDecisions.Web.CrystalReportViewer CRV;
  protected System.Web.UI.WebControls.Label lblError;
  protected CrystalDecisions.CrystalReports.Engine.ReportDocument rptDoc;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   string userid="", pwd="", server="", database="";
   getConn(ref userid, ref pwd, ref server, ref database);
   string conStr = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
   SqlConnection con = new SqlConnection(conStr);
   SqlCommand cmd = new SqlCommand("Select Exch_Ref_Date, Exch_Ref_Rate From tms_Exc_Ref Order by Exch_Ref_Date Desc", con);
   SqlDataAdapter da = new SqlDataAdapter(cmd);
   DataSet ds = new DataSet();
   try
   {
    da.Fill(ds,"ExchangeRef");
    rptDoc.Load(Server.MapPath("rptExchangeRefReport.rpt"));
    rptDoc.SetDataSource(ds);
    rptDoc.SetDatabaseLogon(userid,pwd,server,database);
    CRV.ReportSource = rptDoc;
   }
   catch (LogOnException ex)
   {
    lblError.Text = ex.Message;
   }
   catch (Exception ex)
   {
    lblError.Text = ex.Message;
   }
   finally
   {
    ds.Dispose();
    da.Dispose();
    cmd.Dispose();
   }
   
  }
  private void getConn(ref string userid, ref string pwd, ref string server, ref string database)
  {
   string strCon = System.Configuration.ConfigurationSettings.AppSettings["connectionString"];
   int pos1,pos2, pos3;
   //To get user id from the connectionString property defined in Web.Config file
   pos1 = strCon.IndexOf("user id",0);
   pos2 = strCon.IndexOf("=",pos1);
   pos3 = strCon.IndexOf(";",pos2);
   userid = strCon.Substring(pos2+1,pos3-pos2-1);
   //To get password from the connectionString property defined in Web.Config file
   pos1 = strCon.IndexOf("pwd",0);
   pos2 = strCon.IndexOf("=",pos1);
   pos3 = strCon.IndexOf(";",pos2);
   pwd = strCon.Substring(pos2+1, pos3-pos2-1);
   //To get server name from the connectionString property defined in Web.Config file
   pos1 = strCon.IndexOf("server",0);
   pos2 = strCon.IndexOf("=",pos1);
   pos3 = strCon.IndexOf(";",pos2);
   server = strCon.Substring(pos2+1, pos3-pos2-1);
   //To get the database name from the connectionString defined in Web.Config file
   pos1 = strCon.IndexOf("initial catalog",0);
   pos2 = strCon.IndexOf("=",pos1);
   pos3 = strCon.IndexOf(";",pos2);
   database = strCon.Substring(pos2+1,pos3-pos2-1);
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: This call is required by the ASP.NET Web Form Designer.
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {   
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(rptExchRefReport1));
   this.rptDoc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
   //
   // rptDoc
   //
   this.rptDoc.FileName = "";
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值