昨天完成了一个最简单的在数据库中创建标量值函数,今天主要完成表值函数,存储过程和用户定义类型在和.NET结合下的使用方法.
1,表值函数
所谓表值函数就是说这个函数返回的结果是一个Table,而不是单个的值.
在.NET 中创建这样的函数,返回的结果是一个IEnumerable接口.这个接口非常灵活,所有.NET数组集合等都是实现了该接口的.下面我们举一个简单的例子来说明.
在VS2005中创建一个类Student,这个就是我们要返回的表的内容,类下面有属性int Age,string sName,DateTime Birthday,int SID;
然后在另外一个类UserFunction中写入如下代码:
在SQL Server2005中使用 .NET程序集_数据库[SqlFunction(FillRowMethodName="FillRow")]
在SQL Server2005中使用 .NET程序集_数据库public static IEnumerable GetStudent()
在SQL Server2005中使用 .NET程序集_数据库_03在SQL Server2005中使用 .NET程序集_数据库_04...{
在SQL Server2005中使用 .NET程序集_数据库_05    Hashtable hash = new Hashtable();
在SQL Server2005中使用 .NET程序集_数据库_05    for(int i=0;i<3;i++)
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08    ...{
在SQL Server2005中使用 .NET程序集_数据库_05     Student s = new Student();
在SQL Server2005中使用 .NET程序集_数据库_05     s.SID = i;
在SQL Server2005中使用 .NET程序集_数据库_05     ...
在SQL Server2005中使用 .NET程序集_数据库_05     hash.Add(i, s);
在SQL Server2005中使用 .NET程序集_数据库_13    }
在SQL Server2005中使用 .NET程序集_数据库_05    return hash.Values;
在SQL Server2005中使用 .NET程序集_数据库_15}
在SQL Server2005中使用 .NET程序集_数据库public static void FillRow(object obj, ref SqlInt32 id, ref SqlString name, ref SqlDateTime bir, ref SqlInt32 age)
在SQL Server2005中使用 .NET程序集_数据库_03在SQL Server2005中使用 .NET程序集_数据库_04...{
在SQL Server2005中使用 .NET程序集_数据库_05    Student s = (obj as Student);
在SQL Server2005中使用 .NET程序集_数据库_05    id = s.SID;
在SQL Server2005中使用 .NET程序集_数据库_05    name = s.sName;
在SQL Server2005中使用 .NET程序集_数据库_05    bir = s.Birthday;
在SQL Server2005中使用 .NET程序集_数据库_05    age = s.Age;
在SQL Server2005中使用 .NET程序集_数据库_05    
在SQL Server2005中使用 .NET程序集_数据库_15}
在SQL Server2005中使用 .NET程序集_数据库
第一个属性中指定FillRowMethodName就是为了将返回的IEnumerable接口中的数据进行转换,将数据库无法认识的集合转换为数据库人生的字段.下面的函数FillRow就是具体转换的过程.
这样写完成以后,在数据库那边添加好这个程序集,然后就可以创建表值函数了:
在SQL Server2005中使用 .NET程序集_数据库create function BuildTable()
在SQL Server2005中使用 .NET程序集_数据库returns table(SID int,[sName] nvarchar(100),Birthday datetime,Age int)
在SQL Server2005中使用 .NET程序集_数据库as
在SQL Server2005中使用 .NET程序集_数据库external name SQLFunction.[SQLFunction.UserFunction].GetStudent

这儿就不用太多的解释了,就是将名为SQLFunction的程序集中的[名字空间.类].方法添加到BuildTable函数中.
这儿需要说明一下就是数据库中的类型和.NET中的类型的对应问题.int,datetime就不说了,主要是.NET中的string,在数据库中没有string类型,在FillRow中指出了类型SqlString,而这个类型的对应是nchar,nvarchar.这儿不能对应char,varchar,我不知道为什么必须是对应nchar的.所以上面我们写的是[sName] nvarchar(100).
大功告成,测试一下,输入语句select * from BuildTable()看看返回你的表没有.
2.存储过程
CLR存储过程和CLR函数非常相似,不过有几点更高的能力:
CLR存储过程可以有一个返回值,也可以写输出参数,可以返回消息给客户程序,可以调用DDL和DML语句.
.NET创建存储过程要编写为静态函数,然后加上SqlProcedure属性.
比如我们写一个简单的存储过程
在SQL Server2005中使用 .NET程序集_数据库[SqlProcedure]
在SQL Server2005中使用 .NET程序集_数据库public static int Add(int a, int b)
在SQL Server2005中使用 .NET程序集_数据库_03在SQL Server2005中使用 .NET程序集_数据库_04...{
在SQL Server2005中使用 .NET程序集_数据库_05    return a + b;
在SQL Server2005中使用 .NET程序集_数据库_15}

