Import and Export Excel Files into Varbinary (Max) in SQL Server

Introduction

In this article you will get the detail about How to Import and Export Excel files into varbinary(max)in Sql server Using Asp.net C#

What are advantages of storing as varbinary file?

  • We need not to depend on the file system,
  • It will avoid the legal issues and data risk

Aspx code

Add just two buttons,

  1. <asp:Button ID=“BtnImportExcelToDB” runat=“server” Text=“ImportExcelToDb” OnClick=“BtnImportExcelToDB_Click” />
  2. <asp:Button ID=“BtnExportExcelFromDB” runat=“server” Text=“ExportExcelFromDb” OnClick=“BtnExportExcelFromDB_Click” />

Codebehind code,

Code for Import Excel to Db,

  1. //Button Click event for the ImportExcelToDB 
  2. protected void BtnImportExcelToDB_Click(object sender, EventArgs e)
  3. {
  4.     //Specify the filepath where the file exist
  5.     string filename = @ “D:\TPMS\Uploaded_Boq\Raveena_Boq1.xlsx”;
  6.     //pass the filename as a parameter
  7.     this.StoreExcelFileToDatabase(filename);
  8. }
  9. // store Excel sheet (or any file for that matter) into a SQL Server table
  10. public void StoreExcelFileToDatabase(string excelFileName)
  11. {
  12.     // if file doesn’t exist –> terminate (you might want to show a message box or something)
  13.     if (!File.Exists(excelFileName))
  14.     {
  15.         return;
  16.     }
  17.     // get all the bytes of the file into memory
  18.     byte[] excelContents = File.ReadAllBytes(excelFileName);
  19.     // define SQL statement to use
  20.     string insertStmt = “INSERT INTO dbo.Tender_Excel_Source(FileName, FileContent) VALUES(@FileName, @BinaryContent)”;
  21.     // set up connection and command to do INSERT
  22.     using(SqlConnection connection = new SqlConnection(OSMC.constring_Property))
  23.     using(SqlCommand cmdInsert = new SqlCommand(insertStmt, connection))
  24.     {
  25.         cmdInsert.Parameters.Add(“@FileName”, SqlDbType.VarChar, 500).Value = excelFileName;
  26.         cmdInsert.Parameters.Add(“@BinaryContent”, SqlDbType.VarBinary, int.MaxValue).Value = excelContents;
  27.         // open connection, execute SQL statement, close connection again
  28.         connection.Open();
  29.         cmdInsert.ExecuteNonQuery();
  30.         connection.Close();
  31.     }
  32. }

Code for Export Excel from DB,

  1. protected void BtnExportExcelFromDB_Click(object sender, EventArgs e)
  2. {
  3.     string filepathtostore = @ “D:\TPMS\Uploaded_Boq\Raveena_boq_From_Db.xlsx”;
  4.     RetrieveExcelFileFromDatabase(4, filepathtostore);
  5. }
  6. public void RetrieveExcelFileFromDatabase(int ID, string excelFileName)
  7. {
  8.     byte[] excelContents;
  9.     string selectStmt = “SELECT FileContent FROM dbo.Tender_Excel_Source WHERE file_sequence_no = @ID”;
  10.     using(SqlConnection connection = new SqlConnection(OSMC.constring_Property))
  11.     using(SqlCommand cmdSelect = new SqlCommand(selectStmt, connection))
  12.     {
  13.         cmdSelect.Parameters.Add(“@ID”, SqlDbType.Int).Value = ID;
  14.         connection.Open();
  15.         excelContents = (byte[]) cmdSelect.ExecuteScalar();
  16.         connection.Close();
  17.     }
  18.     File.WriteAllBytes(excelFileName, excelContents);
  19. }
  20. }

Database Create Table Script

  1. USE [tpms_release1]
  2. GO
  3. /****** Object: Table [dbo].[Tender_Excel_Source] Script Date: 09-06-16 Morning 10:19:05 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. SET ANSI_PADDING ON
  9. GO
  10. CREATE TABLE [dbo].[Tender_Excel_Source](
  11. [fk_tender_id] [intNULL,
  12. [file_sequence_no] [int] IDENTITY(1,1) NOT NULL,
  13. [FileName] [nvarchar](1024) NULL,
  14. [FileContent] [varbinary](maxNULL
  15. ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
  16. GO
  17. SET ANSI_PADDING OFF
  18. GO

Output

Output
Note

Excel File had converted to the Byte file and will be saved like this.

Excel File
Excel File
Note

Now vice versa Byte file converted into original Excel file.

Excel File

Hope the above information was useful, kindly let me know your feedback or suggestion.

 

Leave a Reply

Your email address will not be published. Required fields are marked *