How I do it:
First you get the bytes from database:
private byte[] GetFile(string id)
{
using (SqlConnection con = new SqlConnection("CONNECTION STRING")
{
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "Select COLUMN from TABLE where id = @id";
cmd.Parameters.AddWithValue("@id", id);
con.Open();
return (byte[])cmd.ExecuteScalar();
}
}
}
Then save file:
var byteFile = GetFile(name,tableName, urn);
if (table.Extension == "pdf")
{
File.WriteAllBytes("PATH/NAME", byteFile);
}
else
{
string text = Encoding.Unicode.GetString(byteFile);
File.WriteAllText("PATH/NAME", text);
}
Sometimes you need write bytes (ie: pdf), sometime it is better to write text. For text don't forget to specify correct encoding.