然后在数据库中写入:
在SQL Server2005中使用 .NET程序集_数据库create procedure Add2Num
在SQL Server2005中使用 .NET程序集_数据库@a int,@b int
在SQL Server2005中使用 .NET程序集_数据库as
在SQL Server2005中使用 .NET程序集_数据库external name SQLFunction.[SQLFunction.UserFunction].[Add]

整个代码我就不用解释了,和前面创建函数一样.
我们运行看看结果:
在SQL Server2005中使用 .NET程序集_数据库declare @a int
在SQL Server2005中使用 .NET程序集_数据库exec @a=Add2Num 10,12
在SQL Server2005中使用 .NET程序集_数据库print @a

3.用户定义类型(UDT)
要创建UDT类必须符合"UDT规范",.NET中的约束如下:
他们必须带SqlUserDefinedType 属性
必须带有Serializable属性
必须实现INullable接口
必须博阿訇公开和静态的Parse和ToString方法以用于转换数据类型字符串或逆向转换.
必须暴露数据元素为公开字段或公开属性.
好,那我们就创建一个简单的UDT复数类如下:
   
在SQL Server2005中使用 .NET程序集_数据库 [Serializable]
在SQL Server2005中使用 .NET程序集_数据库    [SqlUserDefinedType(Format.Native)]
在SQL Server2005中使用 .NET程序集_数据库    [StructLayout(LayoutKind.Sequential)]
在SQL Server2005中使用 .NET程序集_数据库   public class Complex:INullable
在SQL Server2005中使用 .NET程序集_数据库_03在SQL Server2005中使用 .NET程序集_数据库_04    ...{
在SQL Server2005中使用 .NET程序集_数据库_05       bool isNull=false;
在SQL Server2005中使用 .NET程序集_数据库_05       double real, imag;
在SQL Server2005中使用 .NET程序集_数据库_05       public bool IsNull
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08       ...{
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08           get ...{ return isNull; }
在SQL Server2005中使用 .NET程序集_数据库_13       }
在SQL Server2005中使用 .NET程序集_数据库_05       public double Real
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08       ...{
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08           get ...{ return real; }
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08           set ...{ real = value; }
在SQL Server2005中使用 .NET程序集_数据库_13       }
在SQL Server2005中使用 .NET程序集_数据库_05       public double Imag
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08       ...{
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08           get ...{ return imag; }
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08           set ...{ imag = value; }
在SQL Server2005中使用 .NET程序集_数据库_13       }
在SQL Server2005中使用 .NET程序集_数据库_05       public override string ToString()
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08       ...{
在SQL Server2005中使用 .NET程序集_数据库_05           if (isNull)
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08           ...{
在SQL Server2005中使用 .NET程序集_数据库_05               return "NULL";
在SQL Server2005中使用 .NET程序集_数据库_13           }
在SQL Server2005中使用 .NET程序集_数据库_05           else
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08           ...{
在SQL Server2005中使用 .NET程序集_数据库_05               return real + "," + imag;
在SQL Server2005中使用 .NET程序集_数据库_13           }
在SQL Server2005中使用 .NET程序集_数据库_13       }
在SQL Server2005中使用 .NET程序集_数据库_05        public static Complex Parse(SqlString s)
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08        ...{
在SQL Server2005中使用 .NET程序集_数据库_05            if (s == null || s.IsNull)
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08            ...{
在SQL Server2005中使用 .NET程序集_数据库_05                return null;
在SQL Server2005中使用 .NET程序集_数据库_13            }
在SQL Server2005中使用 .NET程序集_数据库_05            else
在SQL Server2005中使用 .NET程序集_数据库_07在SQL Server2005中使用 .NET程序集_数据库_08            ...{
在SQL Server2005中使用 .NET程序集_数据库_05                Complex c = new Complex();
在SQL Server2005中使用 .NET程序集_数据库_05                string str = Convert.ToString(s);
在SQL Server2005中使用 .NET程序集_数据库_05                string[] st = str.Split(',');
在SQL Server2005中使用 .NET程序集_数据库_05                c.real = Convert.ToDouble(st[0]);
在SQL Server2005中使用 .NET程序集_数据库_05                c.imag = Convert.ToDouble(st[1]);
在SQL Server2005中使用 .NET程序集_数据库_05                return c;
在SQL Server2005中使用 .NET程序集_数据库_13            }
在SQL Server2005中使用 .NET程序集_数据库_13        }
在SQL Server2005中使用 .NET程序集_数据库_15    }
编译好,在数据库中添加程序集后,我们运行如下代码:
在SQL Server2005中使用 .NET程序集_数据库create type Complex
在SQL Server2005中使用 .NET程序集_数据库external name SQLFunction.[SQLFunction.Complex]

这样我们就创建好了用户定义类型Complex.
数据库事例代码中有相关内容,参见:
\Program Files\Microsoft SQL Server\90\Samples\Engine\Programmability\CLR\UserDefinedDataType