如果有文件路径,如何在SQL Server数据库中保存文件?

时间:2021-04-03 16:28:00

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#桌面应用程序,我需要将文件保存到数据库中。我找到了一些文件选择器,它可以给我正确的文件路径。现在我有了一个问题,如何通过使用这个文件的路径将它保存到数据库中。

5 个解决方案

#1


5  

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)

#2


10  

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).

但是要小心,数据库并不是存储大量文件的好方法(您会遇到一些性能问题)。

#3


2  

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,您将希望尽快处理这些字符串。当你用完后立即清空!

But if you can, just upgrade to 2008 and use FILESTREAM.

但是如果可以的话,升级到2008并使用FILESTREAM。

#4


1  

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#功能的例子。

#5


0  

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.

你可以把文件放出来的过程倒过来。

#1


5  

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)

#2


10  

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).

但是要小心,数据库并不是存储大量文件的好方法(您会遇到一些性能问题)。

#3


2  

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,您将希望尽快处理这些字符串。当你用完后立即清空!

But if you can, just upgrade to 2008 and use FILESTREAM.

但是如果可以的话,升级到2008并使用FILESTREAM。

#4


1  

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#功能的例子。

#5


0  

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.

你可以把文件放出来的过程倒过来。