I am getting the following error:
Cannot find the tye 'IENumerator', becuase it does not exist or you do not have permission
Any ideas?
I am attempting to get an example i found online to work.
Below is the code:
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Collections;
using System.Security.Principal;
namespace SqlServerProject1CSharp
{
// The record that holds each row returned by the TVF.
public class FileRecord
{
public FileStream fContents;
public string fName;
public FileRecord(string fn, FileStream fc)
{
this.fName = fn;
this.fContents = fc;
}
}
public class MultiFileAccess
{
public MultiFileAccess()
{
}
/// <summary>
/// The fill row method that cracks the FileRecord and returns the individual columns.
/// </summary>
/// <param name="Obj"></param>
/// <param name="FName"></param>
/// <param name="FContents"></param>
public static void FillRow(object obj, ref SqlString fName, ref SqlBytes fContents)
{
if ( obj != null )
{
FileRecord record1 = (FileRecord)obj;
fName = (SqlString)record1.fName;
fContents = new SqlBytes(record1.fContents);
}
else
{
fName = SqlString.Null;
fContents = SqlBytes.Null;
}
}
/// <summary>
/// Root method that returns MultiFileLoader which does the bulk of the work.
/// </summary>
/// <param name="PathName"></param>
/// <param name="Pattern"></param>
/// <returns></returns>
[SqlFunction(FillRowMethodName = "FillRow", TableDefinition = "FName nvarchar(300), FContents varbinary(max)")]
public static IEnumerator GetFilesInDirectory(string pathName, string pattern)
{
return new MultiFileLoader(pathName, pattern);
}
}
// The core of the TVF's implementation that implements IEnumerator.
public class MultiFileLoader : IEnumerator
{
private WindowsIdentity callerIdentity;
private int currentFile;
private FileStream currentFileStream;
private string[] fileNames;
public MultiFileLoader(string pathName, string pattern)
{
this.currentFile = -1;
this.callerIdentity = SqlContext.WindowsIdentity;
WindowsImpersonationContext context1 = this.callerIdentity.Impersonate();
try
{
this.fileNames = Directory.GetFiles(pathName, pattern);
}
catch
{
this.fileNames = null;
}
finally
{
if ( context1 != null )
{
context1.Undo();
}
}
}
/// <summary>
/// Retrieves the FileStream for the current file as pointed to by the CurrentFile array index.
/// Constructs a FileRecord with the current file's name and the contents (as FileStream) and
/// returns it.
/// Impersonates caller before opening the file and unimpersonates after getting the filestream.
/// </summary>
public object Current
{
get
{
WindowsImpersonationContext context1 = this.callerIdentity.Impersonate();
try
{
this.currentFileStream = new FileStream(this.fileNames[this.currentFile], FileMode.Open);
}
catch
{
this.currentFileStream = null;
}
finally
{
if ( context1 != null )
{
context1.Undo();
}
}
return new FileRecord(this.fileNames[this.currentFile], this.currentFileStream);
}
}
/// <summary>
/// Advances the CurrentFile position in the FileNames array.
/// Also makes sure to close the currently open FileStream before advancing to the next File.
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
if ( this.fileNames == null )
{
return false;
}
if ( this.currentFile < (this.fileNames.Length - 1) )
{
this.currentFile++;
if ( this.currentFileStream != null )
{
this.currentFileStream.Close();
}
return true;
}
if ( this.currentFileStream != null )
{
this.currentFileStream.Close();
return false;
}
return false;
}
public virtual void Reset()
{
this.currentFile = -1;
}
}
}
Cannot find the tye 'IENumerator', becuase it does not exist or you do not have permission
Any ideas?
I am attempting to get an example i found online to work.
Below is the code:
using System;
using System.Data;
using System.Data.Sql;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.IO;
using System.Collections;
using System.Security.Principal;
namespace SqlServerProject1CSharp
{
// The record that holds each row returned by the TVF.
public class FileRecord
{
public FileStream fContents;
public string fName;
public FileRecord(string fn, FileStream fc)
{
this.fName = fn;
this.fContents = fc;
}
}
public class MultiFileAccess
{
public MultiFileAccess()
{
}
/// <summary>
/// The fill row method that cracks the FileRecord and returns the individual columns.
/// </summary>
/// <param name="Obj"></param>
/// <param name="FName"></param>
/// <param name="FContents"></param>
public static void FillRow(object obj, ref SqlString fName, ref SqlBytes fContents)
{
if ( obj != null )
{
FileRecord record1 = (FileRecord)obj;
fName = (SqlString)record1.fName;
fContents = new SqlBytes(record1.fContents);
}
else
{
fName = SqlString.Null;
fContents = SqlBytes.Null;
}
}
/// <summary>
/// Root method that returns MultiFileLoader which does the bulk of the work.
/// </summary>
/// <param name="PathName"></param>
/// <param name="Pattern"></param>
/// <returns></returns>
[SqlFunction(FillRowMethodName = "FillRow", TableDefinition = "FName nvarchar(300), FContents varbinary(max)")]
public static IEnumerator GetFilesInDirectory(string pathName, string pattern)
{
return new MultiFileLoader(pathName, pattern);
}
}
// The core of the TVF's implementation that implements IEnumerator.
public class MultiFileLoader : IEnumerator
{
private WindowsIdentity callerIdentity;
private int currentFile;
private FileStream currentFileStream;
private string[] fileNames;
public MultiFileLoader(string pathName, string pattern)
{
this.currentFile = -1;
this.callerIdentity = SqlContext.WindowsIdentity;
WindowsImpersonationContext context1 = this.callerIdentity.Impersonate();
try
{
this.fileNames = Directory.GetFiles(pathName, pattern);
}
catch
{
this.fileNames = null;
}
finally
{
if ( context1 != null )
{
context1.Undo();
}
}
}
/// <summary>
/// Retrieves the FileStream for the current file as pointed to by the CurrentFile array index.
/// Constructs a FileRecord with the current file's name and the contents (as FileStream) and
/// returns it.
/// Impersonates caller before opening the file and unimpersonates after getting the filestream.
/// </summary>
public object Current
{
get
{
WindowsImpersonationContext context1 = this.callerIdentity.Impersonate();
try
{
this.currentFileStream = new FileStream(this.fileNames[this.currentFile], FileMode.Open);
}
catch
{
this.currentFileStream = null;
}
finally
{
if ( context1 != null )
{
context1.Undo();
}
}
return new FileRecord(this.fileNames[this.currentFile], this.currentFileStream);
}
}
/// <summary>
/// Advances the CurrentFile position in the FileNames array.
/// Also makes sure to close the currently open FileStream before advancing to the next File.
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
if ( this.fileNames == null )
{
return false;
}
if ( this.currentFile < (this.fileNames.Length - 1) )
{
this.currentFile++;
if ( this.currentFileStream != null )
{
this.currentFileStream.Close();
}
return true;
}
if ( this.currentFileStream != null )
{
this.currentFileStream.Close();
return false;
}
return false;
}
public virtual void Reset()
{
this.currentFile = -1;
}
}
}