【问题标题】:CREATE ASSEMBLY in SQLCLR using assembly byte string failed because it could not open the physical file在 SQLCLR 中使用程序集字节字符串创建程序集失败,因为它无法打开物理文件
【发布时间】:2012-03-17 05:29:33
【问题描述】:

我正在尝试将 user-defined aggregates 添加到 SQL Server 2008 数据库,其架构在我的 Visual Studio 2010 解决方案文件中定义。此自定义聚合的代码位于同一解决方案中的 SQLCLR 项目中,但我很难通过单元测试。对于这个项目的单元测试,我使用类似于here 的方法来获取程序集的字节字符串。测试套件或构建后事件每次都需要自动更新我的脚本以创建程序集,因为在任何类型的 MSBuild(从命令行或 Visual Studio)之后,程序集位都会发生变化。

脚本似乎正在更新其程序集位字符串以正确匹配每次运行的当前程序集位。但是单元测试仍然失败,底层 SQLException 说,“CREATE ASSEMBLY 失败,因为它无法打开物理文件“0xDEADBEEF”:3(系统找不到指定的路径。)。(我已经删除了真正的程序集位从那条消息中。)为什么当组装位看起来正确时会出现这个错误?我应该如何解决这个问题?

实际汇编位串的长度约为 16 KB。这会是个问题吗? CREATE ASSEMBLY 是否需要整个程序集的位或只是一个标题,我如何只获得标题?

我们已经准备好部署此数据库的单元测试基础架构,包括运行部署前和部署后脚本。我们还设置了用户定义的函数和聚合所需的授权。

这是我的部署后脚本:

IF EXISTS (SELECT name FROM sys.assemblies WHERE name = 'foo')
   DROP ASSEMBLY foo
go

-- The assembly bits change after every build from the command line or Visual Studio.  
-- This variable needs to change each time the foo assembly changes.
declare @assemblyBits as varchar(max)
-- This string is really about 15000 characters, including some newlines which are removed
-- in the next block.  Word-wrapping this line or not word-wrapping it seems to have no 
-- effect.
set @assemblyBits = '0x42'

-- Trim whitespace from assembly bits.
declare @sanitizedBits as varchar(max)
set @sanitizedBits = Replace(Replace(Replace(@assemblyBits, Char(10), ''), Char(13), ''), Char(9), '')

CREATE ASSEMBLY foo
AUTHORIZATION bar
from @sanitizedBits 
GO

IF EXISTS (SELECT name FROM sysobjects WHERE name = 'Product')
   DROP AGGREGATE Product
go

CREATE AGGREGATE [Aggregations].[Product]
    (@value float,
    @weight float)
    RETURNS float
    EXTERNAL NAME foo.[foo.Product]
go

【问题讨论】:

    标签: visual-studio-2010 unit-testing sql-server-2008-r2 sqlclr


    【解决方案1】:

    尝试在托管数据库的远程服务器上执行创建程序集代码。它应该可以解决问题。

    【讨论】:

      【解决方案2】:

      如果内容为字符串,则使用动态 sql

          EXEC ('CREATE ASSEMBLY foo AUTHORIZATION bar from ' + @sanitizedBits )
      

      通常你会传入varbinary

      【讨论】:

      • 是的,将 varchar 变量更改为 varbinary 有效。谢谢!
      猜你喜欢
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-06
      • 1970-01-01
      • 2012-07-20
      相关资源
      最近更新 更多