以下代码在调试时引发异常-
class Program
{
static void Main(string[] args)
{
//Customise parameters for render method
Warning[] warnings;
string[] streamIds;
string mimeType = "application/pdf";
string encoding = String.Empty;
string filenameExtension = String.Empty;
string deviceInfo = "<DeviceInfo>" + "<OutputFormat>PDF</OutputFormat>" + "<PageWidth>8.5in</PageWidth>" + "<PageHeight>11in</PageHeight>" + "<MarginTop>0.5in</MarginTop>" + "<MarginLeft>1in</MarginLeft>" + "<MarginRight>1in</MarginRight>" + "<MarginBottom>0.5in</MarginBottom>" + "</DeviceInfo>";
//Create a SqlConnection to the AdventureWorks2008R2 database.
SqlConnection connection = new SqlConnection("data source=localhost;initial catalog=AdventureWorks2008R2;integrated security=True");
//Create a SqlDataAdapter for the Sales.Customer table.
SqlDataAdapter adapter = new SqlDataAdapter();
// A table mapping names the DataTable.
adapter.TableMappings.Add("Table", "Sales.Customer");
// Open the connection.
connection.Open();
Console.WriteLine("nThe SqlConnection is open.");
// Create a SqlCommand to retrieve Suppliers data.
SqlCommand command = new SqlCommand("SELECT Sales.Customer.CustomerID,Sales.Customer.PersonID,Sales.Customer.StoreID,Sales.Customer.TerritoryID,Sales.Customer.AccountNumber,Sales.Customer.rowguid,Sales.Customer.ModifiedDate FROM Sales.Customer", connection);
command.CommandType = CommandType.Text;
// Set the SqlDataAdapter's SelectCommand.
adapter.SelectCommand = command;
command.ExecuteNonQuery();
// Fill the DataSet.
DataSet dataset = new DataSet("Sales.Customer");
adapter.Fill(dataset);
//set up Reportviewver
Microsoft.Reporting.WinForms.LocalReport viewer = new Microsoft.Reporting.WinForms.LocalReport();
viewer.ReportPath = @"C:Documents and Settingsmurali.madhavaMy DocumentsVisual Studio 2008ProjectsPdfReportGenerationPdfReportGenerationReport.rdlc";
//add data source.
viewer.DataSources.Clear();
viewer.DataSources.Add(new ReportDataSource("dataset", dataset.Tables[0]));
//Now render it to pdf
try
{
byte[] bytes = viewer.Render("PDF", deviceInfo, out mimeType, out encoding, out filenameExtension, out streamIds, out warnings);
using (System.IO.FileStream fs = new System.IO.FileStream("output.pdf", System.IO.FileMode.Create))
{
//file saved to bin directory
fs.Write(bytes, 0, bytes.Length);
}
//Save report to D:
// FileStream fsi = new FileStream(@"D:output.pdf", FileMode.Create);
}
catch (Exception e)
{
Console.WriteLine("nCHEY!!!this Exception encountered:", e);
}
// Close the connection.
connection.Close();
Console.WriteLine("nThe SqlConnection is closed.");
Console.ReadLine();
}
}
使用的Winform版本是9.0.0.0,我使用了Adventureworks2008R2.Exception说:"本地报告处理过程中发生错误"。如何遇到这种情况?
这里的问题出在语句上
viewer.DataSources.Add(new ReportDataSource("dataset", dataset.Tables[0]));
此处提到的"数据集"名称应与 .rdlc 报表中使用的数据集名称相同。单击报表元素属性以查看它使用的数据集。并提供来自查询数据集的完全相同的数据'数据集。表[0]'。它有效。