• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1615
  • Last Modified:

how to insert a file into a varbinary(MAX) field in SQL Server using .NET code

preferably using VB .Net, i'd like to insert a file (Word document, Excel, PDF, text) into a SQL database varbinary column, and also extract and save the file from the database.

there are other fields that get inserted, and that happens with this code:

          Dim TheSQLConnection As New System.Data.SqlClient.SqlConnection
          Dim TheSQLCommand As New System.Data.SqlClient.SqlCommand
          Dim TheInsertString as string

... open the connection using a connection string
... set the value of TheInsertString to an insert query

          TheSQLCommand.Connection = TheSQLConnection
          TheSQLCommand.CommandText = TheInsertString

i'm not sure how to add the file to the insert command, or if there is another way to get this done.

this is quite easy to do in MS Access, using the Attachment field type but seems to be more difficult to do with SQL Server.
1 Solution
Alpesh PatelAssistant ConsultantCommented:
No way is same as you depicted. Just do the thing first. Read file in binary mode and save it to database field as you mentioned.
Aaron ShiloChief Database ArchitectCommented:
RBECKMNAuthor Commented:
i had this up and running in about 15 minutes.  the ASP suggestion would probably also work, but in this case, i'm not looking for a web-based application.  thanks for the help.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now