国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > SQL Server > 正文

如何:創建和運行 CLR SQL Server 用戶定義的類型

2024-08-31 00:52:58
字體:
來源:轉載
供稿:網友

通過將“用戶定義的類型”添加到 SQL Server 項目,可以創建 SQL 用戶定義的類型。部署成功后,可以將該類型用于可使用系統類型的所有上下文,包括列定義、變量、參數、函數結果、光標、觸發器和復制等。UDT 為用戶提供了 SQL Server 數據類型系統的擴展性,還提供了定義復雜結構類型的功能。

注意 
在默認情況下,Microsoft SQL Server 中關閉了公共語言運行庫 (CLR) 集成功能。必須啟用該功能才能使用 SQL Server 項目項。若要啟用 CLR 集成,請使用 sp_configure 存儲過程的“啟用 clr”選項。有關更多信息,請參見啟用 CLR 集成。
 
注意 
顯示的對話框和菜單命令可能會與幫助中的描述不同,具體取決于您現用的設置或版本。若要更改設置,請在“工具”菜單上選擇“導入和導出設置”。有關更多信息,請參見 Visual Studio 設置。
 

創建用戶定義的類型
創建 SQL 用戶定義的類型
打開一個現有的“SQL Server 項目”,或者創建一個新項目。有關更多信息,請參見如何:創建 SQL Server 項目。

從“項目”菜單中選擇“添加新項”。

在“添加新項”對話框 中選擇“用戶定義的類型”。

為新的 UDT 鍵入“名稱”。

添加用于定義和創建 UDT 的代碼。請參見下面的第一個示例。

注意 
C++ 示例在編譯時必須使用 /clr:safe 編譯器選項。
 

對于 Visual Basic 和 Visual C#,在“解決方案資源管理器”中,打開“TestScripts”文件夾,再雙擊“Test.sql”文件。

對于 Visual C++,在“解決方案資源管理器”中,雙擊“debug.sql”文件。

將代碼添加到“Test.sql”(Visual C++ 中為“debug.sql”)文件中以執行 UDT。請參見下面的第二個示例。

按 F5 生成、部署和調試 UDT。有關不進行調試直接部署的信息,請參見如何:將 SQL Server 項目項部署到 SQL Server 中。

在“輸出”窗口 中查看結果,然后選擇“從此處顯示輸出:數據庫輸出”。

示例
此示例創建一個 Point 類型,該類型的用法與其他簡單類型一樣。使用 Serializable 和 SqlUserDefinedTypeAttribute 屬性來修飾這個類聲明。SqlUserDefinedTypeAttribute 的 Format 屬性決定了 UDT 的存儲格式。該類型通過實現 Parse 和 ToString 方法來實現字符串轉換。此外,該類型還實現兩個屬性過程,以便為此類所表示的點獲取和設置 X 和 Y 的值。

Visual Basic 復制代碼
Imports System
Imports System.Data.SqlTypes
Imports Microsoft.SqlServer.Server

<Serializable()> _
<SqlUserDefinedType(Format.Native)> _
Public Structure Point
    Implements INullable

    PRivate m_x As Int32
    Private m_y As Int32
    Private is_Null As Boolean


    Public Property X() As Int32
        Get
            Return (Me.m_x)
        End Get
        Set(ByVal Value As Int32)
            m_x = Value
        End Set
    End Property


    Public Property Y() As Int32
        Get
            Return (Me.m_y)
        End Get
        Set(ByVal Value As Int32)
            m_y = Value
        End Set
    End Property


    Public ReadOnly Property IsNull() As Boolean Implements INullable.IsNull
        Get
            Return is_Null
        End Get
    End Property


    Public Shared ReadOnly Property Null() As Point
        Get
            Dim pt As Point = New Point
            pt.is_Null = True
            Return pt
        End Get
    End Property


    Public Overrides Function ToString() As String
        If Me.IsNull() Then
            Return Nothing
        Else
            Return Me.m_x & ":" & Me.m_y
        End If
    End Function


    Public Shared Function Parse(ByVal s As SqlString) As Point
        If s = SqlString.Null Then
            Return Null
        End If

        If s.ToString() = SqlString.Null.ToString() Then
            Return Null
        End If

        If s.IsNull Then
            Return Null
        End If

        'Parse input string here to separate out coordinates
        Dim str As String = Convert.ToString(s)
        Dim xy() As String = str.Split(":"c)

        Dim pt As New Point()
        pt.X = CType(xy(0), Int32)
        pt.Y = CType(xy(1), Int32)
        Return (pt)
    End Function


    Public Function Quadrant() As SqlString

        If m_x = 0 And m_y = 0 Then
            Return "centered"
        End If

        Dim stringResult As String = ""

        Select Case m_x
            Case 0
                stringResult = "center"
            Case Is > 0
                stringResult = "right"
            Case Is < 0
                stringResult = "left"
        End Select

        Select Case m_y
            Case 0
                stringResult = stringResult & " center"
            Case Is > 0
                stringResult = stringResult & " top"
            Case Is < 0
                stringResult = stringResult & " bottom"
        End Select

        Return stringResult
    End Function
End Structure
C# 復制代碼
using System;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;

[Serializable()]
[SqlUserDefinedType(Format.Native)]
public struct Point : INullable
{
    private Int32 m_x;
    private Int32 m_y;
    private bool is_Null;


