Click here to Skip to main content
15,670,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I upload doc,pdf.. files to SQL in C# (not asp.net)? I don't want to upload as binary(dynamic)
Posted

1 solution

Binary is probably the best - some .DOC files are not just text, they include binary data as well. If you store them in a text field, you may get problems later because of character sets / translations.

Storing them as VARBINARY(MAX) isn't difficult - all you have to do is use a parameterized query, just like you would have to do for a text file:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myFileColumn) VALUES (@DATA)", con))
        {
        com.Parameters.AddWithValue("@DATA", File.ReadAllBytes(path));
        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