在运行时动态更改连接字符串 [英] Changing Connection String Dynamically at run-time

查看:77
本文介绍了在运行时动态更改连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Crystal Reports Viewer控件和运行时,我加载了用户想要显示的报表。 用户还将更改报告应在运行时运行的数据库。 不幸的是,报告将运行的数据库不是SQL服务器,因此我必须使用连接字符串(ODBC RDO连接)。

我的控件在C#程序中运行。 />我看过很多关于如何在运行时更新数据库连接的例子,但我无法让它为我工作。 每次我在运行时更新报表上的连接字符串时,报表都无法连接(我使用的是无DSN连接字符串)。 当报表在查看器控件中运行时,会弹出一个窗口,显示服务器名称,数据库名称,用户名和密码。 服务器名称字段中显示了我的整个连接字符串(尽管服务器名称和数据库字段是只读的)。

以下是我尝试用来更新连接字符串的代码:

样品#1:



 rd =  new  ReportDocument(); 

//加载报告
rd.Load(fileName,OpenReportMethod.OpenReportByDefault);
rd.Refresh();



//创建连接对象
ConnectionInfo connectionInfo = CreateConnection(Server,Port,ServerNamespace , 用户名密码);

//在报告中的每个表上设置连接信息
SetDBLogonForReport(connectionInfo,rd);
SetDBLogonForSubreports(connectionInfo,rd);



private static ConnectionInfo CreateConnection(< span style ="color:blue"> string 服务器,
int 端口,
string ServerNamespace,
string 用户名,
string 密码)

{

ConnectionInfo connectionInfo = new ConnectionInfo();

string connString = " DRIVER = InterSystems ODBC; SERVER =" +服务器
+ " ;; PORT =" +端口
+ " ; DATABASE =" + ServerNamespace
+ " ;; UID =" +用户名
+ " ;; PWD =" +密码;

connectionInfo.IntegratedSecurity = false ;
connectionInfo.UserID =用户名;
connectionInfo.Password =密码;
//在我看过的例子中,这是实际的连接字符串,而不仅仅是服务器名称


connectionInfo.ServerName = connString;
connectionInfo.DatabaseName = ServerNamespace;
connectionInfo.Type = ConnectionInfoType.CRQE;

return connectionInfo;

}

私人 静态 void SetDBLogonForReport(ConnectionInfo connectionInfo,ReportDocument reportDocument)
{
foreach (CrystalDecisions.CrystalReports.Engine 。表 in reportDocument.Database.Tables)

{
TableLogOnInfo tableLogonInfo = table.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
table.ApplyLogOnInfo(tableLogonInfo);
}
}

私人 静态 void SetDBLogonForSubreports(ConnectionInfo connectionInfo,ReportDocument reportDocument)
{
foreach (部分 in reportDocument.ReportDefinition.Sections)
{
foreach (ReportObject reportObject in section.ReportObjects)
{
if (reportObject.Kind == ReportObjectKind.SubreportObject)
{
SubreportObject subreportObject =(SubreportObject)reportObject;
ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName);
SetDBLogonForReport(connectionInfo,subReportDocument);
}
}
}
}

解决方案

< blockquote>

我在4年内从未能够以编程方式更新RDO连接。 我已经尝试了我发现的每个例子,并且我自己写了10-20个,没有工作。 零。


这就是说,如果我使用SQL Native Client或OLE DB,我已经以编程方式运行我的代码。 因此,如果您的数据库(不是SQL Server)具有OLE DB驱动程序,那么您可能要完成此操作。 这个答案很晚,但对于后人
我以为我会发布它。 更改RDO连接字符串(即ODBC)是完全破坏的。 ; P&NBSP;甚至SAP人员似乎都无法理解/提供工作样本(即使你声明RDO,他们也总是给你OLEDB / ADO示例,但这不是
工作)。 


