I managed to use SQL Filestream locally, but when I try to upload files to a remote SQL server that uses SQL authentication, I get an Access Denied exception. Apparently, SQL Filestream only works with Windows authentication (Integrated Security = true), and not with the SQL authentication that we currently have.
Nobody uses Windows authentication in a production environment, so I just want to know how to overcome this limitation. What is the best practice?
public static void AddItem(RepositoryFile repository, byte[] data)
{
using (var scope = new TransactionScope())
{
using (var db = new MyEntities())
{
db.RepositoryTable.AddObject(repository);
db.SaveChanges();
}
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
using (var cmd = new SqlCommand(string.Format("SELECT Data.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() FROM dbo.RepositoryTable WHERE ID='{0}'", repository.ID), con))
{
cmd.Connection.Open();
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
var path = reader.GetString(0);
var transactionContext = reader.GetSqlBytes(1).Buffer;
var fileStream = new SqlFileStream(path, transactionContext, FileAccess.Write);
fileStream.Write(contents, 0, contents.Length);
fileStream.Close();
}
}
}
scope.Complete();
}
}
source
share