前两天Insus.NET实现一个功能《在数据库中提供只读数据》http://www.cnblogs.com/insus/p/4384411.html ,在数据库中为程序提供静态数据。
它是在Clr存储过程实现。现想使用另外一种方式来解决。如果提供这些静态数需要与其它数据结合Join等,这样象前面的存储过程,有些不太好操作。那我们可以把它实现在table-valued function。
创建一个model:
打开SqlFunction类,编写table-valued函数:
可复制代码:
[SqlFunction(DataAccess = DataAccessKind.None, FillRowMethodName = "FillRow_1A", TableDefinition = "Id TINYINT,Name NVARCHAR(30), [key] NVARCHAR(30)") ] public static IEnumerable Tvf_SiteInfor() { FruitEntity fe = new FruitEntity(); return fe.GetData(); } private static void FillRow_1A(object source, out SqlByte id, out SqlChars name, out SqlChars key) { Si obj = (Si)source; id = new SqlByte(obj.Id); name = new SqlChars(obj.Name); key = new SqlChars(obj.Key); }
IF EXISTS (SELECT * FROM sys.objects WHERE name = 'Tvf_SiteInfor') DROP FUNCTION Tvf_SiteInfor; GO IF EXISTS (SELECT * FROM sys.assemblies WHERE name = 'FruitClr') DROP ASSEMBLY FruitClr; GO CREATE ASSEMBLY FruitClr FROM 'E:FruitClr.dll' WITH PERMISSION_SET = SAFE; GO CREATE FUNCTION Tvf_SiteInfor() RETURNS TABLE( Id TINYINT, [Name] NVARCHAR(30), [key] NVARCHAR(50) ) AS EXTERNAL NAME [FruitClr].UserDefinedFunctions.Tvf_SiteInfor; GO