如何:创建和运行 CLR SQL Server 用户定义的来源: 发布时间:星期二, 2009年2月3日 浏览:27次 评论:0
="t18">通过将“用户定义类型”添加到 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请参见下面第 2个举例 按 F5 生成、部署和调试 UDT有关不进行调试直接部署信息请参见如何:将 SQL Server 项目项部署到 SQL Server 中 在“输出”窗口 中查看结果然后选择“从此处显示输出:数据库输出” 举例 此举例创建个 Po 类型该类型使用方法和其他简单类型样使用 Serializable 和 SqlUserDefinedTypeAttribute 属性来修饰这个类声明SqlUserDefinedTypeAttribute Format 属性决定了 UDT 存储格式该类型通过实现 Parse 和 思路方法来实现串转换此外该类型还实现两个属性过程以便为此类所表示点获取和设置 X 和 Y 值 Visual Basic 复制代码 Imports Imports .Data.SqlTypes Imports Microsoft.SqlServer.Server <Serializable> _ <SqlUserDefinedType(Format.Native)> _ Public Structure Po 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 Po Get Dim pt As Po = New Po pt.is_Null = True Return pt End Get End Property Public Overrides Function 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 Po If s = SqlString.Null Then Return Null End If If s. = SqlString.Null. Then Return Null End If If s.IsNull Then Return Null End If 'Parse input here to separate out coordinates Dim str As String = Convert.(s) Dim xy As String = str.Split(":"c) Dim pt As New Po 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 Result As String = "" Select Case m_x Case 0 Result = "center" Case Is > 0 Result = "right" Case Is < 0 Result = "left" End Select Select Case m_y Case 0 Result = Result & " center" Case Is > 0 Result = Result & " top" Case Is < 0 Result = Result & " bottom" End Select Return Result End Function End Structure C# 复制代码 using ; using .Data.SqlTypes; using Microsoft.SqlServer.Server; [Serializable] [SqlUserDefinedType(Format.Native)] public struct Po : INullable { private Int32 m_x; private Int32 m_y; private bool is_Null; public Int32 X { get { (this.m_x); } { m_x = value; } } public Int32 Y { get { (this.m_y); } { m_y = value; } } public bool IsNull { get { is_Null; } } public Po Null { get { Po pt = Po; pt.is_Null = true; (pt); } } public override { (this.IsNull) { "NULL"; } { this.m_x + ":" + this.m_y; } } public Po Parse(SqlString s) { (s.IsNull) { Null; } // Parse input here to separate out coordinates str = Convert.(s); xy = str.Split(':'); Po pt = Po; pt.X = Convert.ToInt32(xy[0]); pt.Y = Convert.ToInt32(xy[1]); (pt); } public SqlString Quadrant { (m_x 0 && m_y 0) { "centered"; } SqlString Return = ""; (m_x 0) { Return = "center"; } (m_x > 0) { Return = "right"; } (m_x < 0) { Return = "left"; } (m_y 0) { Return = Return + " center"; } (m_y > 0) { Return = Return + " top"; } (m_y < 0) { Return = Return + " bottom"; } Return; } } C 复制代码 # "stdafx.h" #using <.dll> #using <.Data.dll> #using <.Xml.dll> using ; using ::Data; using ::Data::Sql; using ::Data::SqlTypes; using Microsoft::SqlServer::Server; // In order to debug your User-Defined Types, add the following to your debug.sql file: // // CREATE TABLE test_table (column1 Po) // // 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 Po : public INullable { private: Int32 m_x; Int32 m_y; bool is_Null; public: property Int32 X { Int32 get { (this->m_x); } void (Int32 value) { m_x = value; } } property Int32 Y { Int32 get { (this->m_y); } void (Int32 value) { m_y = value; } } virtual property bool IsNull { bool get { is_Null; } } property Po Null { Po get { Po pt; pt.is_Null = true; (pt); } } virtual String ^ override { (this->IsNull) { "NULL"; } { this->m_x + ":" + this->m_y; } } Po Parse(SqlString s) { (s.IsNull) { Null; } // Parse input here to separate out coordinates String ^str = Convert::(s); .gif' /><String ^> ^xy = str->Split(':'); Po pt; pt.X = Convert::ToInt32(xy[0]); pt.Y = Convert::ToInt32(xy[1]); (pt); } SqlString Quadrant { (m_x 0 && m_y 0) { "centered"; } SqlString Return = ""; (m_x 0) { Return = "center"; } (m_x > 0) { Return = "right"; } (m_x < 0) { Return = "left"; } (m_y 0) { Return = Return + SqlString(" center"); } (m_y > 0) { Return = Return + SqlString(" top"); } (m_y < 0) { Return = Return + SqlString(" bottom"); } Return; } }; 将用于执行和测试用户定义类型 (Po) 代码添加到“Test.sql”(Visual C 中为“debug.sql”)文件中该文件在项目“TestScripts”文件夹中例如若要检查新类型可以创建个使用此类型表下面举例演示如何在创建表过程中使用 Po 类型 复制代码 CREATE TABLE test_table (column1 Po) 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 0
相关文章
读者评论发表评论 |
|