将扫描的 pdf 插入数据库 c#
Insert an scanned pdf into database c#
我正在尝试将 pdf PdfDocument doc
插入到包含扫描文档的数据库中。当我尝试添加它时,出现下一条消息错误:
System.Data.SqlClient.SqlException: 'Implicit conversion is not allowed from type nvarchar to varbinary(max). Use the function CONVERT to execute this query.'
我正在使用 Visual Studio 2019 和 SQL Server 2012。
SqlCommand add = new SqlCommand("UPDATE table SET documento = @documento WHERE p = @contentP AND n = @contentN;", con);
add.Parameters.AddWithValue("@documento", Convert.ToString(doc));
add.Parameters.AddWithValue("@contentP", contentP);
add.Parameters.AddWithValue("@contentN", contentN);
con.Open();
add.ExecuteNonQuery();
con.Close();
System.Data.SqlClient.SqlException: 'Implicit conversion is not allowed from type nvarchar to varbinary(max). Use the function CONVERT to execute this query.'
您的问题是因为您试图通过 Convert.ToString(doc)
将 NVARCHAR
压缩到 VARBINARY(MAX)
列中。您必须获得一个 byte[]
并将其用作您的参数值
要修复此错误,您需要将 PdfSharp.Pdf.PdfDocument
保存到 MemoryStream
,然后从流中获取 byte[]
,请参见下文。
MemoryStream stream = new MemoryStream();
doc.Save(stream, false);
byte[] bytes = stream.ToArray();
现在您可以使用 bytes
作为您的 documento
参数值。
我正在尝试将 pdf PdfDocument doc
插入到包含扫描文档的数据库中。当我尝试添加它时,出现下一条消息错误:
System.Data.SqlClient.SqlException: 'Implicit conversion is not allowed from type nvarchar to varbinary(max). Use the function CONVERT to execute this query.'
我正在使用 Visual Studio 2019 和 SQL Server 2012。
SqlCommand add = new SqlCommand("UPDATE table SET documento = @documento WHERE p = @contentP AND n = @contentN;", con);
add.Parameters.AddWithValue("@documento", Convert.ToString(doc));
add.Parameters.AddWithValue("@contentP", contentP);
add.Parameters.AddWithValue("@contentN", contentN);
con.Open();
add.ExecuteNonQuery();
con.Close();
System.Data.SqlClient.SqlException: 'Implicit conversion is not allowed from type nvarchar to varbinary(max). Use the function CONVERT to execute this query.'
您的问题是因为您试图通过 Convert.ToString(doc)
将 NVARCHAR
压缩到 VARBINARY(MAX)
列中。您必须获得一个 byte[]
并将其用作您的参数值
要修复此错误,您需要将 PdfSharp.Pdf.PdfDocument
保存到 MemoryStream
,然后从流中获取 byte[]
,请参见下文。
MemoryStream stream = new MemoryStream();
doc.Save(stream, false);
byte[] bytes = stream.ToArray();
现在您可以使用 bytes
作为您的 documento
参数值。