Link to home
Start Free TrialLog in
Avatar of RBECKMN
RBECKMNFlag for United States of America

asked on

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
          TheSQLCommand.ExecuteNonQuery()

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.
Avatar of Alpesh Patel
Alpesh Patel
Flag of India image

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.
ASKER CERTIFIED SOLUTION
Avatar of Éric Moreau
Éric Moreau
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of RBECKMN

ASKER

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.