Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Can anybody tell me how to upload word, pdf, excell etc file to SQL server 2005 and how to retrieve them.
Posted

Read them as an array of bytes:
C#
byte[] bytes = File.ReadAllBytes(@"D:\Temp\MyFile.doc");

Then insert them into your table:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("INSERT INTO myTable (myFileDataColumn) VALUES (@DATA)", con))
        {
        com.Parameters.AddWithValue("@DATA", bytes);
        com.ExecuteNonQuery();
        }
    }
To read them back is very, very similar:

C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT myFileDataColumn FROM myTable", con))
        {
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                byte[] bytes = (byte() reader["myFileDataColumn "];
                File.WriteAllBytes(@"D:\Temp\MyFileBackAgain.doc", bytes);
                }
            }
        }
    }
 
Share this answer
 
 
Share this answer
 
First thing that you need to learn then is google is your friend.
 
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