How to get file extension when extracting saved files from MS SQL Server2008 to local disk

In that case, when I want to save and extract all types of files in MS SQL SERVER 2008, I can save and get any file, but without the extension. For example, if I save the xyz.txt file in the database, when I extract it to the local disk, it looks like xyz, without the extension, and yes, if we give the extension manually, then we can see its contents.

Next image of my table Table

C # code to save any type of file in a 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 to extract a file from a database to a local disk

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());
         }
    }

An image of how this project works.

http://i.stack.imgur.com/dqYMe.jpg

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

http://www.shabdar.org/sql-server/121-store-or-save-files-in-sql-server-database-using-c.html

+3
3

" "?

Windows, Alt, " | ", "" " "? , "".

, !:)

+1

- , System.IO.Path.GetFileName(""), . , .

0

You can use System.IO.Path.GetExtension()to get the file extension:

string fileName = @"C:\a\b\c\test.mp3";
string extension = System.IO.Path.GetExtension(fileName);
0
source

All Articles