Search code examples
.netdatabasefilebinarysqlparameter

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.

Thank you in advance


Solution

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