阅读全文:并下载例子:http://www.cckan.net/forum.php?mod=viewthread&tid=175
1.把连接数据库的代码写在web.config文件中
在 <connectionStrings> </connectionStrings>中加入以下代码:
<add name="Data Source" connectionString="***.***.***.***" />
<add name="Initial Catalog" connectionString="******" />
<add name="User ID" connectionString="******" />
<add name="Password" connectionString="******" />
<add name="Initial Catalog" connectionString="******" />
<add name="User ID" connectionString="******" />
<add name="Password" connectionString="******" />
2.具体代码:
public class CrystalHelper
{
public void NoPasswd(CrystalReportViewer crystalReportViewer, string path1)
{
TableLogOnInfo logOnInfo = new TableLogOnInfo();
//这里必须事先申明一个ReportDocument对象 Report,同时加载数据报表
ReportDocument oRpt = new ReportDocument();
//获取.rpt文件真实路径
//path1 = Microsoft.SqlServer.Server.MapPath("~/Pages/ARC/ARCjp.rpt");
oRpt.Load(path1);
//从web.config中获取logOnInfo参数信息
string a, b, c, d;
//获取ServerName
a = ConfigurationManager.ConnectionStrings["Data Source"].ConnectionString;
//获取DatabaseName
b = ConfigurationManager.ConnectionStrings["Initial Catalog"].ConnectionString;
//获取UserId
c = ConfigurationManager.ConnectionStrings["User ID"].ConnectionString;
//获取password
d = ConfigurationManager.ConnectionStrings["Password"].ConnectionString;
//设置logOnInfo参数
logOnInfo.ConnectionInfo.ServerName = a;
logOnInfo.ConnectionInfo.DatabaseName = b;
logOnInfo.ConnectionInfo.UserID = c;
logOnInfo.ConnectionInfo.Password = d;
oRpt.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
//建立.rpt文件与CryStalReportviewer文件之间的连接
crystalReportViewer.ReportSource = oRpt;
}
}
{
public void NoPasswd(CrystalReportViewer crystalReportViewer, string path1)
{
TableLogOnInfo logOnInfo = new TableLogOnInfo();
//这里必须事先申明一个ReportDocument对象 Report,同时加载数据报表
ReportDocument oRpt = new ReportDocument();
//获取.rpt文件真实路径
//path1 = Microsoft.SqlServer.Server.MapPath("~/Pages/ARC/ARCjp.rpt");
oRpt.Load(path1);
//从web.config中获取logOnInfo参数信息
string a, b, c, d;
//获取ServerName
a = ConfigurationManager.ConnectionStrings["Data Source"].ConnectionString;
//获取DatabaseName
b = ConfigurationManager.ConnectionStrings["Initial Catalog"].ConnectionString;
//获取UserId
c = ConfigurationManager.ConnectionStrings["User ID"].ConnectionString;
//获取password
d = ConfigurationManager.ConnectionStrings["Password"].ConnectionString;
//设置logOnInfo参数
logOnInfo.ConnectionInfo.ServerName = a;
logOnInfo.ConnectionInfo.DatabaseName = b;
logOnInfo.ConnectionInfo.UserID = c;
logOnInfo.ConnectionInfo.Password = d;
oRpt.Database.Tables[0].ApplyLogOnInfo(logOnInfo);
//建立.rpt文件与CryStalReportviewer文件之间的连接
crystalReportViewer.ReportSource = oRpt;
}
}
3.在调用水晶报表文件的页面的***.cs页面中调用2中的方法:
CrystalHelper cry = new CrystalHelper();
cry.NoPasswd(CrystalReportViewer1, Server.MapPath("~/Pages/Arc/ArcJp.rpt"));
cry.NoPasswd(CrystalReportViewer1, Server.MapPath("~/Pages/Arc/ArcJp.rpt"));