<%@ Page Title="SQL Server Provider" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" %>
<%@ Register Assembly="AjaxUploader" Namespace="AjaxUploader.Controls" TagPrefix="au" %>

<asp:Content ID="TitleContent" ContentPlaceHolderID="TitleContent" runat="server">SQL Server Provider - AjaxUploader v5.0 Demos</asp:Content>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeaderTitle" runat="server">SQL Server Provider</asp:Content>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

<div class="demo-card">
    <div class="demo-card-header">SQL Server Provider</div>
    <div class="demo-card-body">
        <p class="demo-description">
            Configure AjaxUploader to store uploaded files in SQL Server instead of the
            file system. Files are stored as binary data in a database table, making them
            easy to manage and back up with your database. Configure via
            <code>web.config</code> connection strings and <code>appSettings</code>.
        </p>

        <au:AjaxFileUpload ID="Uploader1" runat="server"
            AllowMultiple="true"
            AutoUpload="true"
            ShowProgress="true"
            ButtonText="Upload to SQL Server" />

        <div class="code-panel">
            <pre><code>&lt;%-- web.config: connection string and provider settings --%&gt;
&lt;connectionStrings&gt;
    &lt;add name="UploadDb"
         connectionString="Server=.;Database=MyApp;Trusted_Connection=true;"
         providerName="System.Data.SqlClient" /&gt;
&lt;/connectionStrings&gt;

&lt;appSettings&gt;
    &lt;add key="AjaxUploader:StorageProvider" value="SqlServer" /&gt;
    &lt;add key="AjaxUploader:ConnectionStringName" value="UploadDb" /&gt;
    &lt;add key="AjaxUploader:TableName" value="UploadedFiles" /&gt;
    &lt;add key="AjaxUploader:SchemaName" value="dbo" /&gt;
    &lt;add key="AjaxUploader:AutoCreateTable" value="true" /&gt;
&lt;/appSettings&gt;

&lt;%-- The provider automatically creates a table like: --%&gt;
-- CREATE TABLE [dbo].[UploadedFiles] (
--     [Id]          UNIQUEIDENTIFIER PRIMARY KEY,
--     [FileName]    NVARCHAR(256),
--     [ContentType] NVARCHAR(128),
--     [FileSize]    BIGINT,
--     [FileData]    VARBINARY(MAX),
--     [CreatedAt]   DATETIME2 DEFAULT GETUTCDATE()
-- );

&lt;%-- Retrieve a file from SQL Server in an .ashx handler --%&gt;
public class FileHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        Guid id = Guid.Parse(context.Request.QueryString["id"]);
        string connStr = ConfigurationManager
            .ConnectionStrings["UploadDb"].ConnectionString;

        using (var conn = new SqlConnection(connStr))
        using (var cmd = new SqlCommand(
            "SELECT FileName, ContentType, FileData " +
            "FROM UploadedFiles WHERE Id = @Id", conn))
        {
            cmd.Parameters.AddWithValue("@Id", id);
            conn.Open();
            using (var reader = cmd.ExecuteReader())
            {
                if (reader.Read())
                {
                    context.Response.ContentType =
                        reader["ContentType"].ToString();
                    context.Response.BinaryWrite(
                        (byte[])reader["FileData"]);
                }
            }
        }
    }
}</code></pre>
        </div>
    </div>
</div>

</asp:Content>
