问题
I need a code to store (eventually large) files into DB using C#.
The solution I'm using is sth like: (I'm using varbinary(MAX) column in the DB)
1) Create SqlCommand
2) Create SqlParameter
3) Set parameter.Value = File.ReadAllBytes(filePath)
4) Execute SqlCommand
Is there any more effective solution? Since the file can be large, I'm affraid of performance problems, when reading all bytes into memory, and then storing them into DB.
Thank you in advance
回答1:
What you have is the best you can get with a varbinary(MAX)
column. While you can stream data out of the database, there's no way to stream it in.
回答2:
I'm facing the same issue right now. There is a way to sequentially write into varbinary fields. See this article: 'Download and Upload Images from SQL Server via ASP.NET MVC' http://www.codeproject.com/KB/aspnet/streamingblobhttp.aspx
It shows a way to use a command like:
UPDATE <table> SET <field>.WRITE(@data, null, null) WHERE ...
This allows writing chunks of data into your column without having to read an entire file.
来源:https://stackoverflow.com/questions/4716706/save-large-binary-data-into-sql-db-with-c-sharp