C# 如何为水晶报表设置数据库登录信息(连接信息)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12002957/
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
How to set database login infos (connection info) for crystal report?
提问by medo ampir
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.
经过一番搜索后,我发现我必须在运行时为报告设置 connectioninfo,我找到了一些解决方案,但是当我查看应用程序的代码时,我没有像预期的那样找到它。
the frmviewrpt (the form that has the crystal report viewer) have some thing like this:
frmviewrpt(具有水晶报表查看器的表单)有这样的东西:
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):
RptProBalance()(从 RptProBalance.rpt 文件扩展而来的 cs 文件):
//------------------------------------------------------------------------------
// <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.
我不知道我应该在哪里制作连接信息并将其传递给报告。
采纳答案by medo ampir
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;
回答by Lee O.
It sounds like the datasource is set up in the .rpt file. Open the .rpt file in the crystal reports designer (or VS if you have the add on) and choose "verify database". Once you have supplied this info and saved, it shouldn't do that anymore. The password was probably changed since the original setup.
听起来数据源是在 .rpt 文件中设置的。在水晶报表设计器中打开 .rpt 文件(或 VS,如果你有插件)并选择“验证数据库”。一旦您提供了此信息并保存了,它就不应该再这样做了。自原始设置以来,密码可能已更改。
回答by b.pell
If you want to supply it via code, then in your case it looks like you'd go into that datasource that is calling your .rpt file and set it there. Here is an example from my blog in VB.Net. This code will go through all sub reports and the main report and update the connection strings (this assumes, that all reports/subreports are connecting to the same database, if not, you could use pieces of this code to update individual sub reports).
如果您想通过代码提供它,那么在您的情况下,您似乎会进入调用 .rpt 文件的数据源并将其设置在那里。这是我在 VB.Net 中的博客中的一个示例。此代码将遍历所有子报告和主报告并更新连接字符串(假设所有报告/子报告都连接到同一个数据库,如果没有,您可以使用此代码的片段来更新各个子报告)。
It would be used something like this (although, you're binding to a viewer probably and not exporting, so you could ignore that, this is just for example).
它会像这样使用(尽管您可能绑定到查看器而不是导出,因此您可以忽略它,这只是示例)。
Using rd As New ReportDocument
rd.Load("C:\Temp\CrystalReports\InternalAccountReport.rpt")
rd.ApplyNewServer("serverName or DSN", "databaseUsername", "databasePassword")
rd.ApplyParameters("AccountNumber=038PQRX922;", True)
rd.ExportToDisk(ExportFormatType.PortableDocFormat, "c:\temp\test.pdf")
rd.Close()
End Using
System.Diagnostics.Process.Start("c:\temp\test.pdf")
Here is the extension method module:
这是扩展方法模块:
Imports System.Runtime.CompilerServices
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.CrystalReports
Namespace Extensions
''' <summary>
''' A set of extension methods to make manually working with Crystal Reports easier.
''' </summary>
''' <remarks>
''' Pieces of this code started in March of 2004 and have evolved over the last 8 years.
''' </remarks>
Public Module CrystalReportExtensions
'*********************************************************************************************************************
'
' Module: CrystalReportExtensions
' Initial Date: 03/26/2004
' Last Updated: 05/22/2012
' Programmer(s): Blake Pell
'
'*********************************************************************************************************************
''' <summary>
''' Applies a new server name, SQL username and password to a ReportDocument. This method can be used with any number
''' of database providers.
''' </summary>
''' <remarks></remarks>
<Extension()> _
Public Sub ApplyNewServer(ByVal report As ReportDocument, serverName As String, username As String, password As String)
For Each subReport As ReportDocument In report.Subreports
For Each crTable As Table In subReport.Database.Tables
Dim loi As TableLogOnInfo = crTable.LogOnInfo
loi.ConnectionInfo.ServerName = serverName
loi.ConnectionInfo.UserID = username
loi.ConnectionInfo.Password = password
crTable.ApplyLogOnInfo(loi)
Next
Next
'Loop through each table in the report and apply the new login information (in our case, a DSN)
For Each crTable As Table In report.Database.Tables
Dim loi As TableLogOnInfo = crTable.LogOnInfo
loi.ConnectionInfo.ServerName = serverName
loi.ConnectionInfo.UserID = username
loi.ConnectionInfo.Password = password
crTable.ApplyLogOnInfo(loi)
'If your DatabaseName is changing at runtime, specify the table location.
'crTable.Location = ci.DatabaseName & ".dbo." & crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1)
Next
End Sub
''' <summary>
''' Applies a new server name to the ReportDocument. This method is SQL Server specific if integratedSecurity is True.
''' </summary>
''' <param name="report"></param>
''' <param name="serverName">The name of the new server.</param>
''' <param name="integratedSecurity">Whether or not to apply integrated security to the ReportDocument.</param>
''' <remarks></remarks>
<Extension()> _
Public Sub ApplyNewServer(report As ReportDocument, serverName As String, integratedSecurity As Boolean)
For Each subReport As ReportDocument In report.Subreports
For Each crTable As Table In subReport.Database.Tables
Dim loi As TableLogOnInfo = crTable.LogOnInfo
loi.ConnectionInfo.ServerName = serverName
If integratedSecurity = True Then
loi.ConnectionInfo.IntegratedSecurity = True
End If
crTable.ApplyLogOnInfo(loi)
Next
Next
'Loop through each table in the report and apply the new login information (in our case, a DSN)
For Each crTable As Table In report.Database.Tables
Dim loi As TableLogOnInfo = crTable.LogOnInfo
loi.ConnectionInfo.ServerName = serverName
If integratedSecurity = True Then
loi.ConnectionInfo.IntegratedSecurity = True
End If
crTable.ApplyLogOnInfo(loi)
'If your DatabaseName is changing at runtime, specify the table location.
'crTable.Location = ci.DatabaseName & ".dbo." & crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1)
Next
End Sub
''' <summary>
''' Applies a new database name to all of the tables in the Crystal Report. If you do not wish to use a schemaName, pass
''' a blank string in for it.
''' </summary>
''' <param name="report">The Crystal Report document.</param>
''' <param name="databaseName">The name of the database.</param>
''' <param name="schemaName">The schema name if necessary. If this is not needed, pass a blank in.</param>
''' <remarks>Depending on your database server, this may require a schema also. For instance, in SQL Server
''' you may need NorthWind.dbo. </remarks>
<Extension()> _
Public Sub ApplyNewDatabaseName(ByVal report As ReportDocument, databaseName As String, schemaName As String)
Dim prefix As String = ""
If schemaName <> "" Then
prefix = String.Format("{0}.{1}.", databaseName, schemaName)
Else
prefix = String.Format("{0}.", databaseName)
End If
'Loop through each table in the report and apply the new database name
For Each crTable As Table In report.Database.Tables
'If your DatabaseName is changing at runtime, specify the table location.
crTable.Location = String.Format("{0}{1}", prefix, crTable.Location.Substring(crTable.Location.LastIndexOf(".") + 1))
Next
End Sub
''' <summary>
''' Sets the Username, Password and ServerName property and/or the UseTrustedConnection property with the values listed in the
''' provided connection string. Currently, only Sql Server is supported for automatically loading values from a connection
''' string.
''' </summary>
''' <param name="sqlConnectionString"></param>
''' <remarks></remarks>
<Extension()> _
Public Sub ApplyCredentialsFromConnectionString(report As ReportDocument, ByVal sqlConnectionString As String)
' Apply the connection information from the web.config file.
Dim cb As New System.Data.SqlClient.SqlConnectionStringBuilder(sqlConnectionString)
If cb.IntegratedSecurity = False Then
ApplyNewServer(report, cb.DataSource, cb.UserID, cb.Password)
Else
ApplyNewServer(report, cb.DataSource, True)
End If
End Sub
''' <summary>
''' Checks to see if a parameter name exists in the reports parameter fields. This only checks the top level of
''' the report. The top level should propagate down any parameters that need to be passed down.
''' </summary>
''' <param name="paramName"></param>
''' <param name="report"></param>
''' <returns></returns>
''' <remarks></remarks>
<Extension()> _
Public Function DoesParameterExist(ByVal report As ReportDocument, ByVal paramName As String) As Boolean
If report Is Nothing Or report.ParameterFields Is Nothing Then
Return False
End If
For Each param As ParameterField In report.ParameterFields
If paramName = param.Name Then
Return True
End If
Next
Return False
End Function
''' <summary>
''' Takes a parameter string and places them in the corresponding parameters for the report. The parameter string must
''' be semi-colon delimited with the parameter inside of that delimited with an equal sign. E.g.<br /><br />
'''
''' <code>
''' lastName=Pell;startDate=1/1/2012;endDate=1/7/2012
''' </code>
'''
''' </summary>
''' <param name="report">The Crystal Reports ReportDocument object.</param>
''' <param name="parameters">A parameter string representing name/values. See the summary for usage.</param>
''' <remarks></remarks>
<Extension()> _
Public Sub ApplyParameters(report As ReportDocument, parameters As String)
ApplyParameters(report, parameters, False)
End Sub
''' <summary>
''' Takes a parameter string and places them in the corresponding parameters for the report. The parameter string must
''' be semi-colon delimited with the parameter inside of that delimited with an equal sign. E.g.<br /><br />
'''
''' <code>
''' lastName=Pell;startDate=1/1/2012;endDate=1/7/2012
''' </code>
'''
''' </summary>
''' <param name="report">The Crystal Reports ReportDocument object.</param>
''' <param name="parameters">A parameter string representing name/values. See the summary for usage.</param>
''' <param name="removeInvalidParameters">If True, parameters that don't exist in the Crystal Report will
''' be removed. If False, these parameters will be left in and an exception will be thrown listing
''' the offending parameter name.</param>
''' <remarks></remarks>
<Extension()> _
Public Sub ApplyParameters(report As ReportDocument, parameters As String, removeInvalidParameters As Boolean)
' No parameters (or valid parameters) were provided.
If String.IsNullOrEmpty(parameters) = True Or parameters.Contains("=") = False Then
Exit Sub
End If
' Get rid of any trailing or leading semi-colons that would mess up the splitting.
parameters = parameters.Trim(";")
' The list of parameters split out by the semi-colon delimiter
Dim parameterList As String() = parameters.Split(Chr(Asc(";")))
For Each parameter As String In parameterList
' nameValue(0) = Parameter Name, nameValue(0) = Value
Dim nameValue As String() = parameter.Split(Chr(Asc("=")))
' Validate that the parameter exists and throw a legit exception that describes it as opposed to the
' Crystal Report COM Exception that gives you little detail.
If report.DoesParameterExist(nameValue(0)) = False And removeInvalidParameters = False Then
Throw New Exception(String.Format("The parameter '{0}' does not exist in the Crystal Report.", nameValue(0)))
ElseIf report.DoesParameterExist(nameValue(0)) = False And removeInvalidParameters = True Then
Continue For
End If
' The ParameterFieldDefinition MUST be disposed of otherwise memory issues will occur, that's why
' we're going the "using" route. Using should Dispose of it even if an Exception occurs.
Using pfd As ParameterFieldDefinition = report.DataDefinition.ParameterFields.Item(nameValue(0))
Dim pValues As ParameterValues
Dim parm As ParameterDiscreteValue
pValues = New ParameterValues
parm = New ParameterDiscreteValue
parm.Value = nameValue(1)
pValues.Add(parm)
pfd.ApplyCurrentValues(pValues)
End Using
Next
End Sub
End Module
End Namespace

