逐步解說:應用程式層級專案中的簡單資料繫結

更新: 2008 年 7 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

專案類型

  • 應用程式層級專案

Microsoft Office 版本

  • Word 2007

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

從 Visual Studio 2008 Service Pack 1 (SP1) 開始,您可以透過應用程式層級專案,將資料繫結至主控制項和 Windows Form 控制項。本逐步解說示範如何在執行階段將控制項加入至 Microsoft Office Word 文件,以及將控制項繫結至資料。

這個逐步解說將說明下列工作:

  • 在執行階段將 ContentControl 加入至文件。

  • 建立可將控制項連接至資料集執行個體的 BindingSource

  • 讓使用者可以捲動記錄,並用此控制項檢視這些記錄。

注意事項:

您的電腦可能會在下列說明中,以不同名稱或位置顯示某些 Visual Studio 使用者介面項目。您所擁有的 Visual Studio 版本以及使用的設定會決定這些項目。如需詳細資訊,請參閱 Visual Studio 設定

必要條件

您需要下列元件才能完成此逐步解說:

  • Visual Studio Tools for Office (Visual Studio 2008 Professional 和 Visual Studio Team System 的選擇性元件)。

    Visual Studio Tools for Office 預設會與列出的 Visual Studio 版本一併安裝。若要查看是否已安裝,請參閱 安裝 Visual Studio Tools for Office

  • Word 2007。

  • 存取附加了 AdventureWorksLT 範例資料庫之執行中的 SQL Server 2005 或 SQL Server 2005 Express 執行個體。您可以從 CodePlex 網站 (本頁面可能為英文) 下載 AdventureWorksLT 資料庫。如需附加資料庫的詳細資訊,請參閱下列主題:

建立新專案

首先,必須建立 Word 增益集專案。

若要建立新的專案

  • 使用 Visual Basic 或 C#,建立名為從資料庫填入文件的 [Word 2007 增益集] 專案。

    如需詳細資訊,請參閱 HOW TO:建立 Visual Studio Tools for Office 專案

    Visual Studio 隨即開啟 ThisAddIn.vb 或 ThisAddIn.cs 檔案,並將從資料庫填入文件專案加入至 [方案總管]。

建立資料來源

請使用 [資料來源] 視窗,將具型別資料集加入您的專案。

若要將具型別資料集加入至專案

  1. 在 [資料] 功能表上,按一下 [加入新資料來源]。

    [資料來源組態精靈] 隨即開啟。

  2. 按一下 [資料庫],然後按 [下一步]。

  3. 如果已經有現有的 AdventureWorksLT 資料庫連接,請選擇此連接,再按 [下一步]。

    否則,請按一下 [新增連接],並使用 [加入連接] 對話方塊建立新連接。如需詳細資訊,請參閱 HOW TO:建立連接至 SQL Server 資料庫

  4. 在 [將連接字串儲存到應用程式組態檔] 頁面上,按 [下一步]。

  5. 在 [選擇您的資料庫物件] 頁面中,展開 [資料表],並選取 [客戶 (SalesLT)]。

  6. 按一下 [完成]。

    AdventureWorksLTDataSet.xsd 檔案隨即加入至 [方案總管]。這個檔案定義下列項目:

    • 名稱為 AdventureWorksLTDataSet 的具型別資料集。這個資料集代表 AdventureWorksLT 資料庫中 客戶 (SalesLT) 資料表的內容。

    • 名為 CustomerTableAdapter 的 TableAdapter。這個 TableAdapter 可以用來讀取和寫入 AdventureWorksLTDataSet 中的資料。如需詳細資訊,請參閱 TableAdapter 概觀

    在這個逐步解說後面的步驟中,您會用到這兩個物件。

建立控制項並將控制項繫結至資料

本逐步解說用來檢視資料庫記錄的是非常基本的介面,而這個介面是直接建立在文件裡面。介面中的 ContentControl 會一次顯示一筆資料庫記錄,而另外兩個 Button 控制項可讓您來回捲動記錄。內容控制項會使用 BindingSource 來連接資料庫。

