您的位置:首页 > 数据库

水晶报表项目运行时 去掉 输入连接数据库密码 的页面

xuhongfei111 2014-04-29 10:10 10 查看
1.把连接数据库的代码写在web.config文件中

在 <connectionStrings> </connectionStrings>中加入以下代码:

<add name="Data Source" connectionString="***.***.***.***" />

<add name="Initial Catalog" connectionString="******" />

<add name="User ID" connectionString="******" />

<add name="Password" connectionString="******" />

2.写一个CrystalHelper类

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;

}

}

3.在调用水晶报表文件下调用下面的语句

CrystalHelper cry = new CrystalHelper();

cry.NoPasswd(CrystalReportViewer1, Server.MapPath("~/Pages/Arc/ArcJp.rpt"));
标签: