Krystalware
SlickUpload logo

Uploading to SQL Server

The SQL Client upload stream provider is a built-in SlickUpload provider that uses a SQL Server database for uploaded file storage.

Configuration

To use the SQL provider, set the provider="SqlClient" attribute on the uploadStreamProvider web.config key. The following settings allow you to customize the table and fields used for file storage:

Setting Description
provider provider="SqlClient"
connectionString

Optional string attribute. Either connectionString or connectionStringName is required.

Specifies the connection string to use to connect to the SQL Server database.

connectionStringName

Optional string attribute. Either connectionString or connectionStringName is required.

Specifies the name of the connection string from the connectionStrings web.config element to use to connect to the SQL Server database.

table

Required string attribute.

Specifies the table to write to.

keyField

Required string attribute.

Specifies the key field that identifies the record to write to.

dataField

Required string attribute.

Specifies the data field to write to.

fileNameField

Optional string attribute.

Specifies the field to which to write the file name.

criteriaMethod

Optional CriteriaMethod attribute.

  • Identity – Set the key field as an identity. This is the default.
  • Custom – Use a ICriteriaGenerator, specified in the criteriaGenerator attribute, to generate the key.
criteriaGenerator

Optional class reference attribute.

Specifies a custom class that implements the ICriteriaGenerator interface. Loaded when the criteriaMethod attribute is set to Custom.

Controlling record to write during upload

The record mode is set by the criteriaMethod config attribute (see above). To specify which record to write to you can either use the Identity method to automatically insert or the Custom method to insert manually and generate your own criteria.

The Identity method can be used with a table that has a single primary key that is an insertable identity. SlickUpload will insert into the table, and then write the file data into the resulting record.

For more control, you can use the Custom method. You'll create a class that implements the ICriteriaGenerator interface, insert a record, and return the WHERE criteria to that record for SlickUpload to use.

Retrieving record criteria

After the upload you can get the record criteria for a file by indexing into the UploadedFile.LocationInfo dictionary. For example, given an UploadedFile instance named file, you'd get the identity value like so:

string id = file.LocationInfo[SqlClientUploadStreamProvider.IdentityIdKey];

To get the criteria as a string:

string criteria = file.LocationInfo[SqlClientUploadStreamProvider.WhereCriteriaKey];