2011-02-07 34 views
0

我必須編寫一個控制檯應用程序,它可以在數據庫上運行sql查詢。然後,應用程序必須將這些信息編譯成報告,將此報告導出爲PDF格式,然後通過電子郵件發送pdf報告。 (所有這一切都必須自動發生 - 我將使用Windows調度運行的具體日期和時間這個應用程序。)在控制檯應用程序中編譯SQL報告

這是我到目前爲止有:

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報告。

在此先感謝!

回答

0

如果你想在一個用戶友好的設計師中很好地設計你的報告,你可以使用DevExpress的XtraReports或任何其他第三方報表引擎,他們通常允許你綁定一個數據源並導出爲PDF(或Excel,HTML ,png等等...)。

如果你想自己做所有事情,你可以使用一個表格(例如),在dataReader字段和列中逐字地構成網格循環,然後你應該使用任何允許你的組件創建一個pdf文檔,最後你可以使用內置的.NET Framework的Smtp郵件通過電子郵件發送PDF。

0

真的希望你對這個有更多的意見,因爲我剛剛完成了基本相同的任務。以下是我迄今發現的可能有用的信息;只是一個樣本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(); 
相關問題