using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System; using System.IO; public class FileProcessor { [SqlFunction()] public static SqlBytes GetBytesFromFile(string sFilePath) { System.IO.FileStream fs = System.IO.File.Open(sFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read); long lngLen = fs.Length; byte[] abytBuffer = new byte[(int)lngLen - 1]; fs.Read(abytBuffer, 0, (int)lngLen - 1); fs.Close(); SqlBytes b = new SqlBytes(abytBuffer); return b; } [SqlFunction()] public static int SaveFileToDisk(SqlString sFileName, SqlBytes Blob) { using (System.IO.FileStream file = new System.IO.FileStream(sFileName.Value, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write)) { try { byte[] Buffer = new byte[Blob.Length]; for (int i = 0; i < Blob.Length; i++) Buffer[i] = Blob[i]; System.IO.MemoryStream stream = new System.IO.MemoryStream(Buffer); byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); file.Write(bytes, 0, bytes.Length); stream.Close(); return 1; } catch (System.Exception ex) { using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\temp\error.txt", true)) { sw.Write(ex.Message); } return 0; } } } }