Save large binary data into sql DB with C#
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.
开发者_C百科Thank you in advance
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.
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.
精彩评论