HOW TO:建立及執行 CLR SQL Server 預存程序

更新:2007 年 11 月

將 [預存程序] 項目加入至 SQL Server 專案,建立 SQL 預存程序。順利部署至執行 SQL Server 的電腦之後,便可如同其他預存程序般,呼叫和執行以 Managed 程式碼所建立的預存程序。

注意事項:

Microsoft SQL Server 中的 Common Language Runtime (CLR) 整合功能預設為關閉,在使用 SQL Server 專案項目時則必須啟用這個功能。若要啟用 CLR 整合,請使用 sp_configure 預存程序的 clr enabled 選項。如需詳細資訊,請參閱啟用 CLR 整合

注意事項:

您所看見的對話方塊與功能表命令可能會與 [說明] 所描述的有所不同,視您所使用的設定或版本而定。如果要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。如需詳細資訊,請參閱 Visual Studio 設定

建立 SQL Server 預存程序

若要建立 SQL Server 預存程序

  1. 開啟現有的 [SQL Server 專案],或建立一個新專案。如需詳細資訊,請參閱 HOW TO:建立 SQL Server 專案

  2. 從 [專案] 功能表中選取 [加入新項目]。

  3. 加入新項目對話方塊中,選取 [預存程序]。

  4. 為新的預存程序輸入 [名稱]。

  5. 加入執行預存程序時執行的程式碼。請參閱下列範例。

    注意事項:

    C++ 範例必須使用 /clr:safe 編譯器選項進行編譯。

  6. 若為 Visual Basic 和 Visual C#,請在 [方案總管] 中,開啟 [TestScripts] 資料夾,並按兩下 [Test.sql] 檔案。

    若為 Visual C++,請在 [方案總管] 中,開啟 [debug.sql] 檔案。

  7. 將程式碼加入至 Test.sql (在 Visual C++ 中為 debug.sql) 檔案,以執行預存程序。請參閱下列第二個範例。

  8. 按下 F5,即可建置、部署和偵錯預存程序。如需不偵錯就進行部署的詳細資訊,請參閱 HOW TO:將 SQL Server 專案項目部署至 SQL Server

  9. 輸出視窗中,檢視結果,並選取 [顯示輸出來源: 資料庫輸出]。

範例

下列程式碼範例會建立預存程序,將資料錄插入 Adventure Works 範例資料庫的 Currency 資料表。建立預存程序之後,將它部署至 SQL Server。如需詳細資訊,請參閱 HOW TO:將 SQL Server 專案項目部署至 SQL Server

Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server

Partial Public Class StoredProcedures

    <SqlProcedure()> _
    Public Shared Sub InsertCurrency( _
        ByVal currencyCode As SqlString, ByVal name As SqlString)

        Using conn As New SqlConnection("context connection=true")

            Dim InsertCurrencyCommand As New SqlCommand()
            Dim currencyCodeParam As New SqlParameter("@CurrencyCode", SqlDbType.NVarChar)
            Dim nameParam As New SqlParameter("@Name", SqlDbType.NVarChar)

            currencyCodeParam.Value = currencyCode
            nameParam.Value = name


            InsertCurrencyCommand.Parameters.Add(currencyCodeParam)
            InsertCurrencyCommand.Parameters.Add(nameParam)

            InsertCurrencyCommand.CommandText = _
                "INSERT Sales.Currency (CurrencyCode, Name, ModifiedDate)" & _
                " VALUES(@CurrencyCode, @Name, GetDate())"

            InsertCurrencyCommand.Connection = conn

            conn.Open()
            InsertCurrencyCommand.ExecuteNonQuery()
            conn.Close()
        End Using
    End Sub
End Class
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;


public partial class StoredProcedures
{
    [SqlProcedure()]
    public static void InsertCurrency_CS(
        SqlString currencyCode, SqlString name)
    {
        using (SqlConnection conn = new SqlConnection("context connection=true"))
        {
            SqlCommand InsertCurrencyCommand = new SqlCommand();
            SqlParameter currencyCodeParam = new SqlParameter("@CurrencyCode", SqlDbType.NVarChar);
            SqlParameter nameParam = new SqlParameter("@Name", SqlDbType.NVarChar);

            currencyCodeParam.Value = currencyCode;
            nameParam.Value = name;

            InsertCurrencyCommand.Parameters.Add(currencyCodeParam);
            InsertCurrencyCommand.Parameters.Add(nameParam);

            InsertCurrencyCommand.CommandText =
                "INSERT Sales.Currency (CurrencyCode, Name, ModifiedDate)" +
                " VALUES(@CurrencyCode, @Name, GetDate())";

            InsertCurrencyCommand.Connection = conn;

            conn.Open();
            InsertCurrencyCommand.ExecuteNonQuery();
            conn.Close();
        }
    }
}
#include "stdafx.h"

#using <System.dll>
#using <System.Data.dll>
#using <System.Xml.dll>

using namespace System;
using namespace System::Data;
using namespace System::Data::Sql;
using namespace System::Data::SqlClient;
using namespace System::Data::SqlTypes;
using namespace Microsoft::SqlServer::Server;

// In order to debug your Stored Procedure, add the following to your debug.sql file:
//
// EXEC InsertCurrency_CPP 'AAA', 'Currency Test'
// SELECT * FROM Sales.Currency WHERE CurrencyCode = 'AAA'

public ref class StoredProcedures
{
public:
    [SqlProcedure]
    static void InsertCurrency_CPP(SqlString currencyCode, SqlString name)
    {
        SqlConnection ^conn = gcnew SqlConnection("context connection=true");

        SqlCommand ^insertCurrencyCommand = gcnew SqlCommand();
        SqlParameter ^currencyCodeParam =
            gcnew SqlParameter("@CurrencyCode", SqlDbType::NVarChar);
        SqlParameter ^nameParam =
            gcnew SqlParameter("@Name", SqlDbType::NVarChar);

        insertCurrencyCommand->CommandText =
            "insert Sales.Currency(CurrencyCode, Name, ModifiedDate)" +
            " values(@CurrencyCode, @Name)";
        insertCurrencyCommand->Connection = conn;

        conn->Open();
        insertCurrencyCommand->ExecuteNonQuery();

        conn->Close();
    }
};

將程式碼加入至專案中 TestScripts 資料夾的 Test.sql (在 Visual C++ 中為 debug.sql) 檔案,以執行和測試預存程序。例如,如果您部署一個預存程序,則呼叫 EXEC <StoredProcedureName>,並傳入任何所需參數,即可執行此預存程序。如果預存程序沒有傳回任何值,請插入額外程式碼,驗證資料是否受預存程序影響。

EXEC InsertCurrency 'AAA', 'Currency Test'
SELECT * from Sales.Currency where CurrencyCode = 'AAA'

請參閱

工作

HOW TO:建立 SQL Server 專案

HOW TO:建立及執行 CLR SQL Server 預存程序

HOW TO:建立及執行 CLR SQL Server 觸發程序

HOW TO:建立及執行 CLR SQL Server 彙總

HOW TO:建立及執行 CLR SQL Server 使用者定義函式

HOW TO:建立及執行 CLR SQL Server 使用者定義型別

逐步解說:使用 Managed 程式碼建立預存程序

HOW TO:偵錯 SQL CLR 預存程序

概念

SQL Server CLR 整合簡介 (ADO.NET)

使用 Managed 程式碼建立資料庫物件的好處

SQL Server 專案的項目範本

參考

SQL Server 專案和資料庫物件的屬性

其他資源

SQL CLR 資料庫偵錯