将文件上载到SQL [英] Uploading file to SQL
本文介绍了将文件上载到SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在C#(不是asp.net)中将doc,pdf ..文件上传到SQL?我不想上传为二进制(动态)
How can I upload doc,pdf.. files to SQL in C# (not asp.net)? I don't want to upload as binary(dynamic)
推荐答案
二进制可能是最好的 - 一些.DOC文件不仅仅是文本,它们还包括二进制数据。如果您将它们存储在文本字段中,稍后可能会因为字符集/翻译而出现问题。
将它们存储为VARBINARY(MAX)并不困难 - 所有您需要做的是使用参数化查询,就像您必须为文本文件所做的那样:
Binary is probably the best - some .DOC files are not just text, they include binary data as well. If you store them in a text field, you may get problems later because of character sets / translations.
Storing them as VARBINARY(MAX) isn't difficult - all you have to do is use a parameterized query, just like you would have to do for a text file:
using (SqlConnection con = new SqlConnection(strConnect))
{
con.Open();
using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myFileColumn) VALUES (@DATA)", con))
{
com.Parameters.AddWithValue("@DATA", File.ReadAllBytes(path));
com.ExecuteNonQuery();
}
}
这篇关于将文件上载到SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文