如需將控制項繫結至資料的詳細資訊,請參閱將資料繫結至控制項

若要在文件中建立介面

  1. 在 ThisAddIn 類別中,宣告下列控制項以顯示並捲動 AdventureWorksLTDataSet 資料庫的 Customer 資料表。

    Private adventureWorksDataSet As AdventureWorksLTDataSet
    Private customerTableAdapter As AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter
    Private customerBindingSource As System.Windows.Forms.BindingSource
    Private customerContentControl As Microsoft.Office.Tools.Word.RichTextContentControl
    Private WithEvents button1 As Microsoft.Office.Tools.Word.Controls.Button
    Private WithEvents button2 As Microsoft.Office.Tools.Word.Controls.Button
    
    private AdventureWorksLTDataSet adventureWorksDataSet;
    private AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter customerTableAdapter;
    private System.Windows.Forms.BindingSource customerBindingSource;
    private Microsoft.Office.Tools.Word.RichTextContentControl customerContentControl;
    private Microsoft.Office.Tools.Word.Controls.Button button1;
    private Microsoft.Office.Tools.Word.Controls.Button button2;
    
  2. 在 ThisAddIn_Startup 方法中加入下列程式碼以初始化該資料集,並以 AdventureWorksLTDataSet 資料庫內的資訊填入資料集。

    Me.adventureWorksDataSet = New AdventureWorksLTDataSet()
    Me.customerTableAdapter = New AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter()
    Me.customerTableAdapter.Fill(Me.adventureWorksDataSet.Customer)
    Me.customerBindingSource = New System.Windows.Forms.BindingSource()
    
    this.adventureWorksDataSet = new AdventureWorksLTDataSet();
    this.customerTableAdapter = new AdventureWorksLTDataSetTableAdapters.CustomerTableAdapter();
    this.customerTableAdapter.Fill(this.adventureWorksDataSet.Customer);
    this.customerBindingSource = new System.Windows.Forms.BindingSource();
    
  3. 將以下程式碼加入至 ThisAddIn_Startup 方法中。這會產生可擴充文件的主項目。如需詳細資訊,請參閱在應用程式層級增益集的執行階段中擴充 Word 文件和 Excel 活頁簿

    Dim currentDocument As Word.Document = Me.Application.ActiveDocument
    Dim extendedDocument As Document = currentDocument.GetVstoObject()
    
    Word.Document currentDocument = this.Application.ActiveDocument;
    Document extendedDocument = currentDocument.GetVstoObject();
    
  4. 在文件的開頭定義數個範圍。這些範圍會識別文字插入和控制項放置的位置。

    extendedDocument.Paragraphs(1).Range.InsertParagraphBefore()
    extendedDocument.Paragraphs(1).Range.InsertParagraphBefore()
    extendedDocument.Paragraphs(1).Range.Text = "The companies listed in the AdventureWorksLT database:   "
    
    extendedDocument.Paragraphs(2).Range.Text = "  "
    
    Dim range1 As Word.Range = extendedDocument.Paragraphs(2).Range.Characters.First
    Dim range2 As Word.Range = extendedDocument.Paragraphs(2).Range.Characters.Last
    Dim range3 As Word.Range = extendedDocument.Paragraphs(1).Range.Characters.Last
    
    extendedDocument.Paragraphs[1].Range.InsertParagraphBefore();
    extendedDocument.Paragraphs[1].Range.InsertParagraphBefore();
    extendedDocument.Paragraphs[1].Range.Text = 
        "The companies listed in the AdventureWorksLT database:   \n";
    extendedDocument.Paragraphs[2].Range.Text = "  "; 
    
    Word.Range range1 = extendedDocument.Paragraphs[2].Range.Characters.First;
    Word.Range range2 = extendedDocument.Paragraphs[2].Range.Characters.Last;
    Word.Range range3 = extendedDocument.Paragraphs[1].Range.Characters.Last;
    
  5. 將介面控制項加入至前面定義的範圍。

    Me.button1 = extendedDocument.Controls.AddButton(range1, 60, 15, "1")
    Me.button1.Text = "Previous"
    Me.button2 = extendedDocument.Controls.AddButton(range2, 60, 15, "2")
    Me.button2.Text = "Next"
    
    Me.customerContentControl = extendedDocument.Controls.AddRichTextContentControl( _
        range3, _
        "richTextContentControl1")
    
    this.button1 = extendedDocument.Controls.AddButton(range1, 60, 15, "1");
    this.button1.Text = "Previous";
    this.button2 = extendedDocument.Controls.AddButton(range2, 60, 15, "2");
    this.button2.Text = "Next";
    
    this.customerContentControl = extendedDocument.Controls.AddRichTextContentControl(
        range3,
        "richTextContentControl1");
    
  6. 使用 BindingSource,將內容控制項繫結至 AdventureWorksLTDataSet。如果是 C# 開發人員,請為 Button 控制項加入兩個事件處理常式。

    Me.customerBindingSource.DataSource = Me.adventureWorksDataSet.Customer
    Me.customerContentControl.DataBindings.Add( _
        "Text", _
        Me.customerBindingSource, _
        "CompanyName", _
        True, _
        Me.customerContentControl.DataBindings.DefaultDataSourceUpdateMode)
    
    this.customerBindingSource.DataSource = this.adventureWorksDataSet.Customer;
    this.customerContentControl.DataBindings.Add(
        "Text", 
        this.customerBindingSource, 
        "CompanyName", 
        true,
        this.customerContentControl.DataBindings.DefaultDataSourceUpdateMode);
    
    this.button1.Click += new EventHandler(button1_Click);
    this.button2.Click += new EventHandler(button2_Click);
    
  7. 加入下列程式碼以巡覽資料庫記錄。

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles button1.Click
        Me.customerBindingSource.MovePrevious()
    End Sub
    
    Private Sub button2_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles button2.Click
        Me.customerBindingSource.MoveNext()
    End Sub
    
    void button1_Click(object sender, EventArgs e)
    {
        this.customerBindingSource.MovePrevious();
    }
    
    void button2_Click(object sender, EventArgs e)
    {
        this.customerBindingSource.MoveNext();
    }
    

