水晶报表学习记录之二(数据库登陆问题)

在使用到CrystalReportViewer的时候,有个选项是关于数据库连接的,每次在Web页面浏览的时候只要有关于数据库相关的,都首先需要先登陆数据库,那么我们能不能把这个讨厌的窗口去掉了呢?直接每次打开页面的时候就通过认证,然后就可以直接显示报表数据。

Default.aspx
-----------------------------------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %>
<%@ Register Assembly="CrystalDecisions.Web, Version=10.2.3600.0, Culture=neutral, PublicKeyToken=692fbea5521e1304" Namespace="CrystalDecisions.Web" TagPrefix="CR" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
    <link href="/aspnet_client/System_Web/2_0_50727/CrystalReportWebFormViewer3/css/default.css"
        rel="stylesheet" type="text/css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <CR:CrystalReportViewer ID="CrystalReportViewer1" runat="server" AutoDataBind="true" />
    </div>
    </form>
</body>
</html>
--------------------------------------------------------------------
Default.aspx.cs
--------------------------------------------------------------------
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;

private void Page_Init(object sender, EventArgs e)
    {
        ConfigCrystalReports();
    }
    private void ConfigCrystalReports()
    {
        ConnectionInfo ConnectionInfo = new ConnectionInfo();
        ConnectionInfo.ServerName = "服务器名称或IP";
        ConnectionInfo.DatabaseName = "数据库名称";
        ConnectionInfo.UserID = "登陆帐号";
        ConnectionInfo.Password = "登陆密码";
        string ReportPath = Server.MapPath("CrystalReport1.rpt(这个是你的报表名称)");
        CrystalReportViewer1.ReportSource = ReportPath;
        SetDbLoginForReport(ConnectionInfo);
    }

    private void SetDbLoginForReport(ConnectionInfo ConnectionInfo)
    {
        TableLogOnInfos tableLogOnInfos = CrystalReportViewer1.LogOnInfo;
        foreach (TableLogOnInfo tableLogOnInfo in tableLogOnInfos)
        {
            tableLogOnInfo.ConnectionInfo = ConnectionInfo;
        }
    }

原文地址:https://www.cnblogs.com/Apollo/p/338604.html