C# 如果有文件路径,如何将文件保存在 SQL Server 数据库中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2121441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to save file in SQL Server database if have file path?
提问by eomeroff
I am building some C# desktop application and I need to save file into database. I have come up with some file chooser which give me correct path of the file. Now I have question how to save that file into database by using its path.
我正在构建一些 C# 桌面应用程序,我需要将文件保存到数据库中。我想出了一些文件选择器,它为我提供了正确的文件路径。现在我有疑问如何使用其路径将该文件保存到数据库中。
采纳答案by Paul Creasey
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
int numBytes = new FileInfo(fileName).Length;
byte[] buff = br.ReadBytes(numBytes);
Then you upload it to the DB like anything else, I'm assume you are using a varbinary column (BLOB)
然后你像其他任何东西一样将它上传到数据库,我假设你使用的是 varbinary 列(BLOB)
回答by Pedro
It really depends on the type and size of the file. If it's a text file, then you could use File.ReadAllText()
to get a string that you can save in your database.
这实际上取决于文件的类型和大小。如果它是一个文本文件,那么您可以使用它File.ReadAllText()
来获取可以保存在数据库中的字符串。
If it's not a text file, then you could use File.ReadAllBytes()
to get the file's binary data, and then save that to your database.
如果它不是文本文件,那么您可以使用它File.ReadAllBytes()
来获取文件的二进制数据,然后将其保存到您的数据库中。
Be careful though, databases are not a great way to store heavy files (you'll run into some performance issues).
不过要小心,数据库不是存储大量文件的好方法(你会遇到一些性能问题)。
回答by Burt
You would need the file into a byte array then store this as a blob field in the database possible with the name you wanted to give the file and the file type.
您需要将文件放入一个字节数组中,然后将其作为 blob 字段存储在数据库中,并可能使用您想要为文件提供的名称和文件类型。
You could just reverse the process for putting the file out again.
您可以逆转再次放出文件的过程。
回答by AdaTheDev
If you're using SQL Server 2008, you could use FILESTREAM(getting started guide here). An example of using this functionality from C# is here.
如果您使用的是 SQL Server 2008,则可以使用FILESTREAM(此处为入门指南)。在 C# 中使用此功能的示例是here。
回答by AdaTheDev
So filestream would be it but since you're using SQL 2K5 you will have to do it the read into memory way; which consumes alot of resources.
所以文件流就是它,但是由于您使用的是 SQL 2K5,因此您必须以读入内存的方式进行;这消耗了大量资源。
First of the column type varchar(max) is your friend this give you ~2Gb of data to play with, which is pretty big for most uses.
第一个列类型 varchar(max) 是你的朋友,它给你大约 2Gb 的数据,这对于大多数用途来说是相当大的。
Next read the data into a byte array and convert it to a Base64String
接下来将数据读入字节数组并将其转换为 Base64String
FileInfo _fileInfo = new FileInfo(openFileDialog1.FileName);
if (_fileInfo.Length < 2147483647) //2147483647 - is the max size of the data 1.9gb
{
byte[] _fileData = new byte[_fileInfo.Length];
_fileInfo.OpenRead().Read(_fileData, 0, (int)_fileInfo.Length);
string _data = Convert.ToBase64String(_fileData);
}
else
{
MessageBox.Show("File is too large for database.");
}
And reverse the process to recover
并反转恢复过程
byte[] _fileData = Convert.FromBase64String(_data);
You'll want to dispose of those strings as quickly as possible by setting them to string.empty as soon as you have finished using them!
您将希望尽快处理这些字符串,方法是在使用完后将它们设置为 string.empty!
But if you can, just upgrade to 2008 and use FILESTREAM.
但如果可以,只需升级到 2008 并使用 FILESTREAM。