前两天Insus.NET实现一个功能《在数据库中提供只读数据》http://www.cnblogs.com/insus/p/4384411.html ,在数据库中为程序提供静态数据。它是在Clr存储过程实现。现想使用另外一种方式来解决。如果提供这些静态数需要与其它数据结合Join等,这样象前面的存储过程,有些不太好操作。那我们可以把它实现在table-valued function。创建一个model:
再创建一个Entity:
打开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); }View Code
部署至SQL:可复制代码:
IF EXISTS (SELECT * FROM sys.objects WHERE name = 'Tvf_SiteInfor') DROP FUNCTION Tvf_SiteInfor;GOIF EXISTS (SELECT * FROM sys.assemblies WHERE name = 'FruitClr') DROP ASSEMBLY FruitClr;GOCREATE ASSEMBLY FruitClrFROM 'E:/FruitClr.dll' WITH PERMISSION_SET = SAFE;GOCREATE FUNCTION Tvf_SiteInfor() RETURNS TABLE( Id TINYINT, [Name] NVARCHAR(30), [key] NVARCHAR(50)) AS EXTERNAL NAME [FruitClr].UserDefinedFunctions.Tvf_SiteInfor; GOView Code
执行结果:
新闻热点
疑难解答