在Golang中将PDF文件保存到SQL Server中

huangapple go评论69阅读模式
英文:

Saving PDF file in SQL Server using Golang

问题

我在使用Golang中的存储过程将PDF文件保存到SQL Server数据库时遇到了问题。以下是代码:

tsql := fmt.Sprintf("DECLARE @tmp varbinary(max);"+
	"SET @tmp = CAST('%s' as varbinary(max));"+
	"EXEC BP_AddCorrespondenceIn @PatientID=1, @ContactName='Test', @Subject='First Test',"+
	"@Category='Report', @DocType='PDF', @Content = @tmp", content)

// 执行查询
rows, err := db().Query(tsql)

这里的content是[ ]byte类型。当我运行程序时,查询执行后出现了以下错误:

mssql: '3�Ze� #��!~T��ϔljQ*���f1-~L���^ը;s;���.�)�[P�hjDN��J�.1��W�Zt���xq�\r���ן�)N���=df' 是无效的名称,因为它包含了一个NULL字符或无效的Unicode字符。

谢谢!

英文:

I'm having issue in saving a PDF file in SQL Server database using stored procedure in Golang. Below is the code.

tsql := fmt.Sprintf("DECLARE @tmp varbinary(max);"+
	"SET @tmp = CAST('%s' as varbinary(max));"+
	"EXEC BP_AddCorrespondenceIn @PatientID=1, @ContactName='Test', @Subject='First Test',"+
	"@Category='Report', @DocType='PDF', @Content = @tmp", content)

// Execute query
rows, err := db().Query(tsql)

Here the content is the [ ]byte. When I run the program the query executes and I got the error below:

> mssql: '3�Ze�
#��!~T��ϔljQ*���f1-~L���^ը;s;���.�)�[P�hjDN��J�.1��W�Zt���xq�\r���ן�)N���=df' is an invalid name because it contains a NULL character or an invalid unicode character.

Thank you!

答案1

得分: 0

我通过将存储过程的执行方法更改为_, err := db().Exec(tsql, content),以及将varbinary(max)转换为tmp = CAST(? as varbinary(max))来解决了这个问题。

英文:

I fixed the problem by changing the stored procedure exec method to _, err := db().Exec(tsql, content) and varbinary(max) conversion to tmp = CAST(? as varbinary(max));

huangapple
  • 本文由 发表于 2022年4月21日 10:00:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/71948080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定