Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi...


Can anyone Answer...


How to store word file as binary data into sql server.
Posted
Updated 9-Mar-12 23:44pm
v2
Comments
Richard MacCutchan 10-Mar-12 5:45am    
Use the varbinary type.

Easy!
Create your table field as VARBINARY(MAX).
Read the file as a sequence of bytes (either direct from your Upload control (if this is web based) or by using File:
C#
byte[] bytes = File.ReadAllBytes(path);

Then just insert it with a parametrized query:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myDocumentColumn) VALUES (@DOC)", con))
        {
        com.Parameters.AddWithValue("@DOC", bytes);
        com.ExecuteNonQuery();
        }
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900