This tutorial will show you how we can use ASP.NET to allow uploading of files directly to a SQL database in VB.NET
In this tutorial, we will explore how to use ASP.NET to allow users to upload files directly to a SQL database, rather than storing the file in the Web Server’s file system.
The main advantage to doing this that when it comes to the backing up of the data, it is all stored in one secure place; there is no need to worry about backing up files in different locations that the database may link to.

We will start by creating our VB.NET project in Visual Studio and adding a SQL Database to the project. To do this, right-click your App_Data folder in Solution Explorer, then choose Add New Item.. SQL Server Database.

We will be adding a new table to the database, name it what you want (right-click Tables folder in Server Explorer and choose Add New Table.) We will create the following columns with the respective data types: ID (bigint), FileName (varchar(50)), DateTimeUploaded (datetime), MIME (varchar(50)), and BinaryData (varbinary(MAX)). The varbinary data type will be used to store the uploaded file, which can hold up to 2GB of data.

The next thing we need to do is create our upload form. Open up the ASPX page if it’s not already, and add the following:

Here, we have a Literal control to display messages to the user, a textbox for the user to enter a name for the uploaded file, a File Upload control, and then a button to initiate the upload. Notice the method we are referencing on the OnClick event of the button. To create this handler, double-click on the button in design view. We should get the following:

Now we need to make sure we can connect to our database, we’ll add the connection string in the Web.config:
(A quick way to do this is to add a SqlDataSource control to the ASPX page, then configure it in design view using the Smart Tag. Then simply delete the control when you’re done – the connection string will have been automatically inserted into the Web.config for you).

Before we start coding the button click event, we need to make sure we have the following assembly reference, as we’ll be making use of them:

Now we have prepared, we can begin writing our code. The first thing we will do is to check to see whether we have a file to upload; if not, we need to notify the user:

If we do have a file to upload, then let’s upload it to the database:

Download Source Files