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

来源:互联网 发布:海牙国际法院知乎 编辑:程序博客网 时间:2024/06/07 03:52

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"));
0 0