运行ssrs报告并通过电子邮件发送.net代码
问题描述:
我有一个报告,显示每天运行的进程中的所有错误(如果有的话)。
在这个过程结束时,我想编写一些代码来执行报告并发送电子邮件。我正在看到如何从代码发送电子邮件报告,但似乎无法找到任何显示如何从代码运行报告的地方。
我在C#中使用vs 08,报告来自ssrs 08.任何帮助将不胜感激!运行ssrs报告并通过电子邮件发送.net代码
答
我最终创建了创建新作业的报告的新订阅。 我跑从代码执行的任务(从微软获取)
像这样的工作:
SqlConnection jobConnection;
SqlCommand jobCommand;
SqlParameter jobReturnValue;
SqlParameter jobParameter;
int jobResult;
jobConnection = new SqlConnection("myconnectionstring");
jobCommand = new SqlCommand("sp_start_job", jobConnection);
jobCommand.CommandType = System.Data.CommandType.StoredProcedure;
jobReturnValue = new SqlParameter("@RETURN_VALUE", SqlDbType.Int);
jobReturnValue.Direction = ParameterDirection.ReturnValue;
jobCommand.Parameters.Add(jobReturnValue);
jobParameter = new SqlParameter("@job_name", SqlDbType.VarChar);
jobParameter.Direction = ParameterDirection.Input;
jobCommand.Parameters.Add(jobParameter);
jobParameter.Value = "name of the subscription job";
jobConnection.Open();
jobCommand.ExecuteNonQuery();
jobResult = (Int32)jobCommand.Parameters["@RETURN_VALUE"].Value;
jobConnection.Close();
switch (jobResult)
{
case 0:
Console.WriteLine("SQL Server Agent job, RunSISSPackage, started successfully.");
break;
default:
Console.WriteLine("SQL Server Agent job, RunSISSPackage, failed to start.");
break;
}
Console.Read();
通过调用这是一个订阅求职 - 该报告被渲染并通过电子邮件发送到订阅收件人。
答
下面是一些VB代码呈现报表转换为字节:
Private Sub ExportReport(ByVal ReportViewer)
reportType = "PDF"
deviceInfo = "<DeviceInfo><OutputFormat>PDF</OutputFormat><PageWidth>8.5in</PageWidth><PageHeight>11in</PageHeight><MarginTop>0.5in</MarginTop><MarginLeft>0.5in</MarginLeft><MarginRight>0.5in</MarginRight><MarginBottom>0.5in</MarginBottom></DeviceInfo>"
renderedBytes = ReportViewer.LocalReport.Render(reportType, deviceInfo, mimeType, Encoding, fileNameExtension, streams, warnings)
End Sub
Public Function CreateAttachment() As Attachment
Dim MemStr As New MemoryStream(renderedBytes)
Dim ContentType As New ContentType(mimeType)
Dim pdf As New Attachment(MemStr, ContentType)
pdf.Name = ReportName & "." & fileNameExtension
Return pdf
End Function
这显示了渲染代码报表中的基本的东西。 VS 2005代码。
我看到你正在使用一个reportviewer。我没有用户界面 - 我需要知道如何在没有reportViewer的情况下呈现/调用报表。 – sher1000 2010-07-23 21:50:19
我使用非可视化的报告查看器来渲染输出。您可以声明一个并在没有表单的情况下创建。 ssrs是否报告本地模式或服务器模式报告? – Decker97 2010-07-24 22:59:43