Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get File extension while retriving saved Files from MS SQL Server2008 to local drive

I am in such a circumstance where I want to save and retrieve all types of files in MS SQL SERVER 2008, I can Save and retrieve any file but without extension, For example if I saves xyz.txt file into database, when I retrieve it on local drive it looks like xyz , no extension, and yes if we give extension manually then we can see its contents.

Following is my table image Table

C# code for saving any file type to database

 private void cmdSave_Click(object sender, EventArgs e)
    {
        try
        {
            //Read File Bytes into a byte array
            byte[] FileData = ReadFile(txtFilePath.Text);

            //Initialize SQL Server Connection
            SqlConnection CN = new SqlConnection(txtConnectionString.Text);

            //Set insert query
            string qry = "insert into FilesStore (OriginalPath,FileData) values(@OriginalPath, @FileData)";

            //Initialize SqlCommand object for insert.
            SqlCommand SqlCom = new SqlCommand(qry, CN);

            //We are passing Original File Path and File byte data as sql parameters.
            SqlCom.Parameters.Add(new SqlParameter("@OriginalPath", (object)txtFilePath.Text));
            SqlCom.Parameters.Add(new SqlParameter("@FileData", (object)FileData));

            //Open connection and execute insert query.
            CN.Open();
            SqlCom.ExecuteNonQuery();
            CN.Close();

            //Close form and return to list or Files.
            this.Close();
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

C# code for retrieving file from database to local drive

private void btnSaveFromDBToDisk_Click(object sender, EventArgs e)
    {
         try
         {
             //Check if a file is selected in Grid
             if (dataGridView1.CurrentCell ==null)
             {
                 MessageBox.Show("Select a file in Grid first.");
                 return;
             }

             int SelectedRow = dataGridView1.CurrentCell.RowIndex;
             string OriginalPath = DS.Tables[0].Rows[SelectedRow]["OriginalPath"].ToString();

             saveFileDialog1.FileName = OriginalPath;

             DialogResult DR = saveFileDialog1.ShowDialog();
             if (DR == DialogResult.OK)
             {
                 string FileName = saveFileDialog1.FileName;
                 //Get File data from dataset row.
                 byte[] FileData = (byte[])DS.Tables["FilesStore"].Rows[SelectedRow]["FileData"];
                 //Write file data to selected file.
                 using (FileStream fs = new FileStream(FileName, FileMode.Create))
                 {
                     fs.Write(FileData, 0, FileData.Length);
                     fs.Close();
                 }

                 MessageBox.Show("File saved successfully to ");
             }
         }
         catch(Exception ex)
         {
             MessageBox.Show(ex.ToString());
         }
    }

Image of how this project works.

https://i.sstatic.net/dqYMe.jpg

Save and retrieve image project, unable to see file extension as in step 6 arrow

You can download this project from here http://www.shabdar.org/sql-server/121-store-or-save-files-in-sql-server-database-using-c.html

like image 307
Ahmed Syed Avatar asked Dec 01 '25 02:12

Ahmed Syed


1 Answers

What is the system's setting for "extensions for known file types"?

Go to Windows Explorer, press Alt, Select "Tools|Folder Options", select "View" tab, and is "Hide extensions for known file types" ticked? If so, untick it and click "OK".

If that was the issue, all should now be ok! :)

like image 148
Matthew Watson Avatar answered Dec 03 '25 16:12

Matthew Watson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!