sql server 扩展存储过程


C#代码:

using Microsoft.SqlServer.Server;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 
 
public partial class UserDefinedFunctions
{
    public static SqlString ExampleUDF()
    {
        return new SqlString("Hello");
    }
    [SqlFunction(IsDeterministic = true, IsPrecise = true)]
    public static bool RegExIsMatch(string pattern, string matchString)
    {
        Regex reg = new Regex(pattern.TrimEnd(null));
        return reg.Match(matchString.TrimEnd(null)).Success;
    }
}

SQL SERVER 代码

加载程序集

USE InvestorRelations
CREATE ASSEMBLY ExampleUDF
FROM 'E:\学习\SessionTest\TestKZCCGC\bin\Debug\TestKZCCGC.dll'

创建函数

CREATE FUNCTION ExampleUDFTwo()
RETURNS nvarchar(1000)
AS EXTERNAL NAME ExampleUDF.UserDefinedFunctions.ExampleUDF;

 表值函数:

C#代码

using Microsoft.SqlServer.Server;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
 
 
public partial class UserDefinedFunctions
{
    [SqlFunction(FillRowMethodName = "FillRow")]
    public static IEnumerable DirectoryList(string sRootDir, string sWildCard, bool bIncludeSubDirs)
    {
        ArrayList aFileArray = new ArrayList();
        DirectorySearch(sRootDir, sWildCard, bIncludeSubDirs, aFileArray);
        return aFileArray;
    }
    private static void DirectorySearch(string directory, string sWildCard, bool bIncludeSubDirs, ArrayList aFileArray)
    {
        GetFiles(directory, sWildCard, aFileArray);
        if (bIncludeSubDirs)
        {
            foreach (string d in Directory.GetDirectories(directory))
            {
                DirectorySearch(d, sWildCard, bIncludeSubDirs, aFileArray);
            }
        }
    }
    private static void GetFiles(string d, string sWildCard, ArrayList aFileArray)
    {
        foreach (string f in Directory.GetFiles(d, sWildCard))
        {
            FileInfo fi = new FileInfo(f);
            object[] column = new object[2];
            column[0] = fi.FullName;
            column[1] = fi.LastWriteTime;
            aFileArray.Add(column);
        }
    }
    private static void FillRow(object obj, out string filename, out DateTime date)
    {
        object[] row = (object[])obj;
        filename = (string)row[0];
        date = (DateTime)row[1];
    }
}

SQL server 代码

ALTER DATABASE InvestorRelations
SET TRUSTWORTHY ON;
 
 
USE InvestorRelations
 
CREATE ASSEMBLY fExampleTVF
FROM 'E:\学习\SessionTest\TestKZCCGC\bin\Debug\TestKZCCGC.dll'
WITH PERMISSION_SET=EXTERNAL_ACCESS
  
CREATE FUNCTION fTVFExample(
@RootDir nvarchar(max),
@WildCard nvarchar(max),
@IncludeSubDirs bit
)
RETURNS TABLE (
    FileName nvarchar(max),
    LastWriteTime datetime
)
AS EXTERNAL NAME fExampleTVF.UserDefinedFunctions.DirectoryList
 
SELECT FILENAME,LASTWRITETIME
FROM dbo.fTVFExample('E:\学习','*.ppt',0)

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM