Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
Last active August 29, 2015 14:10
Show Gist options
  • Save AlexArchive/2fba07d46701acc11a7d to your computer and use it in GitHub Desktop.
Save AlexArchive/2fba07d46701acc11a7d to your computer and use it in GitHub Desktop.
SQL FILESTREAM with ADO.NET and C#
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.IO;
using System.Security.Cryptography;
namespace Uhu
{
public class Program
{
private static void Main()
{
//Insert();
//UninstallDb();
//InstallDb();
//Insert();
Read();
}
private static void Create()
{
var connectionStr = ConfigurationManager.ConnectionStrings["Sound"].ConnectionString;
var transactionContext = new byte[0];
var path = string.Empty;
using (var connection = new SqlConnection(connectionStr))
{
connection.Open();
using (var command = new SqlCommand())
{
command.Connection = connection;
command.CommandText = @"
INSERT INTO [Uhu].[dbo].[Sound]
OUTPUT
GET_FILESTREAM_TRANSACTION_CONTEXT() AS transactionContext,
inserted.Audio.PathName() AS filePath
SELECT NEWID(), 0x;";
using (var transaction = connection.BeginTransaction())
{
command.Transaction = transaction;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
transactionContext = (byte[])reader["transactionContext"];
path = reader["filePath"].ToString();
}
}
using (var destination =
new SqlFileStream(path, transactionContext, FileAccess.Write))
{
using (var source =
File.OpenRead("C:\\Users\\ByteBlast\\Desktop\\Screenie.png"))
{
source.CopyTo(destination);
}
}
command.Transaction.Commit();
}
}
}
}
private static void UninstallDb()
{
var connectionStr = ConfigurationManager.ConnectionStrings["Sound"].ConnectionString;
var builder = new SqlConnectionStringBuilder(connectionStr);
builder.InitialCatalog = "Master";
using (var connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
using (var command = new SqlCommand())
{
command.Connection = connection;
command.CommandText = @"
IF DB_ID('Uhu') IS NOT NULL BEGIN
DROP DATABASE [Uhu]
END";
command.ExecuteNonQuery();
}
}
}
private static void InstallDb()
{
var connectionStr = ConfigurationManager.ConnectionStrings["Sound"].ConnectionString;
var builder = new SqlConnectionStringBuilder(connectionStr);
builder.InitialCatalog = "Master";
using (var connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
using (var command = new SqlCommand())
{
command.Connection = connection;
const string schema = @"
IF DB_ID('Uhu') IS NOT NULL BEGIN
DROP DATABASE [Uhu]
END
CREATE DATABASE [Uhu] ON
PRIMARY (
NAME = [Uhu],
FILENAME = 'M:\Databases\Uhu\Uhu.mdf'
),
FILEGROUP [Uhu_fs] CONTAINS FILESTREAM (
NAME = [Uhu_fs],
FILENAME = 'M:\Databases\Uhu\Uhu_fs')
LOG ON (
NAME = [Uhu_log],
FILENAME = 'M:\Databases\Uhu\Uhu.ldf')
GO
USE [Uhu]
GO
CREATE TABLE [dbo].[Sound] (
[Id] UNIQUEIDENTIFIER ROWGUIDCOL NOT NULL UNIQUE,
[Audio] VARBINARY(MAX) FILESTREAM NULL
)
GO";
foreach (var statement in
schema.Split(new[] { "GO" }, StringSplitOptions.RemoveEmptyEntries))
{
command.CommandText = statement;
command.ExecuteNonQuery();
}
}
}
}
private static void Read()
{
var connectionStr = ConfigurationManager.ConnectionStrings["Sound"].ConnectionString;
var transactionContext = new byte[0];
var path = string.Empty;
using (var connection = new SqlConnection(connectionStr))
{
connection.Open();
using (var command = new SqlCommand())
{
command.Connection = connection;
command.CommandText = @"
SELECT TOP 1
Audio.PathName() AS filePath,
GET_FILESTREAM_TRANSACTION_CONTEXT() as transactionContext
FROM [Uhu].[dbo].[Sound]";
using (var transaction = connection.BeginTransaction())
{
command.Transaction = transaction;
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
transactionContext = (byte[])reader["transactionContext"];
path = reader["filePath"].ToString();
}
}
using (var source =
new SqlFileStream(path, transactionContext, FileAccess.Read))
{
using (var destination =
File.OpenWrite("C:\\Users\\ByteBlast\\Desktop\\Magic.png"))
{
source.CopyTo(destination);
}
}
command.Transaction.Commit();
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment