逐步解說:使用預存程序

您不能直接從 LightSwitch 呼叫 SQL Server 資料庫中的預存程序,但是可以在 LightSwitch 應用程式中建立含有預存程序參數的本機資料表,然後從伺服器層事件呼叫預存程序。 這個逐步解說顯示如何使用預存程序,在 SQL 資料庫中插入客戶記錄。

許多資料庫系統管理員都不允許直接存取資料表,而是公開唯讀檢視以及提供預存程序來插入、更新和刪除記錄。 LightSwitch 無法辨識預存程序,因此任何依賴預存程序來更新記錄的資料庫可能一開始都會顯示為無法使用。 如果您執行一些額外的步驟,則的確可以從 LightSwitch 使用這些資料庫。

必要條件

這個逐步解說使用 Northwind 範例資料庫。

建立預存程序

在大多數情況下,您將會存取已存在於資料庫中的預存程序。 Northwind 資料庫沒有插入客戶記錄所需的必要預存程序,因此您必須加入預存程序。

加入預存程序

  1. 在功能表列上,依序選擇 [檢視] 和 [SQL Server 物件總管]。

  2. 在 [SQL Server 物件總管] 視窗中,展開 [NORTHWIND] 資料庫節點,然後選擇 [程式設計]。

  3. 展開 [程式設計] 節點,然後選擇 [預存程序]。

  4. 開啟 [預存程序] 的捷徑功能表,然後選擇 [加入新的預存程序]。

  5. 在 [程式碼編輯器] 中,將內容取代為下列 Transact-SQL 程式碼,然後選擇 [更新] 按鈕。

    CREATE Procedure [dbo].[InsertCustomer]
        @CustomerID nchar(5),
        @CompanyName nvarchar(40),
        @ContactName nvarchar(30),
        @ContactTitle nvarchar(30),
        @Address nvarchar(60),
        @City nvarchar(15),
        @Region nvarchar(15),
        @PostalCode nvarchar(10),
        @Country nvarchar(15),
        @Phone nvarchar(24),
        @Fax nvarchar(24)
    AS
    INSERT INTO [dbo].[Customers]
               ([CustomerID]
               ,[CompanyName]
               ,[ContactName]
               ,[ContactTitle]
               ,[Address]
               ,[City]
               ,[Region]
               ,[PostalCode]
               ,[Country]
               ,[Phone]
               ,[Fax])
         VALUES
               (@CustomerID
               ,@CompanyName
               ,@ContactName
               ,@ContactTitle
               ,@Address
               ,@City
               ,@Region
               ,@PostalCode
               ,@Country
               ,@Phone
               ,@Fax)
    
  6. 在 [預覽資料庫更新] 對話方塊中,選擇 [更新資料庫] 按鈕。

    InsertCustomer 預存程序會加入至資料庫。

建立 LightSwitch 應用程式

在這個步驟中,您將建立呼叫預存程序並將新客戶加入至 Northwind 資料庫的應用程式。

建立應用程式

  1. 在功能表列上,依序選擇 [檔案]、[新增] 和 [專案]。

  2. 在 [新增專案] 對話方塊中,展開 [Visual Basic] 或 [Visual C#] 節點,然後選擇 [LightSwitch HTML 應用程式] 範本。

  3. 在 [名稱] 文字方塊中,輸入 NorthwindSP,然後選擇 [確定] 按鈕。

加入資料來源

  1. 在 [方案總管] 中,開啟 [資料來源] 節點的捷徑功能表,然後選擇 [加入資料來源]。

  2. 在 [附加資料來源精靈] 中,選擇 [資料庫],然後選擇 [下一步] 按鈕。

  3. 在 [連線屬性] 對話方塊中,輸入 Northwind 資料庫的連接詳細資料,然後選擇 [確定] 按鈕。

  4. 展開 [選擇您的資料庫物件] 頁面上的 [資料表] 節點,並選取 [Customers] 核取方塊,然後選擇 [完成] 按鈕。

加入本機資料表

  1. 在 [方案總管] 中,開啟 [資料來源] 節點的捷徑功能表,然後選擇 [加入資料表]。

  2. 在 [屬性] 視窗中,選擇 [名稱] 屬性,並輸入 NewCustomer

  3. 在 Entity Designer 中,加入下列欄位:

    名稱

    類型

    必要項

    CustomerID

    String

    CompanyName

    String

    ContactName

    String

    ContactTitle

    String

    地址

    String

    City

    String

    Region

    String

    PostalCode

    String

    Country

    String

    Phone

    電話號碼

    Fax

    電話號碼

    請注意,所有欄位都是必要欄位。 原因是預存程序的所有參數都需要值。 也請注意,PhoneFax 欄位的資料類型都是 Phone Number。 即使這些在資料庫中都是儲存為 nvarchar,您還是可以在用戶端層上利用 LightSwitch 自訂商務類型。

加入螢幕

  1. 在 Entity Designer 的 [透視圖] 列上,選擇 [HTMLClient]。

  2. 在 [工具列] 上,選擇 [螢幕] 按鈕。

  3. 在 [加入新螢幕] 對話方塊中選擇 [常見的螢幕集合] 範本;在 [螢幕集合名稱] 文字方塊中輸入 Customers,並在 [螢幕資料] 清單中選擇 [NewCustomer],然後選擇 [確定] 按鈕。

呼叫預存程序

  1. 在 [方案總管] 中,開啟 [NorthwindSP.Server] 節點的捷徑功能表,並依序選擇 [加入] 和 [參考]。

  2. 在 [加入參考] 對話方塊中,選取 [System.Configuration] 核取方塊,然後選擇 [確定] 按鈕。

  3. 在 [方案總管] 中,開啟 [NewCustomer.lsml] 節點的捷徑功能表,然後選擇 [開啟]。

  4. 在 Entity Designer 的 [透視圖] 列上,選擇 [伺服器]。

  5. 在 [工具列] 上,展開 [撰寫程式碼] 清單,並選擇 [NewCustomers_Inserting]。

  6. 在 [程式碼編輯器] 中,將現有程式碼取代為下面程式碼:

    Imports System.Configuration
    Imports System.Data
    Imports System.Data.SqlClient
    Namespace LightSwitchApplication
    
        Public Class ApplicationDataService
    
            Private Sub NewCustomers_Inserting(entity As NewCustomer)
                Using connection = New SqlConnection
                    Dim connectionStringName = Me.DataWorkspace.NorthwindData.Details.Name
                    connection.ConnectionString = ConfigurationManager.ConnectionStrings(connectionStringName).ConnectionString
    
                    Dim procedure = "dbo.InsertCustomer"
                    Using command = New SqlCommand(procedure, connection)
                        command.CommandType = CommandType.StoredProcedure
    
                        command.Parameters.Add(
                            New SqlParameter("@CustomerID", entity.CustomerID))
                        command.Parameters.Add(
                            New SqlParameter("@CompanyName", entity.CompanyName))
                        command.Parameters.Add(
                            New SqlParameter("@ContactName", entity.ContactName))
                        command.Parameters.Add(
                            New SqlParameter("@ContactTitle", entity.ContactTitle))
                        command.Parameters.Add(
                            New SqlParameter("@Address", entity.Address))
                        command.Parameters.Add(
                            New SqlParameter("@City", entity.City))
                        command.Parameters.Add(
                            New SqlParameter("@Region", entity.Region))
                        command.Parameters.Add(
                            New SqlParameter("@PostalCode", entity.PostalCode))
                        command.Parameters.Add(
                            New SqlParameter("@Country", entity.Country))
                        command.Parameters.Add(
                            New SqlParameter("@Phone", entity.Phone))
                        command.Parameters.Add(
                            New SqlParameter("@Fax", entity.Fax))
    
                        connection.Open()
                        command.ExecuteNonQuery()
                    End Using
    
    
                End Using
            End Sub
        End Class
    
    End Namespace
    
    using Microsoft.VisualBasic;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Configuration;
    using System.Data.SqlClient;
    namespace LightSwitchApplication
    {
    
    public class ApplicationDataService
    {
    
    private void NewCustomers_Inserting(NewCustomer entity)
    {
    using (connection == new SqlConnection()) {
    dynamic connectionStringName = this.DataWorkspace.NorthwindData.Details.Name;
    connection.ConnectionString = ConfigurationManager.ConnectionStrings(connectionStringName).ConnectionString;
    
    dynamic procedure = "dbo.InsertCustomer";
    using (command == new SqlCommand(procedure, connection)) {
    command.CommandType = CommandType.StoredProcedure;
    
    command.Parameters.Add(new SqlParameter("@CustomerID", entity.CustomerID));
    command.Parameters.Add(new SqlParameter("@CompanyName", entity.CompanyName));
    command.Parameters.Add(new SqlParameter("@ContactName", entity.ContactName));
    command.Parameters.Add(new SqlParameter("@ContactTitle", entity.ContactTitle));
    command.Parameters.Add(new SqlParameter("@Address", entity.Address));
    command.Parameters.Add(new SqlParameter("@City", entity.City));
    command.Parameters.Add(new SqlParameter("@Region", entity.Region));
    command.Parameters.Add(new SqlParameter("@PostalCode", entity.PostalCode));
    command.Parameters.Add(new SqlParameter("@Country", entity.Country));
    command.Parameters.Add(new SqlParameter("@Phone", entity.Phone));
    command.Parameters.Add(new SqlParameter("@Fax", entity.Fax));
    
    connection.Open();
    command.ExecuteNonQuery();
    }
    
    
    }
    }
    }
    
    }
    

    此程式碼會於將新的記錄插入至 NewCustomers 資料表時執行,其會開啟 Northwind 資料庫連接,以及執行 InsertCustomer 預存程序,並提供 NewCustomer 實體中的值做為預存程序的參數。

測試應用程式

  1. 在 [程式碼編輯器] 中,於 NewCustomers_Inserting 方法中設定中斷點。

  2. 執行應用程式,並在 [CustomersSet] 螢幕上選擇 [加入] 按鈕。

  3. 在 [Customers] 螢幕上,填寫所有欄位,然後選擇 [儲存] 按鈕。

    請注意,為示範程式碼可以運作會擊發中斷點,並按 F5 鍵繼續。 如果您開啟 Northwind 資料庫中的 Customers 資料表,則會看到新的客戶記錄。

後續步驟

您可以如常建立 LightSwitch 螢幕和商務邏輯,並使用預存程序快速且輕鬆地製作正常運作的應用程式。

請參閱

工作

如何:在 LightSwitch 中執行預存程序

其他資源

資料:應用程式背後的資訊