SQL SERVER expand Stored Procedures

C # has been a call to the database objects, expanding the stored procedure presented here can call the C # dll in the DB and to return some information DB (table or string)

C # code follows a path following return to the main document, to test

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];
    }
}
View Code

Code 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

Finally, you can inquire at:

SELECT FILENAME,LASTWRITETIME
FROM dbo.fTVFExample('E:\学习','*.ppt',0)


When you query, there may be given as follows:

 

 

 

Of course, in some cases, this also flawed, if you need to disable this part of the functions, see the following article:

https://www.cnblogs.com/chenmh/p/8257369.html

Guess you like

Origin www.cnblogs.com/ziqiumeng/p/11229605.html