如何:创建和运行 CLR SQL Server 用户定义的类型

王朝学院·作者佚名  2009-02-04
窄屏简体版  字體: |||超大  

通过将“用户定义的类型”添加到 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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航