Share via


CLR 整合自訂屬性的概觀

.NET Framework 的 Common Language Runtime (CLR) 允許使用描述性的關鍵字 (稱為屬性)。這些屬性會針對許多元素 (如方法和類別) 提供其他資訊。屬性會隨著物件的中繼資料儲存在組件中,而且可用來將程式碼描述給其他開發工具知道,或是影響 SQL Server 內的執行階段行為。

當您向 SQL Server 註冊 CLR 常式時,SQL Server 會衍生一組有關此常式的屬性。這些常式屬性會決定該常式的功能,包括是否可以為此常式建立索引。例如,將 DataAccess 屬性設定為 DataAccessKind.Read 可讓您從 CLR 函數內的 SQL Server 使用者資料表存取資料。下列範例示範一個簡單案例,其中會設定 DataAccess 屬性,讓您從使用者資料表 table1 方便地存取資料。

using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Data.SqlClient;

public partial class UserDefinedFunctions
{
    [SqlFunction(DataAccess = DataAccessKind.Read)]
    public static string func1()
    {
        // Open a connection and create a command
        SqlConnection conn = new SqlConnection("context connection = true");
        conn.Open();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "SELECT str_val FROM table1 WHERE int_val = 10";
        // where table1 is a user table
        // Execute this command 
        SqlDataReader rd = cmd.ExecuteReader();
        // Set string ret_val to str_val returned from the query
        string ret_val = rd.GetValue(0).ToString();
        rd.Close();
        return ret_val;
    }
}
Imports System
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server
Imports System.Data.SqlClient
 
Public partial Class UserDefinedFunctions
    <SqlFunction(DataAccess = DataAccessKind.Read)> _ 
    Public Shared Function func1() As String
        ' Open a connection and create a command
        Dim conn As SqlConnection = New SqlConnection("context connection = true") 
        conn.Open()
        Dim cmd As SqlCommand =  conn.CreateCommand() 
        cmd.CommandText = "SELECT str_val FROM table1 WHERE int_val = 10"
        ' where table1 is a user table
        ' Execute this command 
        Dim rd As SqlDataReader =  cmd.ExecuteReader() 
        ' Set string ret_val to str_val returned from the query
        Dim ret_val As String =  rd.GetValue(0).ToString() 
        rd.Close()
        Return ret_val
    End Function
End Class

如果是 Transact-SQL 常式,SQL Server 會直接從常式定義衍生常式屬性。如果是 CLR 常式,伺服器不會分析常式的主體來衍生這些屬性。您可以改為將自訂屬性用於使用 .NET Framework 語言實作的類別和類別成員。

CLR 常式、使用者定義型別和彙總所需的自訂屬性會定義在 Microsoft.SqlServer.Server 命名空間內。

請參閱

概念