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" ));
如有转载请注明出处谢谢合作!!!
做一番一生引以为豪的事业;找一个一生荣辱与共的妻子;在有生之年报答帮过我的人;并有能力帮助需要帮助的人;
业务QQ:1013067596 Email:sufei.1013@163.com