Save large binary data into sql DB with C#

浪子不回头ぞ 提交于 2019-12-11 18:28:40

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!