Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Upload and Download Files with SQL Server in ASP.NET

, 1 Jan 2012 CPOL
This article presents an example to upload and download files with SQL Server in ASP.NET.
ASPNetFileUpDownLoad.zip
ASPNetFileUpDownLoad
ASPNetFileUpDownLoad
ASPNetFileUpDownLoad.csproj.user
bin
Global.asax
obj
Debug
Properties
Styles
Utilities
CreateDatabaseScript.zip
USE [master]
GO

-- Create the [AFileStorageDB] database
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N'AFileStorageDB')
BEGIN
	ALTER DATABASE [AFileStorageDB] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
	DROP DATABASE [AFileStorageDB]
END
GO

CREATE DATABASE [AFileStorageDB]
GO

-- Create the FileLoader login to the server
IF  EXISTS (SELECT * FROM sys.server_principals WHERE name = N'FileLoader')
DROP LOGIN [FileLoader]
GO

EXEC sp_addlogin @loginame = 'FileLoader', @passwd  = 'Pd123456';
GO

USE [AFileStorageDB]
GO

-- Create the [Files] table to the [AFileStorageDB] database
CREATE TABLE [dbo].[Files](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](100) NOT NULL,
	[ContentType] [varchar](50) NOT NULL,
	[Size] [bigint] NOT NULL,
	[Data] [varbinary](max) NOT NULL,
 CONSTRAINT [PK_Files] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF,
	IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON,
	ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

-- Grant the required database access to the login 
sp_grantdbaccess 'FileLoader'
GO
sp_addrolemember 'db_datareader', 'FileLoader'
GO
sp_addrolemember 'db_datawriter', 'FileLoader'
GO

-- Bring the database on-line
ALTER DATABASE [AFileStorageDB] SET MULTI_USER
GO

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Dr. Song Li

United States United States
I have been working in the IT industry for some time. It is still exciting and I am still learning. I am a happy and honest person, and I want to be your friend.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411022.1 | Last Updated 1 Jan 2012
Article Copyright 2012 by Dr. Song Li
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid