在控制台应用程序中编译 SQL 报告
我必须编写一个控制台应用程序来在数据库上运行 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();
}
}
}
}
我只需要知道如何:
- 使用此信息编译报告。
- 将此报告导出为 pdf
- 通过电子邮件发送 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:
- Compile a report with this information.
- Export this report to pdf
- Email the pdf report.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想在用户友好的设计器中很好地设计报告,您可以使用 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.
真的希望您对此有更多的意见,因为我刚刚被赋予了基本上完全相同的任务。这是我迄今为止发现的可能有帮助的内容;只是一个 PDF 导出示例(来源:ASP Snippets)...
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)...