c# - sqlFileStream 系统.ComponentModel.Win32Exception : The request is not supported

标签 c# sql-server sql-server-2008 sqlclr sqlfilestream

我在 Windows 7(版本 6.1 Build 7601:Service Pack 1)和 Visual Studio 2010 上安装了 SQL Server Express 2008 SP1。

我正在尝试创建一个存储过程 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);

我得到:

在执行用户定义例程或聚合“sp_fileController”期间发生 .NET Framework 错误:

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 以这种方式执行代码吗?

最佳答案

我想我在这里找到了可行的解决方案:

https://social.msdn.microsoft.com/Forums/sqlserver/en-US/f49def09-3b47-4e54-8a53-2dd47762821e/filestream-on-windows-server-2012-the-request-is-not-supported?forum=sqldatabaseengine

总结:添加注册表项解决了 SQL Server 11.0.7001 上的问题:

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters\FsctlAllowlist]
"FSCTL_SQL_FILESTREAM_FETCH_OLD_CONTENT"=dword:0x00092560

关于c# - sqlFileStream 系统.ComponentModel.Win32Exception : The request is not supported,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7422850/

相关文章:

c# - 重新启动具有不同输入的线程

sql - Microsoft SQL Server Management Studio 2012 的默认服务器名称

sql-server - 有没有办法在 .sql 脚本中创建区域?

sql - 如何按 ASC 和 DESC 顺序对同一列进行排序而不重复

sql-server-2008 - SQL 服务器 (T-SQL) : Avoid call scalar function multiple times

sql-server-2008 - SSRS 中的运行值图表 - 如何让每个系列从 0 开始?

c# - 将文本添加到带有图标的按钮

c# - 在 C# 中查找 gsm 调制解调器端口

c# - 无法将 'Microsoft.NETCore.App (>= 2.0.0)' 解析为 '.NETCoreApp,Version=v2.0'

sql - 使用 SQL Server 或 Vertica 将按日期范围的记录转换为按日/月的记录