文件首頁
MySQL Connector/NET 開發人員指南
相關文件 下載本手冊
PDF (US Ltr) - 1.3Mb
PDF (A4) - 1.3Mb


MySQL Connector/NET 開發人員指南  /  ...  /  教學:Connector/NET ASP.NET 設定檔提供者

6.2.2 教學:Connector/NET ASP.NET 設定檔提供者

本教學將說明如何使用 MySQL 設定檔提供者將使用者設定檔資訊儲存在 MySQL 資料庫中。本教學使用 MySQL Connector/NET 6.9.9、MySQL Server 5.7.21 和 Microsoft Visual Studio 2017 Professional Edition。

許多現代網站允許使用者建立個人設定檔。這需要大量的程式碼,但 ASP.NET 藉由將功能納入其設定檔類別中而大幅減少了此負擔。設定檔提供者會在這些類別與資料來源之間提供抽象化。MySQL 設定檔提供者可讓設定檔資料儲存在 MySQL 資料庫中。這可讓設定檔屬性寫入持久儲存區,並在需要時擷取。設定檔提供者也允許有效管理設定檔資料,例如,它可以刪除自特定日期後未存取的設定檔。

以下步驟說明如何選取 MySQL 設定檔提供者

  1. 建立新的 ASP.NET Web 專案。

  2. 選取 MySQL 應用程式組態工具。

  3. 在 MySQL 應用程式組態工具中,瀏覽工具至「設定檔」頁面。

  4. 選取使用 MySQL 管理我的設定檔核取方塊。

  5. 選取自動產生結構描述核取方塊。

  6. 按一下編輯,然後設定將用於儲存使用者設定檔資訊的資料庫的連接字串。

  7. 瀏覽至工具的最後一頁,然後按一下完成以儲存您的變更並結束工具。

此時,您現在已準備好開始使用 MySQL 設定檔提供者。透過以下步驟,您可以執行初步的安裝測試。

  1. 開啟您的 web.config 檔案。

  2. 新增一個簡單的設定檔,例如以下範例。

    <system.web>
      <anonymousIdentification enabled="true"/>
      <profile defaultProvider="MySQLProfileProvider">
        ...
        <properties>
          <add name="Name" allowAnonymous="true"/>
          <add name="Age" allowAnonymous="true" type="System.UInt16"/>
          <group name="UI">
            <add name="Color" allowAnonymous="true" defaultValue="Blue"/>
            <add name="Style" allowAnonymous="true" defaultValue="Plain"/>
          </group>
        </properties>
      </profile>
      ...

    anonymousIdentification 設定為 true 可讓未驗證的使用者使用設定檔。它們是由 Cookie 中的 GUID 而非使用者名稱識別。

現在 web.config 中已定義簡單的設定檔,下一步是撰寫一些程式碼來測試設定檔。

  1. 在「設計檢視」中,設計一個包含已新增控制項的簡單頁面。下圖顯示已開啟的Default.aspx索引標籤,其中包含各種文字方塊、清單和按鈕控制項。

    圖 6.2 簡單設定檔應用程式

    Content is described in the surrounding text.

    這些控制項可讓使用者輸入一些設定檔資訊。使用者也可以使用按鈕來儲存設定檔、清除頁面和還原其設定檔資料。

  2. 在「程式碼檢視」中,新增以下程式碼片段。

    ...
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            TextBox1.Text = Profile.Name;
            TextBox2.Text = Profile.Age.ToString();
            Label1.Text = Profile.UI.Color;
        }
    }
      
    // Store Profile
    protected void Button1_Click(object sender, EventArgs e)
    {
        Profile.Name = TextBox1.Text;
        Profile.Age = UInt16.Parse(TextBox2.Text);
    }
      
    // Clear Form
    protected void Button2_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "";
        TextBox2.Text = "";
        Label1.Text = "";
    }
    
    // Retrieve Profile
    protected void Button3_Click(object sender, EventArgs e)
    {
        TextBox1.Text = Profile.Name;
        TextBox2.Text = Profile.Age.ToString();
        Label1.Text = Profile.UI.Color;
    }
    
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Profile.UI.Color = DropDownList1.SelectedValue;
    }
    ...
  3. 儲存所有檔案並建置解決方案,以檢查是否引入了任何錯誤。

  4. 執行應用程式。

  5. 輸入您的姓名、年齡,然後從清單中選取顏色。現在,按一下儲存設定檔,將此資訊儲存在您的設定檔中。

    如果未從清單中選取顏色,則會使用 web.config 檔案中指定的預設顏色藍色

  6. 按一下清除表單以清除文字方塊中的文字,以及顯示您所選顏色的標籤。

  7. 現在按一下擷取設定檔,從 MySQL 資料庫還原您的設定檔資料。

  8. 現在結束瀏覽器以終止應用程式。

  9. 再次執行應用程式,這也會從 MySQL 資料庫還原您的設定檔資訊。

在本教學中,您已了解如何將 MySQL 設定檔提供者與 Connector/NET 搭配使用。