Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you have the byte[] in C# code, you could easily write to a file:</p> <p><a href="http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.io.file.writeallbytes.aspx</a></p> <p>Would this work for you?</p> <p><strong>EDIT:</strong> Another idea if you must initiate the file save from SQL Server might be to create a <a href="http://msdn.microsoft.com/en-us/library/w2kae45k.aspx" rel="nofollow">SQL Server CLR Function</a>. This would allow you to execute C# code from SQL Server.</p> <p><strong>EDIT 2:</strong> I did a Google search and found an article on <a href="http://www.codeproject.com/KB/database/blob_compress.aspx" rel="nofollow">CodeProject</a>. In the article, they are compressing and decompressing blob data. I think the trick here is to use the <a href="http://msdn.microsoft.com/en-us/library/system.data.sqltypes.sqlbytes.aspx" rel="nofollow">SqlBytes</a> parameter type vs a plain old byte[]. Below is some (untested) code I came up with after reading the article and your updated question:</p> <pre><code>using System; using System.Data; using System.Data.SqlClient; using System.Data.SqlTypes; using Microsoft.SqlServer.Server; using System.IO; using System.IO.Compression; public partial class UserDefinedFunctions { // Setting function characteristics [Microsoft.SqlServer.Server.SqlFunction(IsDeterministic=true, DataAccess=DataAccessKind.None)] public static void fn_WriteToFile(string fileName, SqlBytes blob) { if( blob.IsNull || String.IsNullOrEmpty(fileName) ) { return; } String fullFileName = Path.Combine(Environment.GetEnvironmentVariable("TEMP"), fileName); using( FileStream fileStream = new FileStream(fullFileName, FileMode.OpenOrCreate, FileAccess.Write)) using( BinaryWriter binaryWriter = new BinaryWriter(fileStream) ) { binaryWriter.Write(blob.Buffer); } } }; </code></pre> <p>Let me know what you think.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload