因工作需要写了个CLR存储过程枚举目录文件并返回结果集

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server; //Author: Jerry Chen(v-jerrch@microsoft.com)
//Date: 10/7/2015
//Comment: This is a CLR class that provides methods to be called to do something beyond the ability of SQL Server
//
// namespace StoredProcedures
{
public partial class EnumerateSourceFileDirectory
{
[SqlProcedure()]
public static void GetFileListByBeginEndAndPattern(
SqlString SourceFolder, SqlDateTime BeginModDate, SqlDateTime EndModDate, SqlString FileNamePattern, SqlInt16 IsSubfolderScanned)
{
if (SourceFolder.ToString().Length == )
{
throw new System.ArgumentException("SourceFolder cannot be null or empty.", "");
} if ((!BeginModDate.IsNull && !EndModDate.IsNull && BeginModDate > EndModDate))
{
throw new System.ArgumentException("'Begin Modify Date' shouldn't be later than 'End Modify Date'.", "");
} //Comment out because Directory.Exists doesn't work for network path
//if (!Directory.Exists(SourceFolder.ToString()))
//{
// throw new System.ArgumentException("Source folder doesn't exist.", "");
//} DirectoryInfo DirInfo = new DirectoryInfo(SourceFolder.ToString());
DateTime dt1 = (DateTime)BeginModDate;
DateTime dt2 = (DateTime)EndModDate; var files = from file in DirInfo.EnumerateFiles(FileNamePattern.ToString(), IsSubfolderScanned == ? SearchOption.AllDirectories: SearchOption.TopDirectoryOnly)
where file.CreationTimeUtc > dt1 & file.CreationTimeUtc < dt2
select file; //create a SqlDataRecord to store file info
SqlDataRecord rec = new SqlDataRecord(new SqlMetaData[] {
new SqlMetaData("FileName", SqlDbType.NVarChar,),
new SqlMetaData("FilFullName", SqlDbType.NVarChar,),
new SqlMetaData("CreateDateUTC", SqlDbType.DateTime),
new SqlMetaData("ModifyDateUTC", SqlDbType.DateTime),
new SqlMetaData("Size_in_bytes", SqlDbType.BigInt),
}); // start sending and tell the pipe to use the created record
SqlContext.Pipe.SendResultsStart(rec);
{
foreach (var file in files)
{
rec.SetSqlString(, file.Name);
rec.SetSqlString(, file.FullName);
rec.SetDateTime(, file.CreationTimeUtc);
rec.SetDateTime(, file.LastWriteTimeUtc);
rec.SetInt64(, file.Length); // send new record/row
SqlContext.Pipe.SendResultsRow(rec);
}
}
SqlContext.Pipe.SendResultsEnd(); // finish sending
} [SqlProcedure()]
public static void GetFileListByBeginEndAndExtension(
SqlString SourceFolder, SqlDateTime BeginModDate, SqlDateTime EndModDate, SqlString FileExtension, SqlInt16 IsSubfolderScanned)
{
if (SourceFolder.ToString().Length == )
{
throw new System.ArgumentException("SourceFolder cannot be null or empty.", "");
} if ((!BeginModDate.IsNull && !EndModDate.IsNull && BeginModDate > EndModDate))
{
throw new System.ArgumentException("'Begin Modify Date' shouldn't be later than 'End Modify Date'.", "");
} //Comment out because Directory.Exists doesn't work for network path
//if (!Directory.Exists(SourceFolder.ToString()))
//{
// throw new System.ArgumentException("Source folder doesn't exist.", "");
//} DirectoryInfo DirInfo = new DirectoryInfo(SourceFolder.ToString());
DateTime dt1 = (DateTime)BeginModDate;
DateTime dt2 = (DateTime)EndModDate; var files = from file in DirInfo.EnumerateFiles("*", IsSubfolderScanned == ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
where file.CreationTimeUtc > dt1 & file.CreationTimeUtc < dt2 & file.Extension == FileExtension.ToString()
select file; //create a SqlDataRecord to store file info
SqlDataRecord rec = new SqlDataRecord(new SqlMetaData[] {
new SqlMetaData("FileName", SqlDbType.NVarChar,),
new SqlMetaData("FilFullName", SqlDbType.NVarChar,),
new SqlMetaData("CreateDateUTC", SqlDbType.DateTime),
new SqlMetaData("ModifyDateUTC", SqlDbType.DateTime),
new SqlMetaData("Size_in_bytes", SqlDbType.BigInt),
}); // start sending and tell the pipe to use the created record
SqlContext.Pipe.SendResultsStart(rec);
{
foreach (var file in files)
{
rec.SetSqlString(, file.Name);
rec.SetSqlString(, file.FullName);
rec.SetDateTime(, file.CreationTimeUtc);
rec.SetDateTime(, file.LastWriteTimeUtc);
rec.SetInt64(, file.Length); // send new record/row
SqlContext.Pipe.SendResultsRow(rec);
}
}
SqlContext.Pipe.SendResultsEnd(); // finish sending
} [SqlProcedure()]
public static void GetFileListByExtension(
SqlString SourceFolder, SqlString FileExtension, SqlInt16 IsSubfolderScanned)
{
//validation
if (SourceFolder.ToString().Length == )
{
throw new System.ArgumentException("SourceFolder cannot be null or empty.", "");
} //Comment out because Directory.Exists doesn't work for network path
//if (!Directory.Exists(SourceFolder.ToString()))
//{
// throw new System.ArgumentException("Source folder doesn't exist.", "");
//} //set directory
DirectoryInfo DirInfo = new DirectoryInfo(SourceFolder.ToString()); //enumerate files
var files = from file in DirInfo.EnumerateFiles("*", IsSubfolderScanned == ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
where file.Extension == FileExtension.ToString()
select file; //create a SqlDataRecord to store file info
SqlDataRecord rec = new SqlDataRecord(new SqlMetaData[] {
new SqlMetaData("FileName", SqlDbType.NVarChar,),
new SqlMetaData("FilFullName", SqlDbType.NVarChar,),
new SqlMetaData("CreateDateUTC", SqlDbType.DateTime),
new SqlMetaData("ModifyDateUTC", SqlDbType.DateTime),
new SqlMetaData("Size_in_bytes", SqlDbType.BigInt),
}); // start sending and tell the pipe to use the created record
SqlContext.Pipe.SendResultsStart(rec);
{
foreach (var file in files)
{
rec.SetSqlString(, file.Name);
rec.SetSqlString(, file.FullName);
rec.SetDateTime(, file.CreationTimeUtc);
rec.SetDateTime(, file.LastWriteTimeUtc);
rec.SetInt64(, file.Length); // send new record/row
SqlContext.Pipe.SendResultsRow(rec);
}
}
SqlContext.Pipe.SendResultsEnd(); // finish sending
} [SqlProcedure()]
public static void GetFileListByExtensionAndPattern(
SqlString SourceFolder, SqlString FileExtension, SqlString FileNamePattern, SqlInt16 IsSubfolderScanned)
{
//validation
if (SourceFolder.ToString().Length == )
{
throw new System.ArgumentException("SourceFolder cannot be null or empty.", "");
} //Comment out because Directory.Exists doesn't work for network path
//if (!Directory.Exists(SourceFolder.ToString()))
//{
// throw new System.ArgumentException("Source folder doesn't exist.", "");
//} //set directory
DirectoryInfo DirInfo = new DirectoryInfo(SourceFolder.ToString()); //enumerate files
var files = from file in DirInfo.EnumerateFiles(FileNamePattern.ToString(), IsSubfolderScanned == ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)
where file.Extension == FileExtension.ToString()
select file; //create a SqlDataRecord to store file info
SqlDataRecord rec = new SqlDataRecord(new SqlMetaData[] {
new SqlMetaData("FileName", SqlDbType.NVarChar,),
new SqlMetaData("FilFullName", SqlDbType.NVarChar,),
new SqlMetaData("CreateDateUTC", SqlDbType.DateTime),
new SqlMetaData("ModifyDateUTC", SqlDbType.DateTime),
new SqlMetaData("Size_in_bytes", SqlDbType.BigInt),
}); // start sending and tell the pipe to use the created record
SqlContext.Pipe.SendResultsStart(rec);
{
foreach (var file in files)
{
rec.SetSqlString(, file.Name);
rec.SetSqlString(, file.FullName);
rec.SetDateTime(, file.CreationTimeUtc);
rec.SetDateTime(, file.LastWriteTimeUtc);
rec.SetInt64(, file.Length); // send new record/row
SqlContext.Pipe.SendResultsRow(rec);
}
}
SqlContext.Pipe.SendResultsEnd(); // finish sending
}
}
}

SQL Server ->> CLR存储过程枚举目录文件并返回结果集的更多相关文章

  1. (转)jdbc 调用 sql server 的存储过程时“该语句没有返回结果集”的解决方法

    本文转载自:http://hedyn.iteye.com/blog/856040 在JDBC中调用SQL Server中的存储过程时出现如下异常: com.microsoft.sqlserver.jd ...

  2. 【转】SQL SERVER CLR存储过程实现

    最近做一个项目,需要做一个SQL SERVER 2005的CLR的存储过程,研究了一下CLR的实现.为方便以后再使用,在这里总结一下我的实现流程,也供对CLR感兴趣但又不知道如何实现的朋友们做一下参考 ...

  3. SQL Server CLR 使用 C# 自定义存储过程和触发器

    资源来源:https://www.cnblogs.com/Brambling/p/8016060.html SQL Server CLR 使用 C# 自定义存储过程和触发器   这一篇博客接着上一篇博 ...

  4. SQL Server中的CLR编程——用.NET为SQL Server编写存储过程和函数

    原文:SQL Server中的CLR编程--用.NET为SQL Server编写存储过程和函数 很早就知道可以用.NET为SQL Server2005及以上版本编写存储过程.触发器和存储过程的,不过之 ...

  5. sql server系统存储过程大全

    关键词:sql server系统存储过程,mssql系统存储过程 xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 x ...

  6. SQL Server CLR 使用 C# 自定义函数

    一.简介 Microsoft SQL Server 2005之后,实现了对 Microsoft .NET Framework 的公共语言运行时(CLR)的集成.CLR 集成使得现在可以使用 .NET ...

  7. SQL Server 2008中删除errorlog文件的方法

    删除error咯个文件[SSQL\MSSQL10.MSSQLSERVER\MSSQL\Log目录下面] 由于默认情况下,SQL Server 保存 7 个 ErrorLog 文件,名为: ErrorL ...

  8. Sql Server来龙去脉系列之一 目录篇

    从工作一直到现在都没怎么花功夫深入学习下Sql Server数据库,在使用Sql Server时90%的时间基本上都是在接触T-SQL,所以数据库这块基本上属于菜鸟级别.至于数据库的底层框架以及运行机 ...

  9. 在sql server中建存储过程,如果需要参数是一个可变集合怎么处理?

    在sql server中建存储过程,如果需要参数是一个可变集合的处理 原存储过程,@objectIds 为可变参数,比如 110,98,99 ALTER PROC [dbo].[Proc_totalS ...

随机推荐

  1. C# 判别系统版本以及Win10的识别办法

    首先得知道操作系统和主次版本的对应关系 我们可以用Environment.OSVersion来判断当前操作系统 && Environment.OSVersion.Version.Min ...

  2. [八分之一的男人]POJ - 1743 后缀数组 height分组 带详解

    题意:求最长不可重叠的相同差值子串的长度 这道题算是拖了好几个月,现在花了点时间应该搞懂了不少,尝试分析一下 我们首先来解决一个退化的版本,求最长不可重叠的相同子串(差值为0) 比如\(aabaaba ...

  3. [转] open-falcon编写的整个脑洞历程

    [From] https://mp.weixin.qq.com/s?__biz=MjM5OTcxMzE0MQ==&mid=400225178&idx=1&sn=c98609a9 ...

  4. 【研究】Struts2-048漏洞

    1.1 漏洞背景 2017年7月7日,Apache Struts发布最新的安全公告,Apache Struts2-strus1-plugin插件存在远程代码执行的高危漏洞,漏洞编号为CVE-2017- ...

  5. PIE SDK分类统计

    1. 算法功能简介 分类统计功能是将分类后的结果统计输出. PIE SDK支持算法功能的执行,下面对分类统计算法功能进行介绍. 2. 算法功能实现说明 2.1. 实现步骤 第一步 算法参数设置 第二步 ...

  6. 配置node.js中的express框架

    玩node.js,不玩后台那就是杀鸡牛刀,今天没事整理一下以前开发node.js后台的心得 1.首先安装node.js以及cnpm,在这儿我就不说了,看我node.js中的另一篇文章node.js的安 ...

  7. opencv + ffmpeg

    opencv2.4.13 与 ffmepg 3.0 一起是可以安装成功的.注意编译ffmpeg时, ./configure   --enable-shared 否则会报错. 另外,把以上组合换成ope ...

  8. vuex中filter的使用 && 快速判断一个数是否在一个数组中

    vue中filter的使用 computed: mapState({ items: state => state.items.filter(function (value, index, arr ...

  9. java多线程lock的使用

    看代码: package com.ming.thread.reentrantlock; import java.util.concurrent.locks.Lock; import java.util ...

  10. JS中彻底删除json对象组成的数组中的元素

    只是分享一个小知识~ 在JS中,对于某个由json对象组成的数组,例如: var test = [{ "a": "1", "b": &quo ...