在C#中加载Crystal报表时保存登录信息

时间:2016-12-01 04:21:29

标签: c# crystal-reports

我创建了一个可以正常运行的Crystal Report,但它需要输入登录信息。我用Google搜索并找到此代码,但它对我不起作用。任何带代码的建议都将受到高度赞赏。

private void frmPrintWIPReport_Load(object sender, EventArgs e)
{
    var cryRpt = new ReportDocument();
    var crtableLogoninfo = new TableLogOnInfo();
    var crConnectionInfo = new ConnectionInfo();
    Tables CrTables;

    cryRpt.Load("rptWIP.rpt");

    crConnectionInfo.ServerName = "192.168.40.253";
    crConnectionInfo.DatabaseName = "TNET";
    crConnectionInfo.IntegratedSecurity = true;
    crConnectionInfo.UserID = "sa";
    crConnectionInfo.Password = "******";

    CrTables = cryRpt.Database.Tables;
    foreach (Table CrTable in CrTables)
    {
        crtableLogoninfo = CrTable.LogOnInfo;
        crtableLogoninfo.ConnectionInfo = crConnectionInfo;
        CrTable.ApplyLogOnInfo(crtableLogoninfo);
    }

    crystalReportViewer1.ReportSource = cryRpt;
    crystalReportViewer1.Refresh();
}

1 个答案:

答案 0 :(得分:0)

你可以试试这个。我希望它和你的需要一样。

private void frmPrintWIPReport_Load(object sender, EventArgs e)
    {
        var cryRpt = new ReportDocument();
        var crtableLogoninfo = new TableLogOnInfo();
        var crConnectionInfo = new ConnectionInfo();
        Tables CrTables;

        cryRpt.Load("rptWIP.rpt");

        ConnectionInfo crConnectionInfo = new ConnectionInfo()
            {
                ServerName = @"192.168.40.253",
                DatabaseName = @"TNET",
                UserID=@"sa",
                Password=@"******"                
               //IntegratedSecurity = true
            };

        CrTables = cryRpt.Database.Tables;
        foreach (Table CrTable in CrTables)
        {
            crtableLogoninfo = CrTable.LogOnInfo;
            crtableLogoninfo.ConnectionInfo = crConnectionInfo;
            CrTable.ApplyLogOnInfo(crtableLogoninfo);
        }

        crystalReportViewer1.ReportSource = cryRpt;
        crystalReportViewer1.Refresh();
    }