Upload Files to Database From an ASP.NET Page

Using C#, it is possible to upload files to SQL Server database. Here we demoed how to do it by creating a sample database that has one table with columns: FileID, Filename, Extension, ContentType, Document. The FileID serves as the primary key in table.

Here is the code:

 <form id="frmMain" runat="server">
<div>
<input type="file" id="txtFile" title=" Browse for file which you want to save " Runat="server"/>
<input id="btnSave" type="button" value="Save" onclick="document.frmMain.submit()" />

</form>

The code below is to create a statement for FileID Table: 

CREATE TABLE [dbo].[File](
[FileId] [int] IDENTITY(1,1) NOT NULL,
[Filename] [varchar](100) NULL,
[Extension] [varchar](8) NULL,
[ContentType] [varchar](50) NULL,
[Document] [varbinary](50) NULL,
CONSTRAINT [PK_File] PRIMARY KEY CLUSTERED
(
[FileId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

Read the rest of the tutorial here: http://www.codeproject.com/Articles/667570/Uploading-files-to-database-from-ASP-NET-pages

Leave a Reply

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