SQL Server ->> CLR存储过程枚举目录文件并返回结果集
因工作需要写了个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存储过程枚举目录文件并返回结果集的更多相关文章
- (转)jdbc 调用 sql server 的存储过程时“该语句没有返回结果集”的解决方法
本文转载自:http://hedyn.iteye.com/blog/856040 在JDBC中调用SQL Server中的存储过程时出现如下异常: com.microsoft.sqlserver.jd ...
- 【转】SQL SERVER CLR存储过程实现
最近做一个项目,需要做一个SQL SERVER 2005的CLR的存储过程,研究了一下CLR的实现.为方便以后再使用,在这里总结一下我的实现流程,也供对CLR感兴趣但又不知道如何实现的朋友们做一下参考 ...
- SQL Server CLR 使用 C# 自定义存储过程和触发器
资源来源:https://www.cnblogs.com/Brambling/p/8016060.html SQL Server CLR 使用 C# 自定义存储过程和触发器 这一篇博客接着上一篇博 ...
- SQL Server中的CLR编程——用.NET为SQL Server编写存储过程和函数
原文:SQL Server中的CLR编程--用.NET为SQL Server编写存储过程和函数 很早就知道可以用.NET为SQL Server2005及以上版本编写存储过程.触发器和存储过程的,不过之 ...
- sql server系统存储过程大全
关键词:sql server系统存储过程,mssql系统存储过程 xp_cmdshell --*执行DOS各种命令,结果以文本行返回. xp_fixeddrives --*查询各磁盘/分区可用空间 x ...
- SQL Server CLR 使用 C# 自定义函数
一.简介 Microsoft SQL Server 2005之后,实现了对 Microsoft .NET Framework 的公共语言运行时(CLR)的集成.CLR 集成使得现在可以使用 .NET ...
- SQL Server 2008中删除errorlog文件的方法
删除error咯个文件[SSQL\MSSQL10.MSSQLSERVER\MSSQL\Log目录下面] 由于默认情况下,SQL Server 保存 7 个 ErrorLog 文件,名为: ErrorL ...
- Sql Server来龙去脉系列之一 目录篇
从工作一直到现在都没怎么花功夫深入学习下Sql Server数据库,在使用Sql Server时90%的时间基本上都是在接触T-SQL,所以数据库这块基本上属于菜鸟级别.至于数据库的底层框架以及运行机 ...
- 在sql server中建存储过程,如果需要参数是一个可变集合怎么处理?
在sql server中建存储过程,如果需要参数是一个可变集合的处理 原存储过程,@objectIds 为可变参数,比如 110,98,99 ALTER PROC [dbo].[Proc_totalS ...
随机推荐
- 215 Kth Largest Element in an Array 快排
题目:在无序的数组中找到第k大的元素,也就是若长度为n的数组从小到大排列时,下标为n-k的元素. 注意Example2:第4大的元素是4,也就是数组中出现的两个5分别是第2大和第3大的数字. 解法一: ...
- bzoj3262 陌上花开 cdq分治(入门)
题目传送门 思路:cdq分治处理偏序关系的模板题,主要就是学cdq分治吧,还在入门中. 代码其实也很好理解,记得树状数组操作的上限是 z的最大值,不是n的最大值,这个细节wa了好久. #include ...
- C# 利用Powershell获取网络相关信息
利用Get-NetAdapter获取信息 Get-NetAdapter 参考链接:https://docs.microsoft.com/en-us/powershell/module/netadapt ...
- SQL批量提交修改业务
把你需要批量提交修改的东西在内存中修改完毕 然后执行以下代码 SqlConnection conn = new SqlConnection(ConnectionString);SqlDataAdapt ...
- hdu1509 优先队列
Windows Message Queue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- 超级详细全截图化VMware 安装ubantu
一,下载镜像 由于ubantu时国外源所以下载十分的缓慢 这里我用清华源下载:https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/18.10/ub ...
- D. Match & Catch 后缀自动机 || 广义后缀自动机
http://codeforces.com/contest/427/problem/D 题目是找出两个串的最短公共子串,并且在两个串中出现的次数只能是1次. 正解好像是dp啥的,但是用sam可以方便很 ...
- (转)Shell中read的用法详解
Shell中read的用法详解 原文:http://blog.csdn.net/jerry_1126/article/details/77406500 read的常用用法如下: read -[pstn ...
- Ace教你一步一步做Android新闻客户端(一)
复制粘贴了那么多博文很不好意思没点自己原创的也说不出去,现在写一篇一步一步教你做安卓新闻客户端,借此机会也是让自己把相关的技术再复习一遍,大神莫笑,专门做给新手看. 手里存了两篇,一个包括软件视图 和 ...
- IAR使用技巧 之 快捷键批量更换指定字符(以及Keil的全局替换功能)
使用IAR(或者Keil)写/移植程序时批量更换字符 作者:李剀 出处:https://www.cnblogs.com/kevin-nancy/p/10776712.html 或者 https://b ...