In this article I will explain with an example, how to save file directly to SQL Server database using the ASP.Net FileUpload control in C# and VB.Net.
This article will illustrate how files like images, Word document, Excel document, Portable Document Format (PDF), Text Files can be uploaded, converted into Binary data and the saved into the SQL Server database in ASP.Net.
 
 
Database
This article makes use of a table named tblFiles whose schema is defined as follows.
Save Files to SQL Server Database using FileUpload Control
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The HTML Markup contains of:
FileUpload – For uploading file.
Button – For saving file into the database.
Label – For displaying success message.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="Upload" />
<hr />
<asp:Label ID="lblMessage" ForeColor="Green" runat="server" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
 
VB.Net
Imports System.IO;
Imports System.Data;
Imports System.Data.SqlClient;
Imports System.Configuration;
 
 
Uploading the files and saving in SQL Server Database
When Upload button is clicked, following event handler is executed and the name of the uploaded File is fetched using the GetFileName function of the Path class.
And then, the ContentType (MIME type) of the File is fetched from ContentType property of the FileUpload control.
Note: The Content type (MIME type) is very important while downloading the files as it notifies the browser about type of the File.
 
The file is read from the FileUpload control using InputStream property and converted into Byte Array (Binary Data) using the ReadBytes method of the BinaryReader class.
Finally, the Byte Array (Binary Data) will be inserted into Database using ADO.Net and a success message is displayed in Label.
Note: For more details on how to insert data into the database in ADO.Net, please refer my article Insert data into Database in ASP.Net using C# and VB.Net.
 
C#
protected void Upload(object sender, EventArgs e)
{
    string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string contentType = FileUpload1.PostedFile.ContentType;
    using (Stream fs = FileUpload1.PostedFile.InputStream)
    {
        using (BinaryReader br = new BinaryReader(fs))
        {
            byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = "INSERT INTO tblFiles VALUES (@Name, @ContentType, @Data)";
                using (SqlCommand cmd = new SqlCommand(query))
                {
                    cmd.Connection = con;
                    cmd.Parameters.AddWithValue("@Name", filename);
                    cmd.Parameters.AddWithValue("@ContentType", contentType);
                    cmd.Parameters.AddWithValue("@Data", bytes);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
    }
    lblMessage.Text = "File uploaded successfully.";
}
 
VB.Net
Protected Sub Upload(sender As Object, e As EventArgs)
    Dim filename As String = Path.GetFileName(FileUpload1.PostedFile.FileName)
    Dim contentType As String = FileUpload1.PostedFile.ContentType
    Using fs As Stream = FileUpload1.PostedFile.InputStream
        Using br As New BinaryReader(fs)
            Dim bytes As Byte() = br.ReadBytes(CType(fs.Length, Integer))
            Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
            Using con As New SqlConnection(constr)
                Dim query As String = "INSERT INTO tblFiles VALUES (@Name, @ContentType, @Data)"
                Using cmd As New SqlCommand(query)
                    cmd.Connection = con
                    cmd.Parameters.Add("@Name", SqlDbType.VarChar).Value = filename
                    cmd.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contentType
                    cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes
                    con.Open()
                    cmd.ExecuteNonQuery()
                    con.Close()
                End Using
            End Using
        End Using
    End Using
    lblMessage.Text = "File uploaded successfully."
End Sub
 
 
Screenshots
The Form
Save Files to SQL Server Database using FileUpload Control
 
Record after Inserted in database
Save Files to SQL Server Database using FileUpload Control
 
 
Downloads