locked
how to read binary file from sqlserver2005 database? RRS feed

  • Question

  • User1575048405 posted

    Hi,

    i have a table fileupload with id,username,filename,filedata,uploaddate

    here i have uploaded successfully...but i cannot retrieve the binary file uploaded by using its id...

    can anybody tell me how to read binary file from database using c#.net3.5?

     

    Saturday, March 19, 2011 9:27 AM

Answers

  • User1575048405 posted

    Hi,

    Thanks for replies...

    i have solved this problem by using this code..

     protected void btndwnload_Click(object sender, EventArgs e)
    {
    try
    {
    string strFileName = System.IO.Path.GetFileName(FileUpload1.FileName);

    SqlConnection Con = new SqlConnection("");

    SqlCommand Cmd = new SqlCommand(
    "Select file from uploads Where fid="+ DropDownList1.SelectedValue+",Con);
    Con.Open();
    SqlDataReader dr = sqlCom.ExecuteReader();
    while (dr.Read())
    {
    byte[] fileBytes = (byte[])dr["file"];
    FileStream fs = new FileStream(@"D:\downloads\"+ strFileName,FileMode.Create);
    fs.Write(fileBytes, 0, fileBytes.Length);
    fs.Close();
    fs.Dispose();
    fileBytes = null;
    }
    dr.Close();
    Con.Close();
    dr.Dispose();
    Con.Dispose();
    Cmd.Dispose();
    lblresult.text="Downloaded...you can find in d:\download folder....";
    }
    catch (Exception ex)
    {
    lblresult.Text=ex.Message;
    }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 20, 2011 9:38 AM

All replies

  • User1182340765 posted

    If you are using a SqlDataReader, you may try this

    say you have SqlDataReader reader

    then

    byte[] imageData = reader.GetSqlBinary(i).Value;

    where i is the column index

    I think if you are using adapter this should be similar.

    Saturday, March 19, 2011 10:52 AM
  • User1575048405 posted

    hi,

    could you give me full code..so that it will be very useful to me...

     

    Saturday, March 19, 2011 10:09 PM
  • User1575048405 posted

    Hi,

    Thanks for replies...

    i have solved this problem by using this code..

     protected void btndwnload_Click(object sender, EventArgs e)
    {
    try
    {
    string strFileName = System.IO.Path.GetFileName(FileUpload1.FileName);

    SqlConnection Con = new SqlConnection("");

    SqlCommand Cmd = new SqlCommand(
    "Select file from uploads Where fid="+ DropDownList1.SelectedValue+",Con);
    Con.Open();
    SqlDataReader dr = sqlCom.ExecuteReader();
    while (dr.Read())
    {
    byte[] fileBytes = (byte[])dr["file"];
    FileStream fs = new FileStream(@"D:\downloads\"+ strFileName,FileMode.Create);
    fs.Write(fileBytes, 0, fileBytes.Length);
    fs.Close();
    fs.Dispose();
    fileBytes = null;
    }
    dr.Close();
    Con.Close();
    dr.Dispose();
    Con.Dispose();
    Cmd.Dispose();
    lblresult.text="Downloaded...you can find in d:\download folder....";
    }
    catch (Exception ex)
    {
    lblresult.Text=ex.Message;
    }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, March 20, 2011 9:38 AM