报告要求数据库登录有关设置数据表作为数据源 [英] Report asking for database login on setting DataTable as DataSource

查看:138
本文介绍了报告要求数据库登录有关设置数据表作为数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

报表设计器中创建与ODBC(DSN)连接,直接连接到数据库设计者水晶报表。同一报告通过DSN通过一个WinForm(C#)应用程序执行,并提供了数据库服务器,数据库,用户ID和密码。

Report designer creates crystal report in designer with direct connection to database with ODBC (DSN) connection. Same reports are executed via a Winform(C#) application via DSN and provides Database Server, database, user ID and password.

我要做出这样的改变水晶报表目的。通过DSN的ReportDocument不应直接连接到数据库。相反,我们将通过调用相应的存储过程和参数 System.Data.DataTable 通过将业务数据。这数据表对象应该被用于填充/生成报表。

I need to make such changes to the Crystal Report object. ReportDocument should not directly connect to Database via DSN. Instead, we will bring data via service by calling respective stored procedure and parameters as System.Data.DataTable. This DataTable object should be used to populate/generate reports.

我得到从存储过程和参数信息 ReportDocument.DataBase.Tables [I] .Location ReportDocument.DataDefinition 分别为对象。与 ReportDocument.DataBase.Tables设置数据源后,[I] .SetDataSource(数据表),它仍然要求提供数据库/服务器和用户凭据连接到服务器。

I do get the stored procedure and parameter information from ReportDocument.DataBase.Tables[I].Location and ReportDocument.DataDefinition object respectively. After setting DataSource with ReportDocument.DataBase.Tables[I].SetDataSource(DataTable), it still ask for database/server and user credential to connect to server.

我们可以实现场景,并在内存中的表,而不是通过ODBC直接连接到数据库填充报告吗?

Can we achieve scenario and populate report with in-memory table instead of direct connection to database over ODBC?

推荐答案

您需要专注于两件事情:

Two things you need to focus on:


  1. 您正在连接到相同的服务器和数据库名称它被用来设计报表。

  2. 您正在连接到不同的数据库或服务器

  1. You are connecting to same server and database name which were used to design the report.
  2. You are connecting to a different database or server.

方案1:相同的服务器和数据库名称

在此情况下,您需要提供使用证书 SetDatabaseLogon 方法如下:

In this case you need to provide the credentials using SetDatabaseLogon method as follows

'crDoc1 is your ReportDocument
'dtDataTable is your DataTable

'set database logon info
crDoc1.SetDatabaseLogon("db_user_name", "db_password", "db_server_name_or_ip", "database_name");

'set DataTable as DataSource
crDoc1.SetDataSource(dtDataTable)

情景2:不同的服务器或数据库名称

在这种情况下,您需要使用提供的凭据 ApplyLogOnInfo

In this case you need to provide the credentials using ApplyLogOnInfo method

ConnectionInfo crConInfo = new ConnectionInfo();
TableLogOnInfo crTblLogonInfo = new TableLogOnInfo();

crConInfo.ServerName = "db_server_name_or_ip";
crConInfo.DatabaseName = "database_name";
crConInfo.UserID = "db_user_name";
crConInfo.Password = "db_password";  

'crDoc1 is your ReportDocument
'dtDataTable is your DataTable

'Set DataSource to your DataTable
crDoc1.SetDataSource(dtDataTable)

'after setting the DataSource apply Logon credentials to each table in ReportDocument
Tables CrTables = crDoc1.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in CrTables) {
    crTblLogonInfo = CrTable.LogOnInfo;
    crTblLogonInfo.ConnectionInfo = crConInfo;
    CrTable.ApplyLogOnInfo(crTblLogonInfo);
}
crDoc1.Refresh();
CrystalReportViewer1.ReportSource = crDoc1;

注:的如果你有任何的子报表,您需要申请 SetDatabaseLogon 和/或 ApplyLogOnInfo 分别向所有的子报告及其表。

Notes: If you have any sub reports, you need to apply SetDatabaseLogon and/or ApplyLogOnInfo to all the sub reports and their tables respectively.

更新:结果
应用ApplyLogOnInfo子报表

UPDATE:
Applying ApplyLogOnInfo to subreports

foreach (ReportDocument srSubReport in crDoc1.Subreports) {
    foreach (CrystalDecisions.CrystalReports.Engine.Table CrTable in srSubReport.Database.Tables) {
        crTblLogonInfo = CrTable.LogOnInfo;
        crTblLogonInfo.ConnectionInfo = crConInfo;
        CrTable.ApplyLogOnInfo(crTblLogonInfo);
    }
}

这篇关于报告要求数据库登录有关设置数据表作为数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