database 在运行时设置 Crystal Report 数据源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/591088/
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
Setting Crystal Report data source at runtime
提问by
When creating my Crystal Report I obviously set up a database and server connection that I use for development.
创建 Crystal Report 时,我显然设置了用于开发的数据库和服务器连接。
What I want to do now in my VB application is to dynamically set the database and server name to use with the reports. I have these values as the strings varServer and varDatabase.
我现在想要在我的 VB 应用程序中做的是动态设置数据库和服务器名称以与报告一起使用。我将这些值作为字符串 varServer 和 varDatabase。
Anyone know how to go about doing this?
有谁知道该怎么做?
Thanks in advance.
提前致谢。
P.S I have tried several online solutions but am having trouble with VB6.
PS 我已经尝试了几种在线解决方案,但在使用 VB6 时遇到了问题。
采纳答案by MicSim
This linkhas all information you want to know.
此链接包含您想知道的所有信息。
UPDATE:Here is a minimum working sample for integrated authentication with SQL Server. You should use the ConnectionProperties of the table object to set connection parameters.
更新:这是与 SQL Server 集成身份验证的最小工作示例。您应该使用表对象的 ConnectionProperties 来设置连接参数。
Dim app As New CRAXDDRT.Application
Dim rpt As CRAXDDRT.Report
Dim tbl As CRAXDDRT.DatabaseTable
Dim tbls As CRAXDDRT.DatabaseTables
Set rpt = app.OpenReport("C:\report\repotest.rpt")
For Each tbl In rpt.Database.Tables
tbl.ConnectionProperties.DeleteAll
tbl.ConnectionProperties.Add "Provider", "SQLOLEDB"
tbl.ConnectionProperties.Add "Data Source", "localhost"
tbl.ConnectionProperties.Add "Initial Catalog", "testdb"
tbl.ConnectionProperties.Add "Integrated Security", "True" ' cut for sql authentication
'tbl.ConnectionProperties.Add "User Id", "myuser" ' add for sql authentication
'tbl.ConnectionProperties.Add "Password", "mypass" ' add for sql authentication
Next tbl
'This removes the schema from the Database Table's Location property.
Set tbls = rpt.Database.Tables
For Each tbl In tbls
With tbl
.Location = .Name
End With
Next
'View the report
Viewer.ReportSource = rpt
Viewer.ViewReport
回答by Tasos
Great Work! thanks For the code! I change it a bit though because this part was not working:
做得好!谢谢你的代码!我稍微改变了一下,因为这部分不起作用:
'View the report
Viewer.ReportSource = rpt
Viewer.ViewReport
It had an error that could not find object
它有一个找不到对象的错误
So I did it like that:
所以我是这样做的:
With Me.CRViewer91
.ReportSource = rpt
.ViewReport
End With
回答by KenDog
One can also set the default provider for the report by adding this code bit.
还可以通过添加此代码位来设置报告的默认提供程序。
Dim MyProvider As Object = logOnInfo.ConnectionInfo.LogonProperties.LookupNameValuePair("Provider")
If IsNothing(MyProvider) = False Then
MyProvider.Value = "SQLOLEDB"
End If
回答by Bill Martin
What version of crystal are you using?
你用的是什么版本的水晶?
In the .net world, I generally pass the dataset to the report as emoreau says here.
在 .net 世界中,我通常将数据集传递给报告,正如 emoreau在此处所说的那样。
That way, your connection is set from code rather than crystal and can be stored in a global connection property. However, that's .net. I'm thinking that either the version of crystal you have should have similar functionality OR Emoreau may have an example of how to do it in VB6 in the version you're using.
这样,您的连接是从代码而不是水晶设置的,并且可以存储在全局连接属性中。然而,那是.net。我认为您拥有的水晶版本应该具有类似的功能,或者 Emoreau 可能有一个示例,说明如何在您使用的版本中的 VB6 中执行此操作。
Hope that get's you started.
希望你开始了。