Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

c# - How to set database login infos (connection info) for crystal report?

I have an application (NOT MY Coding) which have a lot of crystal reports. the problem I'm facing is that every time i open a crystal report it asks for login username and password.

after a little search i found that i have to set the connectioninfo for the report at run time and i found some solution but when i looked at the code of the application i didn't find it as i was expecting.

the frmviewrpt (the form that has the crystal report viewer) have some thing like this:

RptProBalance rptProductBalance = new RptProBalance();
rptProductBalance.RecordSelectionFormula = getBalanceRptSelection();
rptProductBalance.Refresh();
allReportViewer.ReportSource = rptProductBalance; 

the RptProBalance() (the cs file that is extended from the RptProBalance.rpt file):

//------------------------------------------------------------------------------
 // <auto-generated>
 //     This code was generated by a tool.
 //     Runtime Version:2.0.50727.42
 //
 //     Changes to this file may cause incorrect behavior and will be lost if
 //     the code is regenerated.
 // </auto-generated>
 //------------------------------------------------------------------------------

 namespace minfatora {
using System;
using System.ComponentModel;
using CrystalDecisions.Shared;
using CrystalDecisions.ReportSource;
using CrystalDecisions.CrystalReports.Engine;


public class RptProBalance : ReportClass {

    public RptProBalance() {
    }

    public override string ResourceName {
        get {
            return "RptProBalance.rpt";
        }
        set {
            // Do nothing
        }
    }

    [Browsable(false)]
    [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
    public CrystalDecisions.CrystalReports.Engine.Section ReportHeaderSection1 {
        get {
            return this.ReportDefinition.Sections[0];
        }
    }

    [Browsable(false)]
    [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
    public CrystalDecisions.CrystalReports.Engine.Section PageHeaderSection1 {
        get {
            return this.ReportDefinition.Sections[1];
        }
    }

    [Browsable(false)]
    [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
    public CrystalDecisions.CrystalReports.Engine.Section GroupHeaderSection1 {
        get {
            return this.ReportDefinition.Sections[2];
        }
    }

    [Browsable(false)]
    [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
    public CrystalDecisions.CrystalReports.Engine.Section DetailSection1 {
        get {
            return this.ReportDefinition.Sections[3];
        }
    }

    [Browsable(false)]
    [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
    public CrystalDecisions.CrystalReports.Engine.Section GroupFooterSection1 {
        get {
            return this.ReportDefinition.Sections[4];
        }
    }

    [Browsable(false)]
    [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
    public CrystalDecisions.CrystalReports.Engine.Section ReportFooterSection1 {
        get {
            return this.ReportDefinition.Sections[5];
        }
    }

    [Browsable(false)]
    [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
    public CrystalDecisions.CrystalReports.Engine.Section PageFooterSection1 {
        get {
            return this.ReportDefinition.Sections[6];
        }
    }
}

[System.Drawing.ToolboxBitmapAttribute(typeof(CrystalDecisions.Shared.ExportOptions), "report.bmp")]
public class CachedRptProBalance : Component, ICachedReport {

    public CachedRptProBalance() {
    }

    [Browsable(false)]
    [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
    public virtual bool IsCacheable {
        get {
            return true;
        }
        set {
            // 
        }
    }

    [Browsable(false)]
    [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
    public virtual bool ShareDBLogonInfo {
        get {
            return false;
        }
        set {
            // 
        }
    }

    [Browsable(false)]
    [DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Hidden)]
    public virtual System.TimeSpan CacheTimeOut {
        get {
            return CachedReportConstants.DEFAULT_TIMEOUT;
        }
        set {
            // 
        }
    }

    public virtual CrystalDecisions.CrystalReports.Engine.ReportDocument CreateReport()      {
        RptProBalance rpt = new RptProBalance();
        rpt.Site = this.Site;
        return rpt;
    }

    public virtual string GetCustomizedCacheKey(RequestContext request) {
        String key = null;
        // // The following is the code used to generate the default
        // // cache key for caching report jobs in the ASP.NET Cache.
        // // Feel free to modify this code to suit your needs.
        // // Returning key == null causes the default cache key to
        // // be generated.
        // 
        // key = RequestContext.BuildCompleteCacheKey(
        //     request,
        //     null,       // sReportFilename
        //     this.GetType(),
        //     this.ShareDBLogonInfo );
        return key;
    }
}
 }

I have no clue where exactly I'm supposed to make the connection info and pass it to the report.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
ConnectionInfo crconnectioninfo = new ConnectionInfo();
    ReportDocument cryrpt = new ReportDocument();
    TableLogOnInfos crtablelogoninfos = new TableLogOnInfos();
    TableLogOnInfo crtablelogoninfo = new TableLogOnInfo();

    Tables CrTables;

    crconnectioninfo.ServerName = "localhost";
    crconnectioninfo.DatabaseName = "dbclients";
    crconnectioninfo.UserID = "ssssssss";
    crconnectioninfo.Password = "xxxxxxx";  


  cryrpt.Load(Application.StartupPath + "\rpts" + dealerInfo.ResourceName);

        CrTables = cryrpt.Database.Tables;

        foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables)
        {
            crtablelogoninfo = CrTable.LogOnInfo;
            crtablelogoninfo.ConnectionInfo = crconnectioninfo;
            CrTable.ApplyLogOnInfo(crtablelogoninfo);
        }


        cryrpt.RecordSelectionFormula = getCustInfoRptSelection();
        cryrpt.Refresh();

        allReportViewer.ReportSource = cryrpt;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...