做一个简单的水晶报表
VS2005 +自带水晶报表 +MS-sql
做一个简单的水晶报表的方法:
1. 创建一个CrystalTest的项目
2. 在项目中添加CrystalReport1.rpt的报表
3. 连接数据库
-->>点击CrystalReport1.rpt的报表的页面
-->>将数据库的字段拖到右边的详细资料中.
4. Web.config文件
<appSettings>
<add key="DBServerName" value="wj-3bwj"/>
<add key="DBUserID" value="sa"/>
<add key="DBPassword" value="WangJun2005"/>
5.在WEB页面中加入CrystalReportViewer 控件:<add key="DBServerName" value="wj-3bwj"/>
<add key="DBUserID" value="sa"/>
<add key="DBPassword" value="WangJun2005"/>
<CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
6. CS文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//别忘了加下面的两个引用
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
namespace CrystalDemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TableLogOnInfo logOnInfo = new TableLogOnInfo();
ReportDocument objRD = new ReportDocument();
//这里的服务器名,用户密码,用户名,数据库名都可以先在web.config文件中设置后再取来用.
string strDBServerName=ConfigurationSettings.AppSettings["DBServerName"].ToString();
string strDBUserID=ConfigurationSettings.AppSettings["DBUserID"].ToString();
string strDBPassword=ConfigurationSettings.AppSettings["DBPassword"].ToString();
logOnInfo.ConnectionInfo.ServerName = strDBServerName;
logOnInfo.ConnectionInfo.UserID=strDBUserID;
logOnInfo.ConnectionInfo.Password = strDBPassword;
string strPath = Server.MapPath("CrystalReport1.rpt");
objRD.Load(strPath);
objRD.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
CrystalReportViewer1.ReportSource = objRD;
}
}
}
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//别忘了加下面的两个引用
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
namespace CrystalDemo
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TableLogOnInfo logOnInfo = new TableLogOnInfo();
ReportDocument objRD = new ReportDocument();
//这里的服务器名,用户密码,用户名,数据库名都可以先在web.config文件中设置后再取来用.
string strDBServerName=ConfigurationSettings.AppSettings["DBServerName"].ToString();
string strDBUserID=ConfigurationSettings.AppSettings["DBUserID"].ToString();
string strDBPassword=ConfigurationSettings.AppSettings["DBPassword"].ToString();
logOnInfo.ConnectionInfo.ServerName = strDBServerName;
logOnInfo.ConnectionInfo.UserID=strDBUserID;
logOnInfo.ConnectionInfo.Password = strDBPassword;
string strPath = Server.MapPath("CrystalReport1.rpt");
objRD.Load(strPath);
objRD.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
CrystalReportViewer1.ReportSource = objRD;
}
}
}