    public Int32 X
    {
        get
        {
            return (this.m_x);
        }
        set
        {
            m_x = value;
        }
    }


    public Int32 Y
    {
        get
        {
            return (this.m_y);
        }
        set
        {
            m_y = value;
        }
    }


    public bool IsNull
    {
        get
        {
            return is_Null;
        }
    }


    public static Point Null
    {
        get
        {
            Point pt = new Point();
            pt.is_Null = true;
            return (pt);
        }
    }


    public override string ToString()
    {
        if (this.IsNull)
        {
            return "NULL";
        }
        else
        {
            return this.m_x + ":" + this.m_y;
        }
    }


    public static Point Parse(SqlString s)
    {
        if (s.IsNull)
        {
            return Null;
        }

        // Parse input string here to separate out coordinates
        string str = Convert.ToString(s);
        string[] xy = str.Split(':');

        Point pt = new Point();
        pt.X = Convert.ToInt32(xy[0]);
        pt.Y = Convert.ToInt32(xy[1]);
        return (pt);
    }


    public SqlString Quadrant()
    {
        if (m_x == 0 && m_y == 0)
        {
            return "centered";
        }

        SqlString stringReturn = "";

        if (m_x == 0)
        {
            stringReturn = "center";
        }
        else if (m_x > 0)
        {
            stringReturn = "right";
        }
        else if (m_x < 0)
        {
            stringReturn = "left";
        }

        if (m_y == 0)
        {
            stringReturn = stringReturn + " center";
        }
        else if (m_y > 0)
        {
            stringReturn = stringReturn + " top";
        }
        else if (m_y < 0)
        {
            stringReturn = stringReturn + " bottom";
        }

        return stringReturn;
    }
}
C++ 復制代碼
#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::SqlTypes;
using namespace Microsoft::SqlServer::Server;

// In order to debug your User-Defined Types, add the following to your debug.sql file:
//
// CREATE TABLE test_table (column1 Point)
//
// INSERT INTO test_table (column1) VALUES ('1:2')
// INSERT INTO test_table (column1) VALUES ('-2:3')
// INSERT INTO test_table (column1) VALUES ('-3:-4')
//
// SELECT column1.Quadrant() FROM test_table
//
// DROP TABLE test_table
//
[Serializable]
[SqlUserDefinedType(Format::Native)]
public value struct Point : public INullable
{
private:
    Int32 m_x;
    Int32 m_y;
    bool is_Null;

public:
    property Int32 X
    {
        Int32 get() { return (this->m_x); }
        void set(Int32 value) { m_x = value; }
    }

    property Int32 Y
    {
        Int32 get() { return (this->m_y); }
        void set(Int32 value) { m_y = value; }
    }

    virtual property bool IsNull
    {
        bool get() { return is_Null; }
    }

    static property Point Null
    {
        Point get()
        {
            Point pt;
            pt.is_Null = true;
            return (pt);
        }
    }

    virtual String ^ToString() override
    {
        if (this->IsNull)
        {
            return "NULL";
        }
        else
        {
            return this->m_x + ":" + this->m_y;
        }
    }


    static Point Parse(SqlString s)
    {
        if (s.IsNull)
        {
            return Null;
        }

        // Parse input string here to separate out coordinates
        String ^str = Convert::ToString(s);
        array<String ^> ^xy = str->Split(':');

        Point pt;
        pt.X = Convert::ToInt32(xy[0]);
        pt.Y = Convert::ToInt32(xy[1]);
        return (pt);
    }


    SqlString Quadrant()
    {
        if (m_x == 0 && m_y == 0)
        {
            return "centered";
        }

        SqlString stringReturn = "";

        if (m_x == 0)
        {
            stringReturn = "center";
        }
        else if (m_x > 0)
        {
            stringReturn = "right";
        }
        else if (m_x < 0)
        {
            stringReturn = "left";
        }

        if (m_y == 0)
        {
            stringReturn = stringReturn + SqlString(" center");
        }
        else if (m_y > 0)
        {
            stringReturn = stringReturn + SqlString(" top");
        }
        else if (m_y < 0)
        {
            stringReturn = stringReturn + SqlString(" bottom");
        }

        return stringReturn;
    }
};

將用于執行和測試用戶定義類型 (Point) 的代碼添加到“Test.sql”(Visual C++ 中為“debug.sql”)文件中,該文件在項目的“TestScripts”文件夾中。例如,若要檢查新類型,可以創建一個使用此類型的表。下面的示例演示如何在創建表的過程中使用 Point 類型。

 復制代碼
CREATE TABLE test_table (column1 Point)
go

INSERT INTO test_table (column1) VALUES ('1:2')
INSERT INTO test_table (column1) VALUES ('-2:3')
INSERT INTO test_table (column1) VALUES ('-3:-4')

select column1.Quadrant() from test_table


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 金寨县| 德兴市| 瑞金市| 韶山市| 宝清县| 察雅县| 定陶县| 怀集县| 二连浩特市| 南宁市| 连山| 金川县| 新源县| 康平县| 五峰| 合作市| 西华县| 锡林郭勒盟| 乌拉特前旗| 咸宁市| 合作市| 凤阳县| 白山市| 呼和浩特市| 四川省| 招远市| 兴宁市| 通道| 聂拉木县| 昂仁县| 瑞安市| 虹口区| 花垣县| 福州市| 响水县| 阳城县| 东丽区| 重庆市| 平武县| 丹巴县| 赤壁市|