因工作需要写了个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. Codeforces - 38G 可持久化Treap 区间操作

    题意:\(n\)个人排队,每个人有重要度\(p\)和不要脸度\(c\),如果第\(i\)个人的重要度大于第\(i-1\)个人的重要度,那么他们之间可以交换,不要脸度-1,交换后先前的第\(i\)个人也 ...

  2. POJ - 1061 扩展gcd

    题意:求\((n-m)t+Lk=x-y\)的解\(t\) #include<iostream> #include<algorithm> #include<cstdio&g ...

  3. vue 同一个组件的跳转, 返回时保留原来的下拉位置

    1,需求分析 公司的项目有这样一个需求: 同一个list组件,根据传过来的listId渲染成多个页面,每个页面都可以下拉.在返回到不同的list页面时,要保留当时下拉的位置. 说的我自己都有点懵逼了, ...

  4. [转] Linux命令——timeout

    [From] https://blog.csdn.net/xiaqunfeng123/article/details/54315390 Linux命令——timeout 命令简介 运行指定的命令,如果 ...

  5. 一个矩阵 JavaScript

    //矩阵运算的函数 ;(function(global){ global.Matrix = { //生成对角矩阵,非零元素都为1 eye : function( n ){ var result = [ ...

  6. OPENROWSET read excel

    由于64位系统已经不支持 oledb 4.0访问 xls 1. 未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序 去http://download.microsoft. ...

  7. 2.Exadata 硬件体系结构

    Exadata 硬件加构: 高性能,低成本 冗余 线性扩展 ,具有超强性能,开箱即用         例 2-2的满配: 8台数据服务器组成(2C 6核) (3-2是10核, 4-2,5-2 是12核 ...

  8. Spring JDBC Framework

    引自 :学习经典:Spring JDBC Framework 这里记录我对Spring JDBC框架的学习.由于Spring JDBC和我之前做的工作有很多共同之处,学习经典Framework的设计, ...

  9. Spring 和整个体系 @Value注解 配合属性文件.property

    未来学习方向   重要思路 学的时候看官方文档,系统地学,比如学Spring Boot ,但真正使用的时候,有比自动化(条件化)配置,约定即配置 更好的方法(这个可读性不强,对电脑来说它执行的代码一样 ...

  10. [Matlab] figure

    figure只能设置序号 不能设置title 而stem和plot可以设置title