Hi ..

I have been asked to create a new thread rather than recurrent on old post (http://www.daniweb.com/forums/thread116468-2.html), hope to get some help here... :)

I would like to have a "Extract" button which then open a Save As dialog to save the file from the Database into local pc, the SqlFileHolder provided in http://www.daniweb.com/forums/thread116468-1.html , work nice for me, but just wanted to have additional stuff by downloading the file.

private void saveFile(object sender, System.EventArgs e)
            // open SaveFileDialog so the user can save the file
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Title = "Save File..";

How could i proceed to get the file from the database and save into my local pc from the code above?

Thank you.

Presume that the sql table named "fileTable" has filename(varchar(50)) and filedata(image) fields.

string cnStr = "your_connection_string";
            string sql = "select filedata from fileTable where filename='sample.txt'";
            string filename=saveFileDialog1.FileName;
            SqlDataAdapter adp = new SqlDataAdapter(sql, cnStr);
            DataTable dt = new DataTable();

            if (dt.Rows.Count != 0)
                byte[] b = (byte[])dt.Rows[0]["filedata"];
                FileStream fs = new FileStream(filename, FileMode.Create);
                fs.Write(b, 0, b.Length);

hi all,
I tried to extract the files and corrupted file, the special characters you can see
(Rar! Ïs tÀ/ õ 3 ZÌtòf‰”F3)
how to files may display the correct content

Este codigo es para descargar un archivo de una base de datos en sql usando SP este codigo funciona para Asp, wpf

    private void Button_Click(object sender, RoutedEventArgs e)
     int valor = 2;
        //  in it line of code is Of template 2XH
        using (SqlConnection cn = new SqlConnection(@"Data Source=DESKTOP-N1GRH28; Initial Catalog= Etiquetas_Soriana; Trusted_Connection=Yes"))
            string sPathToSaveFileTo = @"C:\Users\Allware\Documents\xml_prueba_soriana\2xh.xml";
            string contenttype = String.Empty;
            SqlCommand cmd = new SqlCommand("sp_GetXmlTipoHoja", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Id", valor);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
                // read in using GetValue and cast to byte array
                byte[] fileData = (byte[])dr.GetValue(0);
                // write bytes to disk as file
                using (System.IO.FileStream fs = new System.IO.FileStream(sPathToSaveFileTo, System.IO.FileMode.Create, System.IO.FileAccess.ReadWrite))
                    // use a binary writer to write the bytes to disk
                    using (System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs))
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.