Share via


How to: Programmatically Cache a Data Source in an Office Document

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

Microsoft Office version

  • 2007 Microsoft Office system

  • Microsoft Office 2003

For more information, see Features Available by Application and Project Type.

You can programmatically add a data object to the data cache in a document by calling the StartCaching method of a host item, such as a Document, Workbook, or Worksheet. The StartCaching method is private, but it appears in IntelliSense.

When you use the StartCaching method to add a data object to the data cache, the data object does not need to be declared with the CachedAttribute attribute. However, the data object must meet certain requirements to be added to the data cache. For more information, see Caching Data.

To programmatically cache a data object

  1. Declare the data object at the class level, not inside a method. This example assumes that you are declaring a DataSet named dataSet1 that you want to cache programmatically.

    Public dataSet1 As DataSet
    
    public DataSet dataSet1;
    
  2. Instantiate the data object, and then call the StartCaching method of the document or worksheet instance and pass in the name of the data object.

    dataSet1 = New DataSet()
    
    If Not (Me.IsCached("dataSet1")) Then 
        Me.StartCaching("dataSet1")
    End If
    
    dataSet1 = new DataSet();
    
    if (!this.IsCached("dataSet1"))
    {
        this.StartCaching("dataSet1");
    }
    

See Also

Tasks

How to: Cache Data for Use Offline or on a Server

How to: Programmatically Stop Caching a Data Source

How to: Cache Data in a Password-Protected Document

Concepts

Caching Data

Accessing Data in Documents on the Server

Other Resources

Saving Data

Concurrency Control in ADO.NET