C# 带有水晶报告的数据库登录提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1132314/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:58:15  来源:igfitidea点击:

database login prompt with crystal reports

c#.netwinformscrystal-reports

提问by Anyul Rivas

I'm trying to display some reportwith some subreportsinside it, but every it shows the report it throws some dialog box asking for database connection. I'm using this code:

我试图显示一些报告,其中包含一些报告,但每次显示报告时都会抛出一些对话框,要求连接数据库。我正在使用此代码:

private void frmReporte_Load(object sender, System.EventArgs e)
    {
        Clave = ConfigurationSettings.AppSettings["Password"].ToString();
        NombreBD = ConfigurationSettings.AppSettings["CatalogBD"].ToString();
        NombreServidor = ConfigurationSettings.AppSettings["Servidor"].ToString(); ;
        UsuarioBD = ConfigurationSettings.AppSettings["UserID"].ToString();
        this.crtReportes.ReportSource = this.prepareReport();
    }
    public void imprimirReporte()
    {
        ReportDocument rpt = new ReportDocument();
        rpt.Load(mvarRutaReporte);
        rpt.SetDataSource(clsReportes.dsReporte);
        rpt.PrintToPrinter(1, false, 1, 1);
    }
    private ReportDocument prepareReport()
    {
        Sections crSections;
        ReportDocument crReportDocument, crSubreportDocument;
        SubreportObject crSubreportObject;
        ReportObjects crReportObjects;
        ConnectionInfo crConnectionInfo;
        Database crDatabase;
        Tables crTables;
        TableLogOnInfo crTableLogOnInfo;
        crReportDocument = new ReportDocument();
        crReportDocument.Load(RutaReporte);
        crReportDocument.SetDataSource(clsReportes.dsReporte.Tables[0]);
        crDatabase = crReportDocument.Database;
        crTables = crDatabase.Tables;
        crConnectionInfo = new ConnectionInfo();
        crConnectionInfo.ServerName = NombreServidor ;
        crConnectionInfo.DatabaseName = NombreBD;
        crConnectionInfo.UserID = UsuarioBD;
        crConnectionInfo.Password = Clave;
        foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)
        {
            crTableLogOnInfo = aTable.LogOnInfo;
            crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
            aTable.ApplyLogOnInfo(crTableLogOnInfo);
        }
        // Para los reportes que poseen subreportes 
        // pongo el objeto seccion del la seccion actual del reporte 
        crSections = crReportDocument.ReportDefinition.Sections;
        // busco en todas las secciones el objeto reporte
        foreach (Section crSection in crSections)
        {
            crReportObjects = crSection.ReportObjects;
            //busco en todos los reportes por subreportes
            foreach (ReportObject crReportObject in crReportObjects)
            {
                if (crReportObject.Kind == ReportObjectKind.SubreportObject)
                {
                    crSubreportObject = (SubreportObject)crReportObject;
                    //abro el subreporte y me logeo con los datos del reporte general
                    crSubreportDocument = crSubreportObject.OpenSubreport(crSubreportObject.SubreportName);
                    crDatabase = crSubreportDocument.Database;
                    crTables = crDatabase.Tables;
                    foreach (CrystalDecisions.CrystalReports.Engine.Table aTable in crTables)
                    {
                        crTableLogOnInfo = aTable.LogOnInfo;
                        crTableLogOnInfo.ConnectionInfo = crConnectionInfo;
                        aTable.ApplyLogOnInfo(crTableLogOnInfo);
                    }
                }
            }
        }
        return crReportDocument;
    }

回答by vcosk

In crystal you can specify whether to prompt for db login @ the time of report generation in Crystal Management Console (CMC) @ each report level. Login to CMC, open your report > Process > Database. At the bottom of the page you can specify "Prompt the user for database logon", "Use SSO context for database logon" or "Use same database logon as when report is run". Select the third option for using the credentials stored in the server.

在Crystal中可以指定是否在Crystal Management Console (CMC) @每个报表级别中@报表生成时间提示db登录。登录到 CMC,打开您的报告 > 流程 > 数据库。在页面底部,您可以指定“提示用户登录数据库”、“使用 SSO 上下文登录数据库”或“在运行报告时使用相同的数据库登录”。选择第三个选项以使用存储在服务器中的凭据。

回答by Ashish S...

You can just write report.SetDatabaseLogon("username", "pwd", @"server", "database"); give the login information then the database logon box will not appear when you will run the code.

你可以只写 report.SetDatabaseLogon("username", "pwd", @"server", "database"); 提供登录信息,那么当您运行代码时,数据库登录框将不会出现。

回答by Jeff Roe

I had a similar problem and it is solved now, so I'm adding this reply in case it might help someone else in my situation.

我有一个类似的问题,现在已经解决了,所以我添加了这个回复,以防它可能对我的情况有所帮助。

When setting the SQL Server login info for the report, make sure you include the servicename. So, for example, make sure you're giving Crystal "myserver\myservice" instead of just "myserver".

为报告设置 SQL Server 登录信息时,请确保包含服务名称。因此,例如,确保给 Crystal 提供“myserver\myservice”而不仅仅是“myserver”。

My program is able to access data from SQL Server using just "myserver", but Crystal needs to be given "myserver\myservice".

我的程序能够仅使用“myserver”访问 SQL Server 中的数据,但需要为 Crystal 提供“myserver\myservice”。

回答by gojimmypi

Some older Crystal Report files saved with prior versions of the SQL Native Client (e.g. Provider: SQLNCLI) will no longer be able to login if either the client or server disables the respective TLS protocol version. For example, if a report was saved with SQL Server 2005 (version 9.00.xx), then TLS 1.0 is needed. If this protocol is disabled, there's no intuitive error, simply a "Database logon failed" is seen programmatically. Client software including rendering in a web browser will be prompted to enter a password, even though the account may have proper SQL permissions.

如果客户端或服务器禁用了各自的 TLS 协议版本,一些使用早期版本的 SQL Native Client(例如提供程序:SQLNCLI)保存的旧版 Crystal Report 文件将不再能够登录。例如,如果使用 SQL Server 2005(版本 9.00.xx)保存报表,则需要 TLS 1.0。如果此协议被禁用,则不会出现直观错误,只会以编程方式看到“数据库登录失败”。即使帐户可能具有适当的 SQL 权限,也会提示客户端软件(包括在 Web 浏览器中呈现)输入密码。

If rendering reports in IIS applications, see a tool called IISCrypto from Nartac Software to easily turn on and off various protocols, ciphers, hashes, and key exchanges.

如果在 IIS 应用程序中呈现报告,请参阅来自 Nartac Software 的名为 IISCrypto 的工具,以轻松打开和关闭各种协议、密码、散列和密钥交换。

Manually editing the report file and using Database - Set Datasource Location when connecting with a more modern native client is one way to fix this so that obsolete and insecure protocols don't need to remain enabled.

手动编辑报告文件并使用数据库 - 在与更现代的本机客户端连接时设置数据源位置是解决此问题的一种方法,这样就不需要保持启用过时和不安全的协议。