Crystal Reports datasource in Visual Studio

2019-09-14 17:07发布

问题:

Using Visual Studio 2005 with default install of Crystal Reports.

When I create a report, Crystal asks for the database, and I give it my development db. In the app, I create a DataTable and pass it to the report, which is then handed to the CR Viewer. At runtime, neither the report object, nor the viewer need to see the db since I have already retrieved the table.

When the app is handed to the users, everything works fine. They cannot see the development db.

Once, in the production office, I created a report at the production site, and instead of creating the Crystal Report by specifying the development db, I gave it the production db. Back in my own office, running in VS, the report tries to connect to the development db, and fails. (It's not visible.)

So, it's not a problem that they can't see my development database, but it is a problem that I can't see the production database.

Questions are: Does this arise because running in VS is different from running the installed app? Why does the report try to connect to the DB anyway? How do I control it?

回答1:

I think I'm following...

When you are looking at the .rpt in VS, right-click on Database Fields in the Field Explorer window and go to 'Set Datasource Location'. What does it show for your connection? Is it pointing to the Production db server? This setting will be stored in the .rpt file so this might explain your issue.



回答2:

When you create the report you should be able to do report.SetDataSouce(DataSet) and pass it your dataset that contains the tables that the report uses.



回答3:

Questions are: Does this arise because running in VS is different from running the installed app? Why does the report try to connect to the DB anyway? How do I control it?

you can control it according to your report content

if your report contain one table and and you passed this table to your report using DataTable , report will work normally

but report try to connect to the original DB location when your data table don't provide all report data, usually when your report contain 2 or more table

in this case you need to pass DATASET to report contain all tables

here my code using 2 tables form authors and titleauthor

'Build a SQL statement to query for the authors table 

Dim sqlString As String = "SELECT * FROM authors"

'Retrieve the data using the SQL statement 

adoOleDbDataAdapter = New OleDbDataAdapter(sqlString, adoOleDbConnection)

'Build a SQL statement to query for the titleauthor table 

sqlString = "SELECT * FROM titleauthor" 

'Retrieve the data using the SQL statement 

adoOleDbDataAdapter2 = New OleDbDataAdapter(sqlString, adoOleDbConnection) 

'Create a instance of a Dataset 

DataSet1 = New DataSet() 

'Fill the dataset with the data with author information 

adoOleDbDataAdapter.Fill(DataSet1, "authors") 

'Fill the dataset with the data with titleauthor information. 
'The table name used in the Fill method must be identical to the name 
'of the table in the report. 

adoOleDbDataAdapter2.Fill(DataSet1, "titleauthor") 

'Pass the dataset to the report 

crReportDocument.Database.Tables(0).SetDataSource(DataSet1) 

'View the report 

CrystalReportViewer1.ReportSource = crReportDocument