測試增益集

當您開啟 Word 時,內容控制項會顯示 AdventureWorksLTDataSet 資料集中的資料。請按 [下一筆] 和 [上一筆] 按鈕,捲動資料庫記錄。

若要測試增益集

  1. 請按 F5。

    名為 customerContentControl 的內容控制項隨即建立,而且會填入資料。同時,也會將名為 adventureWorksLTDataSet 的資料集物件和名為 customerBindingSource 的 BindingSource 加入至專案。ContentControl 會繫結至 BindingSource,而後者則繫結至資料集物件。

  2. 請按 [下一筆] 和 [上一筆] 按鈕,捲動資料庫記錄。

請參閱

工作

HOW TO:將資料庫的資料填入工作表

HOW TO:將資料庫的資料填入文件

HOW TO:將服務的資料填入文件

HOW TO:將物件的資料填入文件

HOW TO:在工作表中捲動資料庫記錄

HOW TO:從主控制項中使用資料更新資料來源

逐步解說:文件層級專案中的簡單資料繫結

逐步解說:文件層級專案中的複雜資料繫結

HOW TO:將物件的資料填入文件

HOW TO:從主控制項中使用資料更新資料來源

概念

將資料繫結至控制項

使用在 Office 方案概觀中的本機資料庫檔案

資料來源概觀

顯示資料概觀

使用在 Office 方案概觀中的本機資料庫檔案

連接至 Visual Studio 中的資料概觀

參考

BindingSource 元件概觀

其他資源

Office 方案的資料

變更記錄

日期

記錄

原因

2008 年 7 月

加入主題。

SP1 功能變更。