[英]How to export PDF from .Net C# and SQL server windows application
I want to export pdf file from my desktop application.我想从我的桌面应用程序导出 pdf 文件。 I tried following code snap我尝试了以下代码快照
private void button1_Click(object sender, EventArgs e)
{
Document doc = new Document();
PdfWriter.GetInstance(doc, new FileStream("myPdf.pdf",FileMode.Create));
doc.Open();
SqlConnection con = new SqlConnection(connectionString);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmd = new SqlCommand("SELECT b.date,a.TotalIncome,b.TotalExpense FROM (SELECT c.date, sum(c.amout) as TotalIncome FROM (SELECT name FROM category_tbl where type = 'Income') a, transactions c where c.type = a.name group by c.date) a, (SELECT c.date, sum(c.amout) as TotalExpense FROM (SELECT name FROM category_tbl where type = 'Expense') a, transactions c where c.type = a.name group by c.date) b", con);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Paragraph p1 = new Paragraph(dr.GetValue(0).ToString());
Paragraph p2 = new Paragraph(dr.GetValue(1).ToString());
Paragraph p3 = new Paragraph(dr.GetValue(2).ToString());
doc.Add(p1);
doc.Add(p2);
doc.Add(p3);
doc.Close();
MessageBox.Show("PDF create");
}
}
Then I got the following output. Here I got only 1st row in SQL output.然后我得到以下 output。这里我只得到 SQL output 中的第一行。
5/16/2022 12:00:00 AM
12000
120
But I need to get all results in the table which are come from the SQL query, like this但我需要获取表中来自 SQL 查询的所有结果,如下所示
date日期 | Income收入 | Expenses花费 |
---|---|---|
5/16/2022 12:00:00 AM 5/16/2022 上午 12:00:00 | 12000 12000 | 120 120 |
5/28/2022 12:00:00 AM 5/28/2022 上午 12:00:00 | 15000 15000 | 145 145 |
6/02/2022 12:00:00 AM 6/02/2022 上午 12:00:00 | 3200 3200 | 60 60 |
. . | . . | . . |
. . | . . | . . |
So, I want to know how to update the code for getting my achievement所以,我想知道如何更新获得成就的代码
Thank you!!!谢谢!!!
Your code executes reader.read() only once which means you will process only first row.您的代码只执行 reader.read() 一次,这意味着您将只处理第一行。 Try to use it this way尝试以这种方式使用它
while(dr.Read())
{
Paragraph p1 = new Paragraph(dr.GetValue(0).ToString());
Paragraph p2 = new Paragraph(dr.GetValue(1).ToString());
Paragraph p3 = new Paragraph(dr.GetValue(2).ToString());
doc.Add(p1);
doc.Add(p2);
doc.Add(p3);
}
doc.Close();
MessageBox.Show("PDF create");
Also consider adding using statement for your sql connection, or add con.Close() at the end.还可以考虑为 sql 连接添加 using 语句,或在末尾添加 con.Close() 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.