开发者

run ssrs report and email it from .net code

开发者 https://www.devze.com 2023-01-08 21:05 出处:网络
I have a report that shows all errors (if there are any) from a process that runs every day. At the end of the process, I want to write some code to execute the report and email it. I am seeing how to

I have a report that shows all errors (if there are any) from a process that runs every day.

At the end of the process, I want to write some code to execute the report and email it. I am seeing how to email a report from code, but I can't seem to find anywhere that shows how to run the report from code.

I am using C# in vs 08, and the repor开发者_开发知识库t is from ssrs 08. Any assistance would be greatly appreciated!


Here is some vb code to render the report into bytes:

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

this shows the basic stuff for rendering a report in code. VS 2005 code.


if you aren't using ReportViewer, then call the Reporting Service web server (not report manager), and then email it.


I ended up creating a new subscription to the report which creates a job. I ran the job from code that executes the job(Taken from Microsoft)

Like this:

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();

By calling the job- which is a subscription - the report was rendered and emailed to the subscription recipients.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号