PDF, XLS, DOC, DOCX, it doesn't matter. The image is stored as binary on the SQL server. You just need to pull down the
byte[] array and store it to disk with the proper file extension. Here is an example:
//Download image
private void button2_Click(object sender, EventArgs e)
{
const string connStr = "Data Source=apex2006sql;Initial Catalog=ServManLeather;Integrated Security=True;";
const string query = "Select ImgNumber, FacilityImg From FacilityImg Where ImgNumber = 1000";
using (SqlConnection conn = new SqlConnection(connStr))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(query, conn))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
using (DataTable dt = new DataTable())
{
dt.Load(dr);
//File.WriteAllBytes(@"C:\picture.bmp", (byte[])dt.Rows[0]["FacilityImg"]);
using (MemoryStream ms = new MemoryStream((byte[])dt.Rows[0]["FacilityImg"]))
{
ms.Position = 0;
ms.Seek(0, SeekOrigin.Begin);
pictureBox1.Image = System.Drawing.Image.FromStream(ms);
ms.Flush();
ms.Close();
}
}
}
}
}
}
I notice in your code you are not using
using() blocks or calling
.Dispose() on classes that implement
IDisposable . You should read up on that to prevent leaking unmanaged resources
For you next question you need to use an
out parameter to indicate that the value will be set in the called method. Example:
private void button1_Click(object sender, EventArgs e)
{
int IDDocument;
GetID(out IDDocument);
MessageBox.Show(IDDocument.ToString());
}
private void GetID(out int Doc_ID)
{
Doc_ID = 2;
}