Visual Studio for Crystal中的snippit是一样的,它适用于具有ADO连接的OLEDB / SQL Native Client,但它*不能用于RDO和连接字符串(DSN-less) &NBSP;我还没有和一个人谈过一个RDO连接
字符串在Crystal Report中成功更改(不使用客户端软件)。 


I am using the Crystal Reports Viewer control, and and runtime, I load the report that the user wants to display.  The user will also be changing which database the report should run against at runtime.  Unfortunately, the database that the report will be running against is NOT an SQL server, so I must use a connection string (ODBC RDO connection).

My control is running inside of a C# program.

I have seen many examples of how to update the database connection at runtime, but I am unable to get it to work for me.  Everytime I update the connection string on the report at runtime, the report fails to connect (I am using a DSN-less connection string).  When the report is run in the viewer control, I get a window that pops up showing the server name, database name, username, and password.  The server name field has my entire connection string displayed in it (although the server name and database fields are read-only).

Below is the code that I have tried using to update the connection string:

Sample #1:

rd = new ReportDocument();

// Load the report
rd.Load(fileName, OpenReportMethod.OpenReportByDefault);
rd.Refresh();



// Create the connection object
ConnectionInfo connectionInfo = CreateConnection(Server, Port, ServerNamespace, Username, Password);

// Set the connection info on each table in the report
SetDBLogonForReport(connectionInfo, rd);
SetDBLogonForSubreports(connectionInfo, rd);



private static ConnectionInfo CreateConnection(string Server,
int Port,
string ServerNamespace,
string Username,
string Password) { ConnectionInfo connectionInfo = new ConnectionInfo(); string connString = "DRIVER=InterSystems ODBC;SERVER=" + Server
+ ";PORT=" + Port
+ ";DATABASE=" + ServerNamespace
+ ";UID=" + Username
+ ";PWD=" + Password; connectionInfo.IntegratedSecurity = false; connectionInfo.UserID = Username; connectionInfo.Password = Password;
//In examples that I have seen, this is the actual connection string, not just the server name

connectionInfo.ServerName = connString;
connectionInfo.DatabaseName = ServerNamespace; connectionInfo.Type = ConnectionInfoType.CRQE; return connectionInfo; } private static void SetDBLogonForReport(ConnectionInfo connectionInfo, ReportDocument reportDocument) { foreach (CrystalDecisions.CrystalReports.Engine.Table table in reportDocument.Database.Tables) { TableLogOnInfo tableLogonInfo = table.LogOnInfo; tableLogonInfo.ConnectionInfo = connectionInfo; table.ApplyLogOnInfo(tableLogonInfo); } } private static void SetDBLogonForSubreports(ConnectionInfo connectionInfo, ReportDocument reportDocument) { foreach (Section section in reportDocument.ReportDefinition.Sections) { foreach (ReportObject reportObject in section.ReportObjects) { if (reportObject.Kind == ReportObjectKind.SubreportObject) { SubreportObject subreportObject = (SubreportObject)reportObject; ReportDocument subReportDocument = subreportObject.OpenSubreport(subreportObject.SubreportName); SetDBLogonForReport(connectionInfo, subReportDocument); } } } }

解决方案

I have never been able in 4 years to get the RDO connection to update programatically.  I've tried every example I've found and written 10-20 on my own, NONE work.  Zero.

That said, I have gotten my code to programatically work if I use the SQL Native Client or OLE DB.  So, if you're database (which isn't SQL Server) has an OLE DB driver then you maybe to accomplish this.  This answer is way late, but for posterity I thought I'd post it.  Changing an RDO connection string (that's ODBC) is flat out broke. ;P  Not even the SAP folks can seem to understand / provide a working sample (even though you state RDO, they always give you OLEDB/ADO example which just doesn't work). 

The snippit in Visual Studio for Crystal is the same way, it will work for OLEDB/SQL Native Client with an ADO connection, but it *won't* work with RDO and a connection string (DSN-less).  I haven't talked to a single person whose gotten an RDO connection string succesfully changed inside a Crystal Report (without using the client software). 


这篇关于在运行时动态更改连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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