问题是:出现这种情况是因为在 VS 中运行与运行已安装的应用程序不同吗?为什么报告仍然尝试连接到数据库?我该如何控制它?
您可以根据您的报告内容进行控制
如果您的报告包含一个表格,并且您使用 DataTable 将此表格传递给您的报告,则报告将正常工作
但是当您的数据表未提供所有报告数据时,通常当您的报告包含 2 个或更多表时,报告会尝试连接到原始数据库位置
在这种情况下,您需要传递 DATASET 以报告包含所有表
这里我的代码使用 2 个表格表单作者和标题作者
'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