自定义函数和存储过程在.net里其实都是方法。只是方法上方标注[Microsoft.SqlServer.Server.SqlProcedure]
和[Microsoft.SqlServer.Server.SqlFunction]不同而已。自定义函数又分TVF函数和Scalar两种,最大区别在于TVF返回表后者返回Scalar(标量),这一篇我们做一下比较。
先看两段代码
存储过程:

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;



public partial class StoredProcedures



{

//这里是告诉sqlserver,这个方法要注册成存储过程

//我感觉[Attribute]这个东西翻译成标签更形像:)

[Microsoft.SqlServer.Server.SqlProcedure]

public static void TestStoredProcedure(string name, ref string outstr)



{

// 在此处放置代码

outstr = "hello," + name;


using (SqlConnection cn = new SqlConnection())



{

//使用上下文链接也就是当前数据库链接

cn.ConnectionString = "context c";

using (SqlCommand cmd = cn.CreateCommand())



{

cmd.CommandText = "Select * from userinfo";

cn.Open();

//SqlContext.Pipe.Send这个方法输出结果集

//接受SqlDataReader,SqlDataRecord和string

SqlContext.Pipe.Send(cmd.ExecuteReader());

//你也可以用下边这样

//SqlContext.Pipe.ExecuteAndSend(cmd);

}

}

}

};

执行存储过程

DECLARE @name nvarchar(4000)

DECLARE @outstr nvarchar(4000)

set @name='david fan'

-- TODO: 在此处设置参数值。

EXECUTE [TestProject].[dbo].[TestStoredProcedure]

@name

,@outstr OUTPUT

print @outstr
结果如下

输出参数返回值

自定义函数
一,TVF函数
示例函数的作用是搜索目录下的某一类型的文件

using System;

using System.Data;

using System.Data.Sql;

using System.Data.SqlTypes;

using Microsoft.SqlServer.Server;

using System.Collections;

using System.IO;

using System.Security.Principal;


public partial class UserDefinedFunctions



{

//需要返回一个表时用TVF(streaming table-valued function)

//可以用select from 语句查询这个方法的返回

//TVF需要返回Ienumerable接口,例如:Array,这里返回一个数组


//FillRowMethodName为填充表行的方法

//TableDefinition为表结构,对应FillRowMethodName方法的参数

[Microsoft.SqlServer.Server.SqlFunction(FillRowMethodName = "BuildRow",

TableDefinition = "Name nvarchar(32), Length bigint, Modified DateTime")]

public static IEnumerable FileListCs(string directoryName, string pattern)



{

FileInfo[] files;

//模拟当前SQL安全上下文

WindowsImpersonationContext OriginalContext= SqlContext.WindowsIdentity.Impersonate();

try



{

DirectoryInfo di = new DirectoryInfo(directoryName);

files = di.GetFiles(pattern);

}

finally



{

if (OriginalContext != null)



{

OriginalContext.Undo();

}

}

return files;

}


public static void BuildRow(object Obj,

ref SqlString fileName,

ref SqlInt64 fileLength,

ref SqlDateTime fileModified)



{

if (Obj != null)



{

FileInfo file = (FileInfo)Obj;

fileName = file.Name;

fileLength = file.Length;

fileModified = file.LastWriteTime;

}

else



{
和[Microsoft.SqlServer.Server.SqlFunction]不同而已。自定义函数又分TVF函数和Scalar两种,最大区别在于TVF返回表后者返回Scalar(标量),这一篇我们做一下比较。
先看两段代码
存储过程:











































执行存储过程








结果如下
输出参数返回值
自定义函数
一,TVF函数
示例函数的作用是搜索目录下的某一类型的文件




































































