Dynamic Web TWAIN: How to upload images directly to Database?

Scenario

Upload an image into the server's database using Dynamic Web TWAIN.

Solution

Here are some methods to achieve this.
HTTPUploadAllThroughPostAsMultiPageTIFF()
HTTPUploadThroughPostAsMultiPageTIFF()
HTTPUploadAllThroughPostAsPDF()
HTTPUploadThroughPostAsMultiPagePDF()
HTTPUploadThroughPostEx()

How

1.Add an action page to save the image data to the database.

For example, we can use C# or VB.NET:

<%@ Page Language="C#" %>
<% 
try { 
  String strConnString; 
  strConnString = "Server=localhost;Database=DynamicWebTWAIN;Integrated Security=SSPI;"; 
  System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(strConnString); 
  String SqlCmdText = "INSERT INTO tblDynamicWebTWAIN (strImageName,imgImageData) VALUES (@ImageName,@Image)"; 
  System.Data.SqlClient.SqlCommand sqlCmdObj = new System.Data.SqlClient.SqlCommand(SqlCmdText, sqlConnection); 
  sqlCmdObj.Parameters.Add("@Image", System.Data.SqlDbType.Binary, iFileLength).Value = inputBuffer; 
  sqlCmdObj.Parameters.Add("@ImageName", System.Data.SqlDbType.VarChar, 255).Value = strImageName; 
  sqlConnection.Open(); 
  sqlCmdObj.ExecuteNonQuery(); 
  sqlConnection.Close(); 
} 
catch (Exception) { } %>

You can download the complete code from here.

2.Add the upload method and the action page.

On the client side, use the above mentioned HTTPUploadXXX methods to upload the image. Put the action page SaveToDB.aspx as the destination of the image. For more info, please refer to Uploading the image(s) to a web server of the Developers' Guide

3.Set up SQL Server

To be noted, when uploading files into database, we also need to make sure the account we are using has the write permission on the database. Take the Internet Information Service(IIS) server for example, if the identity NetworkService is used, as can be checked from the following setting.

Then in Microsoft SQL Server, you need to make sure a server role with write permission, such as sysadmin, is checked.