简体   繁体   English

在C#中检索SQL Server中的二进制字段

[英]Retrieve binary field in SQL Server in C#

I want to save a pdf file in a varbinary(MAX) type column, and I use a TableAdapter in SQL Server. 我想在varbinary(MAX)类型列中保存pdf文件,并在SQL Server中使用TableAdapter I wrote this code to insert: 我写了这段代码来插入:

DataSet1TableAdapters.Table1TableAdapter tblpdf = new DataSet1TableAdapters.Table1TableAdapter();

byte[] b = System.IO.File.ReadAllBytes(pdfDocument1.FilePath);
tblpdf.InsertQuery(b);

and it works correctly, but I have a problem retrieving the PDF fro the binary column. 并且它可以正常工作,但是从二进制列检索PDF时出现问题。 I use this code: 我使用以下代码:

byte[] c= (byte[]) table1TableAdapter.ScalarQuery(txtSearch.Text); //with txtsearch box i search a field

System.IO.File.WriteAllBytes(pdfDocument1.FilePath, c);

and I get an error 我得到一个错误

the path could not be null 路径不能为空

and I think it means that my "c" variable is null and ScalarQuery does not work correctly. 并且我认为这意味着我的“ c”变量为null, ScalarQuery无法正常工作。

Please help me 请帮我

@MarcB: Yes, you are right, i change this line: @MarcB:是的,您是对的,我更改此行:

System.IO.File.WriteAllBytes(pdfDocument1.FilePath, c); System.IO.File.WriteAllBytes(pdfDocument1.FilePath,c);

to this one: 对此:

System.IO.File.WriteAllBytes(@"D:\\serah.pdf", c); System.IO.File.WriteAllBytes(@“ D:\\ serah.pdf”,c);

and it works correctly. 它可以正常工作。

but now the question is how can i use "WriteAllBytes" to write c into a variable in the code, actually i want to do something like this: 但是现在的问题是我如何使用“ WriteAllBytes”将c写入代码中的变量,实际上我想做这样的事情:

System.IO.File.WriteAllBytes(ff, c); System.IO.File.WriteAllBytes(ff,c);

pdfDocument1.FilePath = ff; pdfDocument1.FilePath = ff;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM