【发布时间】:2021-09-22 17:54:48
【问题描述】:
我想显示一个我已经上传到我的 SQL Server 数据库中的.pdf,但我需要它显示在表单中并直接从数据库中显示(而不是将其保存到我的计算机中)。
我使用的是 SQL Server 2012 和 Visual Studio 2019。
我尝试使用 AxAcroPdf,但我不知道它是如何工作的。
DataTable dt = new DataTable();
SqlCommand show = new SqlCommand("SELECT documento FROM table WHERE p = '" + contentP + "'AND n = '" + contentN + "' AND documento is not null;", con);
SqlDataAdapter adapter = new SqlDataAdapter(show);
adapter.Fill(dt);
if (dt.Rows.Count > 0)
{
byte[] ap = (byte[])dt.Rows[0]["documento"];
MemoryStream ms = new MemoryStream(ap);
var axacropdf = new AcroPDFLib.AcroPDF();
axacropdf.LoadFile(ap.ToString());
axacropdf.setShowToolbar(true);
axacropdf.setView("Fit");
}
【问题讨论】:
-
SQL Injection alert - 您应该不将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入 - 查看Little Bobby Tables
-
@Cleptus 我用的是 PDFSharp,但我要试试那个库,谢谢
-
@luciacar 我告诉你 AcroPDF 不能做到这一点。 PDFSharp 看起来可以做到。 Check this PDFSharp answer
标签: c# database pdf sql-server-2012 visual-studio-2019