RBECKMN
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.SqlC onnection
Dim TheSQLCommand As New System.Data.SqlClient.SqlC ommand
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.ExecuteNonQu ery()
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.
there are other fields that get inserted, and that happens with this code:
Dim TheSQLConnection As New System.Data.SqlClient.SqlC
Dim TheSQLCommand As New System.Data.SqlClient.SqlC
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.ExecuteNonQu
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.
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.
check out this article with a step by step guide .
http://aspdotnetcode.source-of-humor.com/Articles/ADO.Net/StoringBinaryDataImagesFilesSqlServerAdoNet.aspx
http://aspdotnetcode.source-of-humor.com/Articles/ADO.Net/StoringBinaryDataImagesFilesSqlServerAdoNet.aspx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.