微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – sqlFileStream System.ComponentModel.Win32Exception:不支持该请求

我在 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,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,FileAccess access)
   at StoredProcedures.sp_fileController(String friendlyName,String filePath)

谁能告诉我如何解决这个问题?只是我不能用sql 2008 Express版本以这种方式执行代码

解决方法

我想我在这里找到了有效的解决方案:

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