2011-09-14 16 views
2

がサポートされていない私は、Windows 7上のSQL Server Expressの2008 SP1を(バージョン6.1ビルド7601:サービスパック1)持っているとVisual Studio 2010sqlFileStream System.ComponentModel.Win32Exception:要求が

を私が作成しようとしています次のコードを使用してファイルをファイルストリームに挿入するためのストアドプロシージャCLR。

using System; 
using System.Data; 
using System.Data.SqlClient; 
using System.Data.SqlTypes; 
using Microsoft.SqlServer.Server; 
using System.IO; 
using System.Security.Principal; 

public partial class StoredProcedures 
{ 
    [Microsoft.SqlServer.Server.SqlProcedure] 
    public static void sp_fileController(String friendlyName, String filePath) 
{ 
    SqlParameter fDataParam = new System.Data.SqlClient.SqlParameter("@fData", SqlDbType.VarBinary, -1); 
    SqlParameter fNameParam = new System.Data.SqlClient.SqlParameter("@fName", SqlDbType.NVarChar, 300); 

    WindowsIdentity newId = SqlContext.WindowsIdentity; 
    WindowsImpersonationContext impersonatedUser = newId.Impersonate(); 

    try 
    { 
     string cs = @"Server=[myservername];Integrated Security=true"; 
     using (SqlConnection con = new SqlConnection(cs)) 
     { 
      con.Open(); 
      SqlTransaction objSqlTran = con.BeginTransaction(); 

      //string sql = "INSERT INTO fileStreamTest VALUES ((Cast('' As varbinary(Max))), @fName, default); Select fData.PathName() As Path From fileStreamTest Where fId = SCOPE_IDENTITY()";//OUTPUT inserted.fid 
      SqlCommand insertFileCommand = con.CreateCommand(); 

      insertFileCommand.Transaction = objSqlTran; 

      insertFileCommand.CommandText = "INSERT INTO fileStreamTest.dbo.fileStreamTest (RowGuid, fData) VALUES (@FileID, CAST ('' as varbinary(max)))"; 

      Guid newFileID = Guid.NewGuid(); 

      insertFileCommand.Parameters.Add("@FileID", SqlDbType.UniqueIdentifier).Value = newFileID; 

      insertFileCommand.ExecuteNonQuery(); 

      SqlCommand getPathAndTokenCommand = con.CreateCommand(); 

      getPathAndTokenCommand.Transaction = objSqlTran; 

      getPathAndTokenCommand.CommandText = 
       "SELECT fData.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT() " + 
       "FROM fileStreamTest.dbo.fileStreamTest " + 
       "WHERE rowGuid = @FileID"; 

      getPathAndTokenCommand.Parameters.Add("@FileID", SqlDbType.UniqueIdentifier).Value = newFileID; 

      SqlDataReader tokenReader = getPathAndTokenCommand.ExecuteReader(CommandBehavior.SingleRow); 

      tokenReader.Read(); 

      SqlString filePathName = tokenReader.GetSqlString(0); 

      SqlBinary fileToken = tokenReader.GetSqlBinary(1); 

      tokenReader.Close(); 

      SqlFileStream sqlFile = new SqlFileStream(filePathName.Value, fileToken.Value, System.IO.FileAccess.ReadWrite); 
      sqlFile.Close(); 

      objSqlTran.Rollback(); 
      //objSqlTran.Commit(); 
      con.Close(); 

     } 
    } 
    finally 
    { 
     impersonatedUser.Undo(); 
    } 
} 
}; 

しかしそれはラインに到達したとき:

SqlFileStream sqlFile = new SqlFileStream(filePathName.Value, fileToken.Value, System.IO.FileAccess.ReadWrite); 

私が取得:

A .NET Frameworkのエラーは、ユーザー定義のルーチンまたは集計 "sp_fileController" の実行中に発生した

System.ComponentModel.Win32Exception: The request is not supported 
System.ComponentModel.Win32Exception: 
    at System.Data.SqlTypes.SqlFileStream.OpenSqlFileStream(String path, Byte[] transactionContext, FileAccess access, FileOptions options, Int64 allocationSize) 
    at System.Data.SqlTypes.SqlFileStream..ctor(String path, Byte[] transactionContext, FileAccess access, FileOptions options, Int64 allocationSize) 
    at System.Data.SqlTypes.SqlFileStream..ctor(String path, Byte[] transactionContext, FileAccess access) 
    at StoredProcedures.sp_fileController(String friendlyName, String filePath) 

誰でもこの問題を解決する方法を教えてもらえますか? SQL 2008 Express Editionでこのようにコードを実行することはできませんか?

答えて

0

それは思ったほど奇妙あなたがMicrosoft Connect issue 768308で読むことができるよう、マイクロソフトは、意図的に(あなたがEXTERNAL_ACCESSを許可またはUNSAFEとしてアセンブリを宣言していても)のSQL CLRアセンブリ内のSqlFileStreamクラスの使用法の使用を阻止しました。

しかし、ストリームとしてSqlBytesタイプ(実際には素晴らしいヒントはblog postにあります)の方法でFILESTREAMにアクセスできます。少なくとも読み取り専用の用途では、私は書いたことがありません。

は私がコピーを過ぎてブログが消えた場合には、コード(やや改善されたバージョンでは、オブジェクトの適切な処分で):

using System; 
using System.IO; 
using System.Data.SqlTypes; 
using Microsoft.SqlServer.Server; 
using System.Security.Cryptography; 

public partial class UserDefinedFunctions 
{ 
    [Microsoft.SqlServer.Server.SqlFunction(DataAccess = DataAccessKind.None, IsDeterministic = true, SystemDataAccess = SystemDataAccessKind.None)] 
    public static SqlBinary Hash(SqlBytes source, SqlString hashAlgorithmName) 
    { 
     if (Source.IsNull) 
     { 
      return null; 
     } 

     using (HashAlgorithm ha = GetHashAlgotithm(hashAlgorithmName.Value)) 
     using (Stream stream = Source.Stream) 
     { 
      return new SqlBinary(ha.ComputeHash(source.Stream)); 
     } 
    } 
} 

私は、これは間違いなく、読み取り専用アクセスのために動作を確認することができます。私は書き込みアクセスを試みたことがありません(私は外部のC#WindowsサービスまたはWebアプリケーションからFILESTREAMデータを書きます)。

+0

これは古い質問ですが、とにかく役に立たない 'System.ComponentModel.Win32Exception:リクエストはサポートされていません.'というエラーメッセージの解決策を見つけようとしていて、答えに記述されている解決策を見つけました。 – Fulvio