我能够在本地使用SQL Filestream,但是当我尝试将文件上载到使用SQL身份验证的远程SQL服务器时,出现Access Denied异常。显然,SQL文件流只适用于Windows身份验证(集成安全性= true),而不适用于我们目前拥有的SQL身份验证。SQL文件流身份验证最佳做法
没有人真的在生产环境中使用Windows身份验证,所以我只想知道如何克服这个限制。最佳做法是什么?
public static void AddItem(RepositoryFile repository, byte[] data)
{
using (var scope = new TransactionScope())
{
using (var db = new MyEntities()) // DBContext
{
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)) // "Data" is the column name which has the FILESTREAM. Data.PathName() gives me the local path to the file.
{
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); // I get the error at this line.
fileStream.Close();
}
}
}
scope.Complete();
}
}
你能告诉我们你得到异常的代码吗? – 2010-12-15 16:11:19
“没有人真正在生产环境中使用Windows身份验证”我已经实现了以下所有情况(Windows服务,Web应用程序和桌面应用程序) – 2010-12-15 16:11:34
@Phil Hunt - 我已经用我的代码片段。当我尝试将字节写入文件时,出现错误。该过程显然无法访问该文件。 – tempid 2010-12-15 16:24:24