C# 通过 Crystal Web 服务在 Crystal Server 上以编程方式设置 Crystal Report 的数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2074557/
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
Programmatically set the datasource for a Crystal Report on a Crystal Server via Crystal Web Services
提问by argyle
How can I change the data source (database server, username, password) that a Crystal report uses at runtime, running within a crystal server?
如何更改 Crystal 报表在运行时使用的、在 Crystal 服务器中运行的数据源(数据库服务器、用户名、密码)?
I have a crystal server and have uploaded reports that have a set datasource (SQL Server 2005 hosted on SERVER A, userA, passwordA). I would like to schedule reports to run using a different datasource (SQL Server 2005 hosted on SERVER B, userB, passwordB) from the c# client I've written.
我有一个水晶服务器,并上传了具有一组数据源的报告(SQL Server 2005 托管在 SERVER A、userA、passwordA 上)。我想安排报告使用我编写的 c# 客户端中的不同数据源(托管在 SERVER B、userB、passwordB 上的 SQL Server 2005)运行。
The c# client can schedule reports to run within the server using objects provided by the crystal webservices. I've been using the following 3 objects:
c# 客户端可以使用 Crystal Web 服务提供的对象安排报告在服务器内运行。我一直在使用以下 3 个对象:
BIPlatform
InfoObject
CrystalReport
Documentation on these objects can be found HERE
可以在此处找到有关这些对象的文档
回答by Robert Vukovi?
You should try to get some info about the ConnectionInfo Class
您应该尝试获取有关ConnectionInfo 类的一些信息
CrystalDecisions.Shared.ConnectionInfo myConnectionInfo =
new CrystalDecisions.Shared.ConnectionInfo();
myConnectionInfo.DatabaseName = "Database";
myConnectionInfo.UserID = "Username";
myConnectionInfo.Password = "P@ssword";
Programmatically Change a Crystal Reports datasource location- in VB but you can use one of the online translation tools to convert from VB to C#:
以编程方式更改 Crystal Reports 数据源位置- 在 VB 中,但您可以使用在线翻译工具之一从 VB 转换为 C#:
http://www.developerfusion.com/tools/convert/vb-to-csharp/http://www.carlosag.net/Tools/CodeTranslator/
http://converter.telerik.com/
http://www.developerfusion.com/tools/convert/vb-to-csharp/ http://www.carlosag.net/Tools/CodeTranslator/
http://converter.telerik.com/
回答by scope
1/30/2018 -Just wanted to add that you will need the CrystalDecisions.ReportAppServer.DataDefModel dll
1/30/2018 - 只是想补充一点,您将需要 CrystalDecisions.ReportAppServer.DataDefModel dll
I had the same problem, where i have a report that has MSAccess database connection and i needed to change that to SQLServer, it took 2 days to figure this out:
我遇到了同样的问题,我有一份报告有 MSAccess 数据库连接,我需要将其更改为 SQLServer,花了 2 天时间才弄明白:
reportDocument = new ReportDocument();
reportDocument.Load(reportFileName);
TableLogOnInfo tableLogOnInfo = ReportClass.GetSQLTableLogOnInfo(connectionProperties.DatabaseSource, connectionProperties.DatabaseName, connectionProperties.UserName, connectionProperties.Password);
for (int i = 0; i < reportDocument.Database.Tables.Count; i++)
{
Table table = reportDocument.Database.Tables[i];
table.ApplyLogOnInfo(tableLogOnInfo);
}
public static ConnectionInfo GetConnectionInfo(string serverName, string databaseName, string userID, string password)
{
ConnectionInfo connectionInfo = new ConnectionInfo();
connectionInfo.ServerName = serverName;
connectionInfo.DatabaseName = databaseName;
connectionInfo.UserID = userID;
connectionInfo.Password = password;
return connectionInfo;
}
public static TableLogOnInfo GetSQLTableLogOnInfo(string serverName, string databaseName, string userID, string password)
{
CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag connectionAttributes = new CrystalDecisions.ReportAppServer.DataDefModel.PropertyBag();
connectionAttributes.EnsureCapacity(11);
connectionAttributes.Add("Connect Timeout", "15");
connectionAttributes.Add("Data Source", serverName);
connectionAttributes.Add("General Timeout", "0");
connectionAttributes.Add("Initial Catalog", databaseName);
connectionAttributes.Add("Integrated Security", false);
connectionAttributes.Add("Locale Identifier", "1033");
connectionAttributes.Add("OLE DB Services", "-5");
connectionAttributes.Add("Provider", "SQLOLEDB");
connectionAttributes.Add("Tag with column collation when possible", "0");
connectionAttributes.Add("Use DSN Default Properties", false);
connectionAttributes.Add("Use Encryption for Data", "0");
DbConnectionAttributes attributes = new DbConnectionAttributes();
attributes.Collection.Add(new NameValuePair2("Database DLL", "crdb_ado.dll"));
attributes.Collection.Add(new NameValuePair2("QE_DatabaseName", databaseName));
attributes.Collection.Add(new NameValuePair2("QE_DatabaseType", "OLE DB (ADO)"));
attributes.Collection.Add(new NameValuePair2("QE_LogonProperties", connectionAttributes));
attributes.Collection.Add(new NameValuePair2("QE_ServerDescription", serverName));
attributes.Collection.Add(new NameValuePair2("SSO Enabled", false));
ConnectionInfo connectionInfo = ReportClass.GetConnectionInfo(serverName, databaseName, userID, password);
connectionInfo.Attributes = attributes;
connectionInfo.Type = ConnectionInfoType.SQL;
TableLogOnInfo tableLogOnInfo = new TableLogOnInfo();
tableLogOnInfo.ConnectionInfo = connectionInfo;
return tableLogOnInfo;
}
回答by xtds
CrystalReportSource1.Report = new Report { FileName = @"C:\test.rpt" };
CrystalReportSource1.ReportDocument.SetDatabaseLogon(user,pass,server,db)