我能夠在本地使用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