简体   繁体   English

如何从.Net C#和SQL服务器windows应用导出PDF

[英]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.

相关问题 使用C#将表从Sql Server导出到PDF文件 - Export Table from Sql Server to PDF file using c# Export Filtered data from crystal report to pdf using c# windows form application - Export Filtered data from crystal report to pdf using c# windows form application 从和C#Windows应用程序进行SQL Server远程访问 - SQL server remote access from and C# windows application 无法从Windows应用程序C#连接到SQL Server数据库 - unable to connect to sql server database from windows application c# C#从SQL Server导出到Excel - C# export to excel from sql server 如何使用SQL Server为C#Windows应用程序创建安装程序包 - How to create a setup package for C# Windows application with SQL server 如何在C#Windows应用程序的图片框中从SQL Server数据库中检索多个图像? - How to retrieve multiple images from SQL Server database in picturebox in C# windows application? 如何授予SQL Server 2008数据库从C#Windows应用程序中的客户端系统插入数据的权限? - How to give the permissions to sql server 2008 database to insert the data from client system in c# windows application? 如何在C#中将图像从SQL Server 2008 R2检索到Datagridview [Windows应用程序] - How to retrieve image from SQL Server 2008 R2 to Datagridview in C# [Windows Application] 如何在 Windows Server 2012 或 2016 中的默认 Edge 浏览器中从 .net C# 应用程序显示 PDF 命名目标? - How do I display a PDF Named Destination in Windows Server 2012 or 2016 in default Edge Browser from a .net C# app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM