ARTICLE

How to store Excel file in Database

Posted by Munesh Sharma Articles | ADO.NET in VB.NET May 06, 2011
In this article, we will learn about how to store a file in Database.
Download Files:
 
Reader Level:

Data files are various types and it's extension is also deferent such as ms word file extension is .Doc, Excel file extension Xls and etc. so we design the data base with three columns. first is file_name, second is content_type and last data.

There are various steps to store data file in to data base. that are given bellow:

  • Design the window form, it contain one TextBox and two Buttons:

    window-form-design.gif

  • design Database, it contain three fields.

    database-of-file.gif

  • OpenDialog1 coding, that open the new dialog and select the file do want to store:

    Public Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click
            Dim ofd As OpenFileDialog = New OpenFileDialog() 
            ofd.ShowDialog()
            TextBox1.Text = ofd.FileName
            Dim path As String = TextBox1.Text
            Dim file_nameAs String = ofd.FileName         

        End Sub

  • SaveDialog1 coding. to use to store file in binary format in to database:

    Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click
            Try
                Dim strAs String ="Data Source=MCNDESKTOP14;Initial Catalog=studentinfo; uid=sa;pwd=wintellect"
                Dim conAs SqlConnection
                con = New SqlConnection(str)
                con.Open()
                Dim pathAs String = TextBox1.Text 
                Dim fs As System.IO.FileStream = New System.IO.FileStream(path, FileMode.Open, FileAccess.Read) 
                Dim br As System.IO.BinaryReader = New System.IO.BinaryReader(fs) 
                Dim bytesAs Byte() = br.ReadBytes(Convert.ToInt32(fs.Length)) 
                br.Close() 
                fs.Close()
                Dim strQueryAs String ="insert into info09(name, contentType, data) values (@name, @contentType, @data)"
     
                Dim cmd As SqlCommand = New SqlCommand(strQuery) 
                cmd.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text  
                cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = "application/vnd.ms-excel"
     
                cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes 
                InsertUpdateData(cmd)  
            Catch ex As Exception
                MessageBox.Show(ex.Message)  
            End Try
     
        End Sub

Output:

                          output-file.gif

                          output-database.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor