Skip to content

Instantly share code, notes, and snippets.

@caseywatson
Created August 8, 2013 21:54
Show Gist options
  • Select an option

  • Save caseywatson/6189129 to your computer and use it in GitHub Desktop.

Select an option

Save caseywatson/6189129 to your computer and use it in GitHub Desktop.
This SQL CLR UDF will download data given a URL and return a VarBinary. Helpful in cases where you are moving binary objects from the database to some other location (in my case, Azure) with minimal impact.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Net;
public partial class UserDefinedFunctions
{
[Microsoft.SqlServer.Server.SqlFunction]
public static SqlBytes Download(SqlString sqlUrl)
{
var url = sqlUrl.ToString();
if (String.IsNullOrEmpty(url))
return null;
using (var webClient = new WebClient())
{
return new SqlBytes(webClient.DownloadData(url));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment