因工作需要写了个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. 【floyd】【bitset】洛谷 P1841 [JSOI2007]重要的城市 题解

        bitset玄学完美优化复杂度? 题目描述 参加jsoi冬令营的同学最近发现,由于南航校内修路截断了原来通向计算中心的路,导致去的路程比原先增加了近一公里.而食堂门前施工虽然也截断了原来通向计 ...

  2. POJ - 3735 循环操作

    构造n+1元组,m次方的矩阵代表循环操作 本题尚有质疑之处(清零操作的正确性还有单位矩阵的必要性),题解可能会改正 #include<iostream> #include<algor ...

  3. [原创]Aop之使用Autofac+Castle 自动注入服务且动态代理服务实现拦截(非MVC控制器拦截)

    public static class AutofacComponentManualRegister { /// <summary> /// 注册 /// </summary> ...

  4. jenkins显示发送邮件发送成功但是邮箱没收到

    jenkins显示发送邮件发送成功但是邮箱没收到 解决方案: 重新配置一下系统管理-系统设置-Extended E-mail Notification

  5. NPOI开发手记

    目录 注意事项 读取Excel 创建Excel表 保存Excel 行 列 单元格样式 添加公式 Dataset.DataGridView转换Excel帮助类 NPOI其实就是POI的.NET移植 项目 ...

  6. storm中几个概念的大小关系

    从图可以看出来:topology>supervisor>worker>excutor>task; 也就是说一个topology可以运行在多个supervisor上,一个supe ...

  7. 【程序员技术练级】学习一门脚本语言 python(一)文件处理

    现在工作上主要用的语言是java,java在企业级的应用上能够发挥很好的用途,但有时候要做一个小功能时,比如批量更新文件,抓取网页等,这时候用java就显得太笨重了.因此就学习了python这门脚本语 ...

  8. Js内存泄漏的几种情况

    想解决内存泄露问题,必须知道什么是内存泄露,什么情况下出现内存泄露,才能在遇到问题时,逐个排除.这里只讨论那些不经意间的内存泄露. 一.什么是内存泄露 内存泄露是指一块被分配的内存既不能使用,又不能回 ...

  9. BNU29139——PvZ once again——————【矩阵快速幂】

    PvZ once again Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java cla ...

  10. sqlite3在别的目录写文件的问题

    今天碰到一个文件,就是sqlite数据不能把db创建在别的目录下.找了好久不得其解.后来换了一个sqlite jar包就好了. 原来我用的是sqlite-nested 内嵌的jar包. 换成这里的包h ...