在控制台应用程序中编译 SQL 报告

发布于 2024-10-16 05:35:44 字数 1566 浏览 1 评论 0原文

我必须编写一个控制台应用程序来在数据库上运行 SQL 查询。然后,应用程序必须获取此信息并将其编译成报告,将该报告导出为 pdf,然后通过电子邮件发送 pdf 报告。 (所有这一切都必须自动发生 - 我将使用 Windows Scheduler 在特定日期和时间运行此应用程序。)

这是到目前为止我所拥有的:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;

namespace SqlQueryReports
{
class Program
{
    static void Main(string[] args)
    {
        SqlConnection dataConnection = new SqlConnection();
        try
        {
            dataConnection.ConnectionString ="Data Source=MY-PC\\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True;Pooling=False";
            dataConnection.Open();

            SqlCommand dataCommand = new SqlCommand();
            dataCommand.Connection = dataConnection;

            dataCommand.CommandText = "SELECT Product_id,Product_name,Product_price FROM Product";
            Console.WriteLine("About to execute: {0}\n\n", dataCommand.CommandText);

            SqlDataReader dataReader = dataCommand.ExecuteReader();

            // Compile data into Report
            // Export Report to .pdf
            // Email .pdf report

            dataReader.Close();

            Console.WriteLine("DONE");
        }
        catch(SqlException e)
        {
            Console.WriteLine(e.Message);
        }

        finally
        {
            dataConnection.Close();
        }

    }       
}
}

我只需要知道如何:

  1. 使用此信息编译报告。
  2. 将此报告导出为 pdf
  3. 通过电子邮件发送 pdf 报告。

提前致谢!

I have to write a console application that would run a sql query on a database. The application then has to take this information and compile it into a report, export this report to pdf and then e-mail the pdf report. (All this must happen automatically – I am going to use Windows Scheduler to run this application on a specific date and time.)

Here is what I have so far:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;

namespace SqlQueryReports
{
class Program
{
    static void Main(string[] args)
    {
        SqlConnection dataConnection = new SqlConnection();
        try
        {
            dataConnection.ConnectionString ="Data Source=MY-PC\\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=True;Pooling=False";
            dataConnection.Open();

            SqlCommand dataCommand = new SqlCommand();
            dataCommand.Connection = dataConnection;

            dataCommand.CommandText = "SELECT Product_id,Product_name,Product_price FROM Product";
            Console.WriteLine("About to execute: {0}\n\n", dataCommand.CommandText);

            SqlDataReader dataReader = dataCommand.ExecuteReader();

            // Compile data into Report
            // Export Report to .pdf
            // Email .pdf report

            dataReader.Close();

            Console.WriteLine("DONE");
        }
        catch(SqlException e)
        {
            Console.WriteLine(e.Message);
        }

        finally
        {
            dataConnection.Close();
        }

    }       
}
}

I just need know how to:

  1. Compile a report with this information.
  2. Export this report to pdf
  3. Email the pdf report.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

战皆罪 2024-10-23 05:35:44

如果您想在用户友好的设计器中很好地设计报告,您可以使用 DevExpress 的 XtraReports 或任何其他第三方报告引擎,它们通常允许您绑定数据源并导出为 pdf(或 excel、html、png 等)在... )。

如果您想自己做所有事情,您可以使用表格(例如)格式化一种 HTML 文档,其中您实际上在 dataReader 字段和列中组成网格循环,那么您应该使用允许您创建 pdf 文档的任何组件最后,您可以使用 .NET Framework 内置的 Smtp Mailing 通过电子邮件发送 pdf。

if you want to design the report nicely in a user friendly designer you could use XtraReports from DevExpress, or any other third party Report Engine, they usually allow you to bind a DataSource and to export as pdf ( or excel, html, png and so on... ).

If you want to do everything yourself you can format a kind of HTML document with a table ( for example ) where you literally compose the grid looping in the dataReader fields and columns, then you should use any component which allows you to create a pdf document and finally you could use the built in Smtp Mailing of the .NET Framework to send the pdf via email.

甜味拾荒者 2024-10-23 05:35:44

真的希望您对此有更多的意见,因为我刚刚被赋予了基本上完全相同的任务。这是我迄今为止发现的可能有帮助的内容;只是一个 PDF 导出示例(来源:ASP Snippets)...

protected void ExportToPDF(object sender, EventArgs e)
{
    //Get the data from database into datatable
    string strQuery = "select CustomerID, ContactName, City, PostalCode"     +
        " from customers";
    SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);

//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
    "attachment;filename=DataTable.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End(); 

Really wish you had more input on this one, as I've just been given the exact same task basically. Here's what I've found thus far that might help; just a sample PDF export (source: ASP Snippets)...

protected void ExportToPDF(object sender, EventArgs e)
{
    //Get the data from database into datatable
    string strQuery = "select CustomerID, ContactName, City, PostalCode"     +
        " from customers";
    SqlCommand cmd = new SqlCommand(strQuery);
DataTable dt = GetData(cmd);

//Create a dummy GridView
GridView GridView1 = new GridView();
GridView1.AllowPaging = false;
GridView1.DataSource = dt;
GridView1.DataBind();

Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition",
    "attachment;filename=DataTable.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End(); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文