SQL CLR 用户自定义类型
首先,请看这里来了解基于SQL CLR的用户自定义类型的好处及为什么要支持用户自定义SQL类型.
http://msdn.microsoft.com/zh-cn/library/k2e1fb36(VS.80).aspx
本文借鉴了:http://www.cnblogs.com/doc/archive/2009/02/11/1388513.html
Reference:http://msdn.microsoft.com/zh-cn/library/a8s4s5dz(VS.80).aspx
1.必须将类 结构标记为:Microsoft.SqlServer.Server.SqlUserDefinedType.
2. Format: Native,UserDefined....
如果是才用Format.Native,那么编译器将会安排结构和布局,你需要通过指定Serializable和StructLayout(LayoutKind.Sequential)属性来告诉编译器你期望的布局方式。并且Format.Native不需要显示指定MaxByteSize. 但他有个DrawBack,你只能是值类型.
如果你设置IsByteOrdered 属性为true。你将可以使用ORDER BY语句来排序,并且你可以将这个列来定义为主键.
1. Exec sp_configure 'clr_enabled',0
reconfigure
2.
Create assembly Person from 'g:\TestProject.dll' with permission_set=safe
Create type Person external name Person.[Person]
3.
create table UdtTest (Id int not null, p Person not null)
insert into UdtTest values(1, 'David,24,男')
select id, convert(nvarchar(25),p) from UdtTest
drop table UdtTest
http://msdn.microsoft.com/zh-cn/library/k2e1fb36(VS.80).aspx
本文借鉴了:http://www.cnblogs.com/doc/archive/2009/02/11/1388513.html
Reference:http://msdn.microsoft.com/zh-cn/library/a8s4s5dz(VS.80).aspx
1.必须将类 结构标记为:Microsoft.SqlServer.Server.SqlUserDefinedType.
2. Format: Native,UserDefined....
如果是才用Format.Native,那么编译器将会安排结构和布局,你需要通过指定Serializable和StructLayout(LayoutKind.Sequential)属性来告诉编译器你期望的布局方式。并且Format.Native不需要显示指定MaxByteSize. 但他有个DrawBack,你只能是值类型.
如果你设置IsByteOrdered 属性为true。你将可以使用ORDER BY语句来排序,并且你可以将这个列来定义为主键.
using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.UserDefined, MaxByteSize = 1024)]
public struct Person : INullable, IBinarySerialize
{
public override string ToString()
{
// 用您的代码替换下列代码
return FormatU();
}
public bool IsNull
{
get
{
// 在此处放置代码
return m_Null;
}
}
public static Person Null
{
get
{
Person h = new Person();
h.m_Null = true;
return h;
}
}
public static Person Parse(SqlString s)
{
if (s.IsNull)
return Null;
Person u = new Person();
string value = s.Value;
if (value == "null") return Null;
string[] parts = value.Split(',');
u.name = parts[0];
u.age = ParseAge(parts[1]);
u.sex = parts[2];
return u;
}
// 这是占位符方法
public string FormatU()
{
//在此处插入方法代码
return string.Format("名称:{0},年龄:{1},性别:{2}", name, age, sex);
}
// 这是占位符静态方法
public static int ParseAge(string str)
{
//在此处插入方法代码
return int.Parse(str.Substring(0, str.Length));
}
// 这是占位符字段成员
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
// 私有成员
private bool m_Null;
public byte[] b;
public void Read(BinaryReader r)
{
name = r.ReadString();
sex = r.ReadString();
age = r.ReadInt32();
m_Null = r.ReadBoolean();
}
public void Write(BinaryWriter w)
{
w.Write(name);
w.Write(sex);
w.Write(age);
w.Write(m_Null);
}
}
配置:using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
[Serializable]
[Microsoft.SqlServer.Server.SqlUserDefinedType(Format.UserDefined, MaxByteSize = 1024)]
public struct Person : INullable, IBinarySerialize
{
public override string ToString()
{
// 用您的代码替换下列代码
return FormatU();
}
public bool IsNull
{
get
{
// 在此处放置代码
return m_Null;
}
}
public static Person Null
{
get
{
Person h = new Person();
h.m_Null = true;
return h;
}
}
public static Person Parse(SqlString s)
{
if (s.IsNull)
return Null;
Person u = new Person();
string value = s.Value;
if (value == "null") return Null;
string[] parts = value.Split(',');
u.name = parts[0];
u.age = ParseAge(parts[1]);
u.sex = parts[2];
return u;
}
// 这是占位符方法
public string FormatU()
{
//在此处插入方法代码
return string.Format("名称:{0},年龄:{1},性别:{2}", name, age, sex);
}
// 这是占位符静态方法
public static int ParseAge(string str)
{
//在此处插入方法代码
return int.Parse(str.Substring(0, str.Length));
}
// 这是占位符字段成员
private int age;
public int Age
{
get { return age; }
set { age = value; }
}
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
private string sex;
public string Sex
{
get { return sex; }
set { sex = value; }
}
// 私有成员
private bool m_Null;
public byte[] b;
public void Read(BinaryReader r)
{
name = r.ReadString();
sex = r.ReadString();
age = r.ReadInt32();
m_Null = r.ReadBoolean();
}
public void Write(BinaryWriter w)
{
w.Write(name);
w.Write(sex);
w.Write(age);
w.Write(m_Null);
}
}
1. Exec sp_configure 'clr_enabled',0
reconfigure
2.
Create assembly Person from 'g:\TestProject.dll' with permission_set=safe
Create type Person external name Person.[Person]
3.
create table UdtTest (Id int not null, p Person not null)
insert into UdtTest values(1, 'David,24,男')
select id, convert(nvarchar(25),p) from UdtTest
drop table UdtTest
浙公网安备 33010602